Java中的常用組件

Java中的常用組件

常用組件
JLable 標籤 構造可以傳文字,圖片,Iocn
JButton 按鈕
JTextField 文本域
JComboBox 下拉列表
JCheckBox 複選框
JRadioButton 單選框
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ComponentTest {
public static void main(String[] args) {
new MyFrame03();
}
}
class MyFrame03 extends JFrame{
JPanel panel;
JLabel label;
JButton button;
JTextField jTextField;
JComboBox comboBox;
JCheckBox checkBox1;
JRadioButton radioButton1;
JRadioButton radioButton2;
JRadioButton radioButton3;
ButtonGroup bg1;
ButtonGroup bg2;
JCheckBox checkBox2;
public MyFrame03(){
//基本設置
this.setSize(600, 400);
this.setVisible(true);
this.setTitle(“常用組件測試”);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

	//初始化面板
	panel = new JPanel();
	//初始化標籤
	label = new JLabel("我是標籤");
	//添加標籤
	panel.add(label);
	//初始化按鈕
	button = new JButton("我是按鈕");
	//添加按鈕
	panel.add(button);
	//初始化文本域
	jTextField = new JTextField("123456");
	//添加文本域
	panel.add(jTextField);
	//定義一個城市數組
	String [] city = {"北京","天津","上海"};
	//初始化下拉列表
	comboBox = new JComboBox(city);
	//添加下拉列表
	panel.add(comboBox);
	//初始化單選框
	radioButton1 = new JRadioButton("尖尖");
	radioButton2 = new JRadioButton("丁丁");
	radioButton3 = new JRadioButton("紅兵");
	bg1 = new ButtonGroup();
	//添加單選框
	bg1.add(radioButton1);
	bg1.add(radioButton2);
	bg1.add(radioButton3);
	panel.add(radioButton1);
	panel.add(radioButton2);
	panel.add(radioButton3);
	//初始化複選
	checkBox1 = new JCheckBox("路過");
	checkBox2 = new JCheckBox("路燈");

// bg2 = new ButtonGroup();
// bg2.add(checkBox1);
// bg2.add(checkBox2);
//添加複選按鈕
panel.add(checkBox1);
panel.add(checkBox2);
//添加面板
this.add(panel);
}
}

發佈了42 篇原創文章 · 獲贊 703 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章