Day17_GUI编程

简易计算器

package com.kuang.lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

//简易计算器
class Calculator extends Frame{
    //属性
    TextField num1,num2,num3;
    //方法
    public void loadFrame(){
        //3个文本框
        num1=new TextField (10);//字符数
        num2=new TextField (10);//字符数
        num3=new TextField (20);//字符数
        //1 个按钮
        Button button =new Button ("=");
        //1 个标签
        Label label = new Label ("+");
        button.addActionListener (new MyCalculatorListener());
        //布局
        setLayout (new FlowLayout ());
        add (num1);
        add (label);
        add (num2);
        add (button);
        add (num3);

        pack ();
        setVisible (true);

    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法!
    private class MyCalculatorListener implements ActionListener{


        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int n1 = Integer.parseInt (num1.getText ());
            int n2 = Integer.parseInt (num1.getText ());

            //2.将这个值+法运算后,放到第三个框
            num3.setText (""+(n1+n2));
            //3.清楚前两个框
            num1.setText ("");
            num2.setText ("");
        }
    }

}

画笔

package com.kuang.lesson03;

import java.awt.*;

public class TestMypaint {
    public static void main(String[] args) {
        new MyPaint ().loadFrame ();
    }
}
class MyPaint extends Frame{
    public void loadFrame(){
        setBounds (200,200,600,500);
        setVisible (true);
    }
    //画笔

    @Override
    public void paint(Graphics g) {
        //画笔,需要有颜色,画笔可以画画
        g.setColor (Color.red);
        g.drawOval (100,100,100,100);
        g.setColor (Color.yellow);
        g.fillRect (150,200,200,200);
        //养成习惯,画笔用完,将他还原到最初的颜色
    }
}

窗口监听事件

package com.kuang.lesson03;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame ();
    }
}

class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground (Color.BLUE);
        setBounds (100,100,200,200);
        setVisible (true);
        //addWindowListener(new MyWindowListener())
        this.addWindowListener (
                //匿名内部类
                new WindowAdapter () {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println ("你点击了X");
                    }
                }
        );
    }
}

窗口监听

package com.kuang.lesson03;


import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame ();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds (1,2,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 ("你按了上键");
                }
            }
        });
    }
}

面板

package com.kuang.lesson04;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
}
class MyJFrame2 extends JFrame{
    public void init(){
        this.setBounds (10,10,200,300);
        this.setVisible (true);

        JLabel label= new JLabel ("欢迎来到HHHH1");
        this.add (label);
        //让文本居中
        label.setHorizontalAlignment (SwingConstants.CENTER);

        //获得一个容器
        Container container=this.getContentPane ();
        container.setBackground (Color.YELLOW);
    }
}

弹窗

package com.kuang.lesson04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible (true);
        this.setSize (700,500);
        this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西,容器
        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) {

            }
        });
        container.add (button);
    }

    public static void main(String[] args) {

    }
}

//弹窗窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible (true);
        this.setBounds (100,100,500,500);
        //this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane ();
        container.setLayout (null);
        container.add(new Label ("弹窗打开了!"));

    }
}

标签

lable

new JLable("xxxx");

图标ICON

package com.kuang.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片地址
        JLabel label =new JLabel ("ImageIcon");
        URL url=ImageIconDemo.class.getResource ("chess.jpg");

        ImageIcon imageIcon = new ImageIcon (url);//命名不要冲突了
        label.setIcon (imageIcon);
        label.setHorizontalAlignment (SwingConstants.CENTER);

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

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

面板

JscrollPanel

package com.kuang.lesson04;

import javax.swing.*;
import java.awt.*;

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container=this.getContentPane ();
        //文本域
        JTextArea textArea =new JTextArea (20,50);
        textArea.setText ("欢迎学习java");

        //Scroll面板
        JScrollPane scrollPane =new JScrollPane (textArea);
        container.add (scrollPane);

        this.setVisible (true);
        this.setBounds (100,100,300,350);
        this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
    }

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

按钮

  • 图片按钮
package com.kuang.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01() {
        Container container=this.getContentPane ();
        //将图片变成一个图标
        URL resource = JButtonDemo01.class.getResource ("tx.jpg");
        Icon icon=new ImageIcon (resource);
        //把图片放在按钮上
        JButton button=new JButton ();
        button.setIcon (icon);
        button.setToolTipText ("图片按钮");
        //add
        container.add (button);
        this.setVisible (true);
        this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
        this.setSize (500,300);

    }
    public static void main(String[] args) {
        new JButtonDemo01 ();
    }
}
  • 单选按钮
package com.kuang.lesson05;

import com.kuang.lesson04.ImageIconDemo;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo02 extends JFrame {

    public JButtonDemo02(){
        Container container=this.getContentPane ();
        //将一个图片变为图标
        URL resource = JButtonDemo02.class.getResource ("tx.jpg");
        Icon icon=new ImageIcon(resource);
        //单选框
        JRadioButton radioButton1 =new JRadioButton ("JRadioButton1");
        JRadioButton radioButton2 =new JRadioButton ("JRadioButton2");
        JRadioButton radioButton3 =new JRadioButton ("JRadioButton3");


        //由于单选框只能选择一个,分组,一个组中只能选择一个·

        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.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
        this.setSize (500,300);
    }
    public static void main(String[] args) {
        new JButtonDemo02 ();
    }
}
  • 复选按钮
package com.kuang.lesson05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03(){
        Container container=this.getContentPane ();
        //将一个图片变为图标
        URL resource = JButtonDemo03.class.getResource ("tx.jpg");
        Icon icon=new ImageIcon (resource);

        //多选框
        JCheckBox checkBox01=new JCheckBox ("checkBox01");
        JCheckBox checkBox02=new JCheckBox ("checkBox02");

        container.add (checkBox01,BorderLayout.NORTH);
        container.add (checkBox02,BorderLayout.SOUTH);

        this.setVisible (true);
        this.setSize (500,500);
        this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
    }

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

}


下拉框

package com.kuang.lesson06;

import javax.swing.*;
import java.awt.*;

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container=this.getContentPane ();

        JComboBox status = new JComboBox<> ();

        status.addItem (null);
        status.addItem ("正在上映");
        status.addItem ("已下架");
        status.addItem ("即将上映");


        container.add (status);

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

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

列表


文本框

package com.kuang.lesson06;

import com.exception.demo01.Test;

import javax.swing.*;
import java.awt.*;

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container container=this.getContentPane ();

        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.setSize (500,500);
        this.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
    }

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

}
posted @ 2020-12-04 21:09  柠檬情  阅读(66)  评论(0编辑  收藏  举报