GUI编程
1、AWT
1.1 介绍
- AWT = Abstract Windows tool:抽象的窗口工具
- 包含了很多类和接口
- 元素:窗口,按钮,文本框
- 核心类:组件(component),容器(container)

1.2 组件和容器
1.2.1 Frame
| public class FrameDemo01 { |
| public static void main(String[] args) { |
| |
| Frame frame = new Frame("第一个Frame窗口"); |
| |
| frame.setVisible(true); |
| |
| frame.setSize(400,400); |
| |
| frame.setBackground(new Color(163, 155, 155)); |
| |
| frame.setLocation(200,200); |
| |
| frame.setResizable(false); |
| } |
| } |
封装Frame
| class MyFrame extends Frame { |
| static int id = 0; |
| |
| |
| public MyFrame(int x,int y,int width,int height,Color color) { |
| super("MyFrame+"+(++id)); |
| setBackground(color); |
| setVisible(true); |
| setBounds(x,y,width,height); |
| setResizable(false); |
| } |
1.2.2 Panel
- 解决了窗口关闭问题
- panel不能单独存在
- 显示和管理其他组件
| package com.quinn.frame; |
| |
| import java.awt.*; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class PanelDemo01 { |
| public static void main(String[] args) { |
| Frame frame = new Frame(); |
| |
| Panel panel = new Panel(); |
| frame.setLayout(null); |
| |
| frame.setBounds(400,400,400,400); |
| frame.setBackground(Color.blue); |
| |
| panel.setBounds(100,100,200,200); |
| panel.setBackground(Color.pink); |
| |
| frame.add(panel); |
| frame.setVisible(true); |
| |
| |
| |
| frame.addWindowListener(new WindowAdapter() { |
| |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| } |
1.3 布局管理器
1.3.1 流式布局
- 从上到下,从左到右
| package com.quinn.frame; |
| |
| import java.awt.*; |
| |
| public class FlowLayoutDemo01 { |
| public static void main(String[] args) { |
| Frame frame = new Frame(); |
| |
| Button btn1 = new Button("btn1"); |
| Button btn2 = new Button("btn2"); |
| Button btn3 = new Button("btn3"); |
| Button btn4 = new Button("btn4"); |
| |
| |
| frame.setLayout(new FlowLayout(FlowLayout.CENTER)); |
| |
| frame.setSize(300,300); |
| |
| |
| frame.add(btn1); |
| frame.add(btn2); |
| frame.add(btn3); |
| frame.add(btn4); |
| |
| frame.setVisible(true); |
| |
| } |
| } |
1.3.2 东西南北中
| package com.quinn.frame; |
| |
| import java.awt.*; |
| |
| public class BorderLayoutDemo01 { |
| public static void main(String[] args) { |
| Frame frame = new Frame(); |
| frame.setSize(300,300); |
| |
| Button east = new Button("East"); |
| Button west = new Button("West"); |
| Button south = new Button("South"); |
| Button north = new Button("North"); |
| Button center = new Button("Center"); |
| |
| frame.add(east,BorderLayout.EAST); |
| frame.add(west,BorderLayout.WEST); |
| frame.add(south,BorderLayout.SOUTH); |
| frame.add(north,BorderLayout.NORTH); |
| frame.add(center,BorderLayout.CENTER); |
| |
| frame.setVisible(true); |
| } |
| } |
1.3.3 网格布局
| package com.quinn.frame; |
| |
| import java.awt.*; |
| |
| public class GirdLayoutDemo01 { |
| public static void main(String[] args) { |
| Frame frame = new Frame(); |
| |
| Button btn1 = new Button("btn1"); |
| Button btn2 = new Button("btn2"); |
| Button btn3 = new Button("btn3"); |
| Button btn4 = new Button("btn4"); |
| Button btn5 = new Button("btn5"); |
| Button btn6 = new Button("btn6"); |
| |
| |
| frame.setLayout(new GridLayout(3,2)); |
| |
| frame.add(btn1); |
| frame.add(btn2); |
| frame.add(btn3); |
| frame.add(btn4); |
| frame.add(btn5); |
| frame.add(btn6); |
| |
| |
| frame.pack(); |
| |
| frame.setVisible(true); |
| } |
| } |
1.4 事件监听
- 事件监听:当某个事情发生的时候,怎么样?
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class ActionDemo01 { |
| public static void main(String[] args) { |
| |
| Frame frame = new Frame(); |
| Button button = new Button(); |
| |
| MyActionListener myActionListener = new MyActionListener(); |
| |
| button.addActionListener(myActionListener); |
| |
| frameClose(frame); |
| |
| frame.add(button,BorderLayout.CENTER); |
| frame.pack(); |
| frame.setVisible(true); |
| } |
| |
| |
| public static void frameClose(Frame frame){ |
| frame.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| } |
| |
| |
| class MyActionListener implements ActionListener{ |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| System.out.println("aaa"); |
| } |
| } |
多个按钮共享一个事件
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| |
| public class ActionDemo02 { |
| public static void main(String[] args) { |
| |
| |
| Frame frame = new Frame(); |
| Button start = new Button("start"); |
| Button stop = new Button("stop"); |
| |
| start.setActionCommand("start"); |
| stop.setActionCommand("stop"); |
| MyMonitor myMonitor = new MyMonitor(); |
| |
| start.addActionListener(myMonitor); |
| stop.addActionListener(myMonitor); |
| |
| frame.add(start,BorderLayout.NORTH); |
| frame.add(stop,BorderLayout.SOUTH); |
| |
| frame.pack(); |
| frame.setVisible(true); |
| } |
| } |
| |
| class MyMonitor implements ActionListener{ |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| |
| System.out.println("按钮被点击了:"+); |
| |
| } |
| } |
1.5 TextFied
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| |
| public class ActionDemo03 { |
| public static void main(String[] args) { |
| |
| new MyFrame(); |
| } |
| } |
| |
| class MyFrame extends Frame{ |
| public MyFrame() { |
| TextField textField = new TextField(); |
| add(textField); |
| |
| MyActionListener2 myActionListener = new MyActionListener2(); |
| |
| textField.addActionListener(myActionListener); |
| |
| |
| textField.setEchoChar('*'); |
| |
| pack(); |
| setVisible(true); |
| |
| } |
| } |
| class MyActionListener2 implements ActionListener{ |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| TextField textField = (TextField) e.getSource(); |
| System.out.printf(textField.getText()); |
| textField.setText(""); |
| } |
| } |
1.6 简易计算器
1.6.1 优化前:面向过程
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class Calculator { |
| public static void main(String[] args) { |
| new MyCalc(); |
| } |
| } |
| |
| class MyCalc extends Frame { |
| |
| |
| public MyCalc(){ |
| |
| TextField textField1 = new TextField(10); |
| TextField textField2 = new TextField(10); |
| TextField textField3 = new TextField(20); |
| |
| |
| Label label = new Label("+"); |
| |
| |
| Button button = new Button("="); |
| MyCalcMonitor myMonitor1 = new MyCalcMonitor(textField1,textField2,textField3); |
| button.addActionListener(myMonitor1); |
| |
| setLayout(new FlowLayout()); |
| add(textField1); |
| add(label); |
| add(textField2); |
| add(button); |
| add(textField3); |
| pack(); |
| setVisible(true); |
| |
| frameClose(this); |
| |
| } |
| public static void frameClose(Frame frame){ |
| frame.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| |
| |
| } |
| class MyCalcMonitor implements ActionListener{ |
| private TextField text1,text2,text3; |
| |
| public MyCalcMonitor(TextField text1, TextField text2, TextField text3) { |
| this.text1 = text1; |
| this.text2 = text2; |
| this.text3 = text3; |
| } |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| int a = Integer.parseInt(text1.getText()); |
| int b =Integer.parseInt(text2.getText()); |
| text3.setText(""+(a+b)); |
| System.out.println(a+"+"+b+"="+text3.getText()); |
| text1.setText(""); |
| text2.setText(""); |
| text3.setText(""); |
| |
| } |
| } |
1.6.2 优化后:面向对象
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class Calculator { |
| public static void main(String[] args) { |
| new MyCalc().loadFrame(); |
| } |
| } |
| |
| class MyCalc extends Frame { |
| TextField textField1,textField2,textField3; |
| |
| public void loadFrame(){ |
| |
| textField1 = new TextField(10); |
| textField2 = new TextField(10); |
| textField3 = new TextField(20); |
| Label label = new Label("+"); |
| |
| |
| Button button = new Button("="); |
| |
| MyCalcMonitor myMonitor1 = new MyCalcMonitor(this); |
| button.addActionListener(myMonitor1); |
| |
| setLayout(new FlowLayout()); |
| add(textField1); |
| add(label); |
| add(textField2); |
| add(button); |
| add(textField3); |
| pack(); |
| setVisible(true); |
| frameClose(this); |
| } |
| |
| |
| |
| public static void frameClose(Frame frame){ |
| frame.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| } |
| |
| class MyCalcMonitor implements ActionListener{ |
| MyCalc myCalc = null; |
| |
| public MyCalcMonitor(MyCalc myCalc) { |
| this.myCalc = myCalc; |
| } |
| |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| int a = Integer.parseInt(myCalc.textField1.getText()); |
| int b = Integer.parseInt(myCalc.textField2.getText()); |
| myCalc.textField3.setText(""+(a+b)); |
| System.out.println(myCalc.textField1.getText()+"+"+myCalc.textField2.getText()+"="+myCalc.textField3.getText()); |
| myCalc.textField1.setText(""); |
| myCalc.textField2.setText(""); |
| } |
| } |
1.6.3 最佳:内部类
好处是可以畅通无阻的访问外部类的属性和方法
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class Calculator { |
| public static void main(String[] args) { |
| new MyCalc().loadFrame(); |
| } |
| } |
| |
| class MyCalc extends Frame { |
| TextField textField1,textField2,textField3; |
| |
| public void loadFrame(){ |
| |
| textField1 = new TextField(10); |
| textField2 = new TextField(10); |
| textField3 = new TextField(20); |
| Label label = new Label("+"); |
| |
| |
| Button button = new Button("="); |
| |
| MyCalcMonitor myMonitor1 = new MyCalcMonitor(); |
| button.addActionListener(myMonitor1); |
| |
| setLayout(new FlowLayout()); |
| add(textField1); |
| add(label); |
| add(textField2); |
| add(button); |
| add(textField3); |
| pack(); |
| setVisible(true); |
| frameClose(this); |
| } |
| |
| private class MyCalcMonitor implements ActionListener{ |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| int a = Integer.parseInt(textField1.getText()); |
| int b = Integer.parseInt(textField2.getText()); |
| textField3.setText(""+(a+b)); |
| System.out.println(textField1.getText()+"+"+textField2.getText()+"="+textField3.getText()); |
| textField1.setText(""); |
| textField2.setText(""); |
| } |
| } |
| |
| |
| public static void frameClose(Frame frame){ |
| frame.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| } |
| |
| |
1.7 Paint
| package com.quinn.frame.demo02; |
| |
| import java.awt.*; |
| |
| public class PaintDemo01 { |
| public static void main(String[] args) { |
| new MyPoint().loadFrame(); |
| } |
| } |
| |
| class MyPoint extends Frame{ |
| public void loadFrame(){ |
| |
| setBounds(200,200,600,500); |
| setVisible(true); |
| } |
| @Override |
| public void paint(Graphics g) { |
| g.setColor(Color.blue); |
| g.drawOval(100,100,100,100); |
| g.fillOval(300,300,100,100); |
| |
| } |
| } |
1.8 鼠标监听
- 实现鼠标画画
| package com.quinn.frame.demo03; |
| |
| import java.awt.*; |
| import java.awt.event.MouseAdapter; |
| import java.awt.event.MouseEvent; |
| import java.util.ArrayList; |
| import java.util.Iterator; |
| |
| public class MouseDemo01 { |
| public static void main(String[] args) { |
| new MyFrame("Paint"); |
| } |
| } |
| class MyFrame extends Frame{ |
| private ArrayList points; |
| public MyFrame(String title) { |
| super(title); |
| setBounds(200,200,400,300); |
| |
| |
| points = new ArrayList<>(); |
| |
| this.addMouseListener(new MyMouseListener()); |
| setVisible(true); |
| } |
| |
| @Override |
| public void paint(Graphics g) { |
| |
| Iterator it = points.iterator(); |
| while (it.hasNext()){ |
| Point point = (Point) it.next(); |
| g.setColor(Color.blue); |
| g.fillOval(point.x,point.y,10,10); |
| } |
| } |
| |
| |
| public void addPoint(Point point){ |
| points.add(point); |
| } |
| |
| private class MyMouseListener extends MouseAdapter { |
| |
| @Override |
| public void mousePressed(MouseEvent e) { |
| MyFrame myFrame = (MyFrame) e.getSource(); |
| |
| myFrame.addPoint(new Point(e.getX(),e.getY())); |
| |
| |
| myFrame.repaint(); |
| } |
| |
| } |
| } |
1.9 窗口监听
| package com.quinn.frame.demo03; |
| |
| import java.awt.*; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class WindowDemo01 { |
| public static void main(String[] args) { |
| new MyWindowFrame(); |
| } |
| } |
| class MyWindowFrame extends Frame{ |
| public MyWindowFrame(){ |
| setBackground(Color.pink); |
| setBounds(400,400,300,200); |
| setVisible(true); |
| addWindowListener(new MyWindowListener()); |
| } |
| |
| class MyWindowListener extends WindowAdapter{ |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| } |
| } |
优化后
| package com.quinn.frame.demo03; |
| |
| import java.awt.*; |
| import java.awt.event.WindowAdapter; |
| import java.awt.event.WindowEvent; |
| |
| public class WindowDemo01 { |
| public static void main(String[] args) { |
| new MyWindowFrame(); |
| } |
| } |
| class MyWindowFrame extends Frame{ |
| public MyWindowFrame(){ |
| setBackground(Color.pink); |
| setBounds(400,400,300,200); |
| setVisible(true); |
| this.addWindowListener(new WindowAdapter() { |
| @Override |
| public void windowClosing(WindowEvent e) { |
| System.exit(0); |
| } |
| }); |
| } |
| |
| |
| } |
1.10 键盘监听
| package com.quinn.frame.demo03; |
| |
| import java.awt.*; |
| import java.awt.event.KeyAdapter; |
| import java.awt.event.KeyEvent; |
| |
| public class KeyDemo01 { |
| public static void main(String[] args) { |
| new MyFrame1(); |
| } |
| } |
| class MyFrame1 extends Frame { |
| public MyFrame1() { |
| setBackground(Color.pink); |
| setBounds(400,400,300,400); |
| setVisible(true); |
| this.addKeyListener(new KeyAdapter() { |
| @Override |
| public void keyPressed(KeyEvent e) { |
| |
| int keyCode = e.getKeyCode(); |
| System.out.println(keyCode); |
| if (keyCode == KeyEvent.VK_K){ |
| System.out.println("press K "); |
| }else { |
| System.out.println("press not K"); |
| } |
| } |
| }); |
| } |
| } |
2、 Swing
2.1 窗口、面板
| package com.quinn.swing.demo01; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class JFrameDemo01 { |
| |
| public void init(){ |
| JFrame jFrame = new JFrame("这是一个JFrame窗口"); |
| jFrame.setVisible(true); |
| jFrame.setBounds(400,400,300,200); |
| |
| |
| |
| JLabel label = new JLabel("welcome quinn java world"); |
| label.setHorizontalAlignment(SwingConstants.CENTER); |
| |
| jFrame.add(label); |
| jFrame.getContentPane().setBackground(Color.pink); |
| |
| |
| |
| |
| jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JFrameDemo01().init(); |
| } |
| } |
2.2 弹窗
| package com.quinn.swing.demo01; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.awt.event.ActionEvent; |
| import java.awt.event.ActionListener; |
| |
| public class DialogDemo01 extends JFrame { |
| public DialogDemo01(){ |
| this.setVisible(true); |
| this.setBounds(300,400,300,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| |
| |
| Container p = this.getContentPane(); |
| p.setBackground(Color.pink); |
| |
| |
| p.setLayout(null); |
| |
| JButton jButton = new JButton("clicked dialog"); |
| jButton.setBounds(30,30,200,50); |
| p.add(jButton); |
| jButton.addActionListener(new ActionListener() { |
| @Override |
| public void actionPerformed(ActionEvent e) { |
| new MyDialog01(); |
| } |
| }); |
| |
| } |
| |
| public static void main(String[] args) { |
| new DialogDemo01(); |
| } |
| } |
| |
| |
| class MyDialog01 extends JDialog{ |
| public MyDialog01() { |
| this.setVisible(true); |
| this.setBounds(100,100,200,150); |
| |
| Container contentPane = this.getContentPane(); |
| |
| contentPane.add(new Label("Quinn learn java")); |
| } |
| } |
2.3 标签
2.3.1 普通标签
| JLabel label = new JLabel("welcome quinn java world"); |
2.3.2 icon标签
| package com.quinn.swing.demo01; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.net.URL; |
| |
| public class IconDemo02 extends JFrame { |
| public IconDemo02(){ |
| JLabel jLabel = new JLabel("IconDemo02"); |
| |
| |
| URL url = IconDemo02.class.getResource("bd.jpg"); |
| |
| ImageIcon icon = new ImageIcon(url); |
| jLabel.setIcon(icon); |
| jLabel.setHorizontalAlignment(SwingConstants.CENTER); |
| |
| Container container = getContentPane(); |
| container.add(jLabel); |
| setVisible(true); |
| setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| setBounds(100,200,500,500); |
| } |
| |
| public static void main(String[] args) { |
| new IconDemo02(); |
| } |
| } |
2.4 面板
| package com.quinn.swing.demo02; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class JPanelDemo01 extends JFrame { |
| public JPanelDemo01() { |
| Container container = this.getContentPane(); |
| |
| container.setLayout(new GridLayout(2,2,10,10)); |
| |
| |
| |
| |
| |
| |
| JPanel panel1 = new JPanel(new GridLayout(1, 3)); |
| JPanel panel2 = new JPanel(new GridLayout(1, 2)); |
| JPanel panel3 = new JPanel(new GridLayout(2, 1)); |
| JPanel panel4 = new JPanel(new GridLayout(3, 2)); |
| |
| panel1.add(new JButton("1")); |
| panel1.add(new JButton("1")); |
| panel1.add(new JButton("1")); |
| panel2.add(new JButton("2")); |
| panel2.add(new JButton("2")); |
| panel3.add(new JButton("3")); |
| panel3.add(new JButton("3")); |
| panel4.add(new JButton("4")); |
| panel4.add(new JButton("4")); |
| panel4.add(new JButton("4")); |
| panel4.add(new JButton("4")); |
| panel4.add(new JButton("4")); |
| panel4.add(new JButton("4")); |
| |
| container.add(panel1); |
| container.add(panel2); |
| container.add(panel3); |
| container.add(panel4); |
| |
| this.setVisible(true); |
| this.setBounds(200,100,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JPanelDemo01(); |
| } |
| } |
| package com.quinn.swing.demo02; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class JScrollDemo01 extends JFrame { |
| public JScrollDemo01() { |
| Container container = this.getContentPane(); |
| |
| JTextArea textArea = new JTextArea(20,30); |
| textArea.setText("welcome learn java"); |
| |
| JScrollPane scrollPane = new JScrollPane(textArea); |
| container.add(scrollPane); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JScrollDemo01(); |
| } |
| } |
2.6 按钮
2.6.1 图片按钮
| package com.quinn.swing.demo03; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.net.URL; |
| |
| public class JButtonDemo01 extends JFrame { |
| |
| public JButtonDemo01() { |
| Container container = this.getContentPane(); |
| URL url = JButtonDemo01.class.getResource("bd.jpg"); |
| |
| ImageIcon icon = new ImageIcon(url); |
| |
| JButton button = new JButton(); |
| button.setIcon(icon); |
| button.setToolTipText("图片按钮"); |
| |
| container.add(button); |
| |
| this.setVisible(true); |
| this.setBounds(100,50,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JButtonDemo01(); |
| } |
| } |
2.6.2 单选按钮
| package com.quinn.swing.demo03; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.net.URL; |
| |
| public class JButtonDemo02 extends JFrame { |
| |
| public JButtonDemo02() { |
| Container container = this.getContentPane(); |
| |
| JRadioButton radioButton1 = new JRadioButton("1"); |
| JRadioButton radioButton2 = new JRadioButton("2"); |
| JRadioButton radioButton3 = new JRadioButton("3"); |
| |
| |
| 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); |
| |
| this.setVisible(true); |
| this.setBounds(100,50,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JButtonDemo02(); |
| } |
| } |
2.6.3 复选按钮
| package com.quinn.swing.demo03; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class JButtonDemo03 extends JFrame { |
| |
| public JButtonDemo03() { |
| Container container = this.getContentPane(); |
| |
| JCheckBox checkBox1 = new JCheckBox("1"); |
| JCheckBox checkBox2 = new JCheckBox("2"); |
| JCheckBox checkBox3 = new JCheckBox("3"); |
| |
| container.add(checkBox1,BorderLayout.SOUTH); |
| container.add(checkBox2,BorderLayout.CENTER); |
| container.add(checkBox3,BorderLayout.NORTH); |
| |
| this.setVisible(true); |
| this.setBounds(100,50,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new JButtonDemo03(); |
| } |
| } |
2.7 下拉框
| package com.quinn.swing.demo04; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.awt.event.ItemEvent; |
| import java.awt.event.ItemListener; |
| |
| public class BoxDemo01 extends JFrame { |
| public BoxDemo01() { |
| Container container = this.getContentPane(); |
| |
| |
| JComboBox comboBox = new JComboBox(); |
| comboBox.addItem(null); |
| comboBox.addItem("正在上映"); |
| comboBox.addItem("已下架"); |
| comboBox.addItem("即将上映"); |
| |
| comboBox.addItemListener(new ItemListener() { |
| @Override |
| public void itemStateChanged(ItemEvent e) { |
| System.out.println(e.getItem()); |
| System.out.println("==============="); |
| } |
| }); |
| |
| container.add(comboBox); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new BoxDemo01(); |
| } |
| } |
2.8 列表框
| package com.quinn.swing.demo04; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.awt.event.ItemEvent; |
| import java.awt.event.ItemListener; |
| |
| public class BoxDemo02 extends JFrame { |
| public BoxDemo02() { |
| Container container = this.getContentPane(); |
| |
| String[] contents = {"1","2","3","4"}; |
| |
| JList list = new JList(contents); |
| |
| container.add(list); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new BoxDemo02(); |
| } |
| } |
2.9 文本框
| package com.quinn.swing.text; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class TextDemo01 extends JFrame{ |
| public TextDemo01() { |
| Container container = this.getContentPane(); |
| |
| TextField textField = new TextField("hello"); |
| TextField textField2 = new TextField("world"); |
| |
| container.add(textField,BorderLayout.SOUTH); |
| container.add(textField2,BorderLayout.NORTH); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new TextDemo01(); |
| } |
| } |
2.10 密码框
| package com.quinn.swing.text; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class TextDemo02 extends JFrame{ |
| public TextDemo02() { |
| Container container = this.getContentPane(); |
| |
| JPasswordField passwordField = new JPasswordField(); |
| |
| passwordField.setEchoChar('*'); |
| |
| container.add(passwordField); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new TextDemo02(); |
| } |
| } |
2.11 文本域
| package com.quinn.swing.text; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| |
| public class TextDemo03 extends JFrame{ |
| public TextDemo03() { |
| Container container = this.getContentPane(); |
| |
| JTextArea textArea = new JTextArea(20, 50); |
| textArea.setText("welcome learn java"); |
| JScrollPane scrollPane = new JScrollPane(textArea); |
| container.add(scrollPane); |
| |
| this.setVisible(true); |
| this.setBounds(100,60,400,400); |
| this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| } |
| |
| public static void main(String[] args) { |
| new TextDemo03(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步