JAVA GUI界面编程常用组件

代码及注释

package 界面编辑1;

public class WXmain {

	public static void main(String[] args) {
		WX wx =new WX();//创建对象
		
	}

}
package 界面编辑1;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class WX extends JFrame{
	public WX() {
		this.setTitle("基本文本框");
		this.setBounds(30, 40, 500, 600);//窗口在屏幕中的位置、大小
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭方式
		this.setVisible(true);//窗口可见
		
		FlowLayout layout = new FlowLayout();//创建流动式布局对象
		this.setLayout(layout);//将窗口设置为流动式布局
		//按键
		JButton button1 = new JButton("按键1");
		this.add(button1);
		
		Button button2 = new Button("按键2");
		this.add(button2);
		//多行文本框
		JTextArea jt1 = new JTextArea(20,30);
		this.add(jt1);
		//单行文本框
		JTextField jt2 = new JTextField(10);
		this.add(jt2);
		//标签 不可编辑的显示组件
		JLabel jlb = new JLabel("标签");
		this.add(jlb);
		//下拉选择框
		Choice choice1 = new Choice();
		choice1.add("下拉框1");
		choice1.add("下拉框2");
		this.add(choice1);
		//将多个复选框组件合成一组,一组Checkbox组件只有一个可选择
		CheckboxGroup choice2 = new CheckboxGroup();
		//复选框组件(也可当作单选框使用)
		Checkbox male = new Checkbox("男",choice2,true);
		Checkbox famale = new Checkbox("女",choice2,false);
		this.add(male);
		this.add(famale);
		//列表框 “1”表示一次性可以可见1行
		List list = new List(1);
		list.add("第一行");
		list.add("第二行");
		list.add("第三行");
		list.add("第四行");
		this.add(list);

		//如果需要输入有规律按键,可借鉴
		for(int i=1;i<10;i++) {
			this.add(new JButton(i+""));
		}
		
	}

}

运行结果

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理了Java GUI编程基础知识! public class AllFrame { /** * 这里的代码涉及到 GUI 编程基本面板、基本组件、事件(ActionListener、ChangeListener、MouseListener) */ public AllFrame() { // new 一个主窗体 也可以这样写 new MainFrame(); MainFrame mf = new MainFrame(); } public static void main(String[] args) { new AllFrame(); } // 主窗体 继承 窗体基本格式 类 Jframe 以下弹出窗口均继承 Jframe class MainFrame extends Jframe implements ActionListener { JMenuBar menubar = new JMenuBar();;// 菜单条 JMenu jmenu[] = new JMenu[5];// panelMenu,usedMenu1,usedMenu2,layoutMenu,helpMenu; // 菜单项 JButton closebtn;// 关闭窗体 // 具体的选项 JMenuItem panelitem[] = new JMenuItem[4]; JMenuItem useditem1[] = new JMenuItem[6]; JMenuItem useditem2[] = new JMenuItem[6]; JMenuItem layoutitem[] = new JMenuItem[3]; JMenuItem helpitem = new JMenuItem("关于"); // 菜单中名称 String menu[] = { "面板使用", "常用组件一", "常用组件二", "布局设置", "帮助" }; String pane[] = { "普通面板", "滚动面板", "分割面板", "选项面板" }; String uesd1[] = { "标签", "按钮", "文本框", "文本域", "单选按钮" ,"微调"}; String uesd2[] = { "滑块","复选框", "下拉菜单", "列表", "表格", "树状" }; String layout[] = { "流式布局", "边界布局", "网格布局" }; // Jpanelitem,Jscrollpaneitem,Splitpaneitem,JTabbedpanelitem, // Jlabelitem,Jbuttonitem,JTextfielditem,JTextareaitem,JRadiobuttonitem, // JCheckboxitem,JCombomoxitem,Jlistitem,Jtableitem,Jtreeitem, // Flowlayoutitem,Borderlayoutitem,Girdlayoutitem, // Helpitem}; public MainFrame() { this.setTitle("JAVA图型界面学习(GUI)"); // 加载菜单条到窗体中 this.setJMenuBar(menubar); // 重新设置大小 this.setSize(380, 290); // 简单的居中方法 在窗体基本格式类 Jframe 中有详细的居中方法 this.setLocationRelativeTo(null); // 设置关闭窗口 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 调用方法 menu(); // 设置容器 Container con = this.getContentPane(); // 定义一个 标签 “JAVA图型界面学习” JLabel jl = new JLabel("JAVA图型界面学习"); // 组件 设置字体大小, 其他组件用法相同 jl.setFont(new Font("隶书", Font.BOLD, 24)); // 具体定位 组件名.setBounds(x, y, width, height) jl.setBounds(30, 30, 280, 50); // 把标签加到 con 容器中 con.add(jl); /

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值