Java GUI编程

1、简介

GUI的核心技术:Swing AWT
1.因为界面不美观
2.需要jre环境
为什么需要学习?
1.可以写出自己心中想要的一些小工具
2.工作时候,也可能需要维护到Swing界面
3.了解MVC架构,了解监听

2、AWT

2.1、AWT介绍

1.包含很多类和接口! GUI!
2.元素:窗口、按钮、文本框
3.java.awt

2.2、组件和容器

  • 窗口Frame
public class TestFrame1 {
    public static void main(String[] args) {
        //创建窗口类
        Frame frame = new Frame("第一个GUI窗口");
        //设置窗口可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(195, 20, 20));
        //设置窗口弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}
public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.red);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.MAGENTA);

    }
}

class MyFrame extends Frame {
    static int id = 0;
    public MyFrame(int x,int y,int w,int h,Color color){
        super("MyFrame"+(++id));
        setVisible(true);
        setBounds(x,y,w,h);
        setBackground(color);
    }
}
  • 面板Panel
public class TestPanel1 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();

        frame.setLayout(null);

        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(0, 253, 51));
        frame.setResizable(false);

        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(253, 0, 79));

        frame.setVisible(true);

        frame.add(panel);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
  • 文本框 TextField
public class Testtext1 {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        MyAction2 myAction2 = new MyAction2();
        textField.addActionListener(myAction2);
        textField.setEchoChar('*');
        pack();
        setVisible(true);
    }
}

class MyAction2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField text = (TextField) e.getSource();
        System.out.println(text.getText());
        text.setText("");
    }
}
  • 画笔 Paint
public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{
    public void loadFrame(){
        setBounds(100,100,600,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        //g.drawOval(100,100,100,100);
        g.fillOval(100,100,100,100);
        g.setColor(Color.GREEN);
        //g.drawRect(150,200,200,200);
        g.fillRect(150,200,200,200);
    }
}

2.3、布局管理器

2.3.1、流式布局FlowLayout
public class TestFlowlayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        frame.setSize(200,200);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
2.3.2、东西南北中BorderLayout
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        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.setSize(200,200);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
2.3.3、表格布局GridLayout
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        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.setSize(200,200);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
  • 鼠标监听
public class TestMouseListener {
    public static void main(String[] args) {
        MyFrame frame = new MyFrame("画图");
    }
}

class MyFrame extends Frame {
    ArrayList points;
    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,300);
        points = new ArrayList();
        this.addMouseListener(new MyMouse());
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        while(iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }

    }
    public void addPaint(Point point){
        points.add(point);
    }

    private class MyMouse extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            frame.addPaint(new Point(e.getX(),e.getY()));
            frame.repaint();
        }
    }
}
  • 窗口监听
public class TestWindows {
    public static void main(String[] args) {
        new MyWindow();
    }
}

class MyWindow extends Frame {
    public MyWindow() {
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }

            @Override
            public void windowOpened(WindowEvent e) {
                System.out.println("windowOpened");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("windowClosed");
            }

            @Override
            public void windowActivated(WindowEvent e) {
                MyWindow myWindow = (MyWindow) e.getSource();
                myWindow.setTitle("激活窗口");
                System.out.println("windowActivated");
            }
        });
    }
}
  • 键盘监听
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(100,100,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_UP){
                    System.out.println("你按下了上键");
                }
            }
        });
    }
}

2.4、事件监听

public class TestActionlistener {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("button");
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        Windowsclose(frame);
        frame.setSize(500,500);
        frame.setVisible(true);
    }

    private static void Windowsclose(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");
    }
}
public class TestAction {
    public static void main(String[] args) {
        Frame frame = new Frame("开始-结束");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        MyAction myAction = new MyAction();

        button1.setActionCommand(button1.getLabel());
        button1.setActionCommand(button1.getLabel());
        button1.addActionListener(myAction);
        button2.addActionListener(myAction);
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        frame.setSize(500,500);
        Windowsclose(frame);
        frame.setVisible(true);
    }
    private static void Windowsclose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyAction implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }
}

2.4、输入框TextField监听

public class Testtext1 {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        MyAction2 myAction2 = new MyAction2();
        textField.addActionListener(myAction2);
        textField.setEchoChar('*');
        pack();
        setVisible(true);
    }
}

class MyAction2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField text = (TextField) e.getSource();
        System.out.println(text.getText());
        text.setText("");
    }
}

简易计算器

public class TestCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame {
    TextField num1 = null;
    TextField num2 = null;
    TextField num3 = null;
    Label label = null;
    Button button = null;
    public void Calculator(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        label = new Label("+");
        button = new Button("=");
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        MyCalculate myCalculate = new MyCalculate();
        button.addActionListener(myCalculate);
        setVisible(true);
    }
    private class MyCalculate implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}

3、Swing

3.1、窗口、面板

public class TestJFrame2 {
    public static void main(String[] args) {
        new MyJFrame().init();
    }
}

class MyJFrame extends JFrame {
    public void init(){
        this.setBounds(100,100,200,200);
        this.setVisible(true);
        JLabel jLabel = new JLabel("好好学习,天天向上");
        this.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = this.getContentPane();
        container.setBackground(Color.CYAN);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

3.2、弹窗

public class TestDialog extends JFrame {

    public TestDialog() {
        this.setVisible(true);
        this.setBounds(100,100,700,500);
        Container container = this.getContentPane();
        container.setBackground(Color.cyan);
        container.setLayout(null);
        JButton button = new JButton("弹窗按钮");
        button.setLayout(null);
        button.setBounds(30,30,200,100);
        container.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestDialog();
    }
}

class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        Container container = this.getContentPane();
        container.setBackground(Color.BLUE);
        container.setLayout(null);
        JLabel label = new JLabel("这是一个弹窗");
        container.add(label);
        label.setVisible(true);
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
}

3.3、标签

标签label

new Label("标签");

图标icon

public class TestIcon extends JFrame implements Icon {

    private int width;
    private int height;

    public TestIcon(){
    }

    public TestIcon(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public void init(){
        TestIcon icon = new TestIcon(20,20);
        JLabel label = new JLabel("icontest",icon,SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);
        this.setBounds(100,100,400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
        g.setColor(Color.cyan);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }

    public static void main(String[] args) {
        new TestIcon().init();
    }
}

图片icon

public class Testicon2 extends JFrame {
    public Testicon2() {
        JLabel label = new JLabel("Imageicon");
        URL url = Testicon2.class.getResource("3.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        Container container = getContentPane();
        container.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        setBounds(100,100,500,300);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Testicon2();
    }
}

3.4、面板

JPanel

public class TestJPanel extends JFrame {

    public TestJPanel() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));
        JPanel jPanel = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(2,2));
        JPanel jPanel3 = new JPanel(new GridLayout(2,1));
        jPanel.add(new Button("1"));
        jPanel.add(new Button("1"));
        jPanel.add(new Button("1"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel2.add(new Button("2"));
        jPanel3.add(new Button("3"));
        jPanel3.add(new Button("3"));
        container.add(jPanel);
        container.add(jPanel2);
        container.add(jPanel3);
        this.setVisible(true);
        this.setSize(500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestJPanel();
    }
}

JScrollPanel

public class TestScroll extends JFrame {
    public TestScroll() {
        Container container = this.getContentPane();
        JTextArea textarea = new JTextArea(20, 50);
        textarea.setText("欢迎学习java");
        JScrollPane scroll = new JScrollPane(textarea);
        container.add(scroll);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestScroll();
    }
}

3.5、按钮

  • 图片按钮
public class TestButton1 extends JFrame {
    public TestButton1() {
        Container container = this.getContentPane();
        URL url = TestButton1.class.getResource("3.jpg");
        Icon icon = new ImageIcon(url);
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        container.add(button);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestButton1();
    }
}
  • 单选按钮
public class TestButton2 extends JFrame{
    public TestButton2() {
        Container container = this.getContentPane();
        URL url = TestButton1.class.getResource("3.jpg");
        Icon icon = new ImageIcon(url);
        JRadioButton radiobutton = new JRadioButton("JRadioButton");
        JRadioButton radiobutton2 = new JRadioButton("JRadioButton2");
        JRadioButton radiobutton3 = new JRadioButton("JRadioButton3");
        ButtonGroup group = new ButtonGroup();
        group.add(radiobutton);
        group.add(radiobutton2);
        group.add(radiobutton3);
        container.add(radiobutton,BorderLayout.CENTER);
        container.add(radiobutton2,BorderLayout.NORTH);
        container.add(radiobutton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestButton2();
    }
}
  • 复选按钮
public class TestButton3 extends JFrame {
    public TestButton3() {
        Container container = this.getContentPane();
        URL url = TestButton1.class.getResource("3.jpg");
        Icon icon = new ImageIcon(url);
        JCheckBox checkBox = new JCheckBox("JCheckBox");
        JCheckBox checkBox2 = new JCheckBox("JCheckBox2");
        container.add(checkBox,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100,100,400,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestButton3();
    }
}

3.6、列表

  • 下拉框
public class TestCombobox extends JFrame {
    public TestCombobox() {
        Container container = this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已下架");
        status.addItem("即将上映");
        container.add(status);

        this.setVisible(true);
        this.setBounds(100,100,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestCombobox();
    }
}
  • 列表框
public class TestCombobox2 extends JFrame {
    public TestCombobox2() {
        Container container = this.getContentPane();

        //String[] contents = {"1","2","3"};
        Vector contents = new Vector();
        JList list = new JList(contents);
        contents.add("张三");
        contents.add("李四");
        contents.add("王五");

        container.add(list);
        this.setVisible(true);
        this.setBounds(100,100,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestCombobox2();
    }
}

3.7、文本框

  • 文本框
public class TestText1 extends JFrame {
    public TestText1() {
        Container container = this.getContentPane();
        container.setLayout(null);
        JTextField textField = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);

        container.add(textField,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100,100,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestText1();
    }
}
  • 密码框
public class TestText2 extends JFrame {
    public TestText2() {
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        container.add(passwordField);

        this.setVisible(true);
        this.setBounds(100,100,500,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestText2();
    }
}
  • 文本域
JTextArea textarea = new JTextArea(20, 50);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值