SWing基础
1. JFrame窗口
JFrame窗口继承了Frame,类似于Frame的操作,不同在于需要容器Container放东西,关闭窗口调用默认方法即可
public class JFrameDemo {
//init(),初始化
public void init() {
JFrame jFrame = new JFrame("这是一个JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
//jFrame.setBackground(Color.GRAY);
//设置文字
JLabel label = new JLabel("welcome~~~");
jFrame.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container container = jFrame.getContentPane();
container.setBackground(Color.YELLOW);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
2. 弹窗Dialog
用来提示信息,默认有关闭事件
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击按钮的时候,弹出弹窗
button.addActionListener(new ActionListener() {//监听
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog {
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new JLabel("Today is Monday"));
}
}
3. 面板
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel = new JPanel(new GridLayout(1,3));
panel.add(new JButton("1"));
panel.add(new JButton("2"));
panel.add(new JButton("3"));
container.add(panel);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
4. 滑窗
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("hello JScrollDemo");
//面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,50);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
5. 单选多选框
public class JRadioButtonDemo extends JFrame {
public static void main(String[] args) {
new JRadioButtonDemo();
}
public JRadioButtonDemo() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//由于单选框只能选一个,分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
//多选框
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
container.add(checkBox1);
container.add(checkBox2);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
6. 下拉框,列表框
public class TestComboxDemo extends JFrame {
public static void main(String[] args) {
new TestComboxDemo();
}
public TestComboxDemo() {
Container container = this.getContentPane();
//生成列表内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList list = new JList(contents);
container.add(list);
//下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已经下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
7. 文本框
- 文本框
- 密码框
- 文本域