GUI知识点

GUI知识点,第1张

GUI知识点 GUI 一、AWT 1、frame
//声明一个窗体
        frame frame = new frame(s);
        //设置窗体可见
        frame.setVisible(true);
        //设置窗体大小
        frame.setSize(400,300);
        //设置窗体颜色
        Color c = new Color(114, 114, 114);
        frame.setBackground(c);
        //frame.setBackground(new Color(114, 114, 114));

        //设置d窗的d出的位置
        frame.setLocation(10,10);
        //设置窗口大小不可变,默认是可变的
        frame.setResizable(false);
public class Test {
    public static void main(String[] args) {
        Myframe frame1 = new Myframe(100,100,200,200,Color.black);
        Myframe frame2 = new Myframe(300,100,200,200,Color.BLUE);
        Myframe frame3 = new Myframe(100,300,200,200,Color.CYAN);
        Myframe frame4 = new Myframe(300,300,200,200,Color.darkGray);

    }
}

class Myframe extends frame{
    static int id =  0;

    public Myframe(int x, int y, int w, int h, Color c) {
        super("Java"+(++id));
        setBackground(c);
        setVisible(true);
        setBounds(x,y,w,h);
    }
}

2、panel

将panel加入frame中,对panel进行 *** 作,

在frame中使用panel要先设置frame布局为NULL

public class TestPanel {
    public static void main(String[] args) {
        Panel panel = new Panel();
        frame frame = new frame();
        //设置布局
        frame.setLayout(null);
        frame.setBounds(100,100,300,300);
        frame.setVisible(true);
        panel.setBounds(100,100,100,100);
        Color c = new Color(0x2E2EC1);
        panel.setBackground(c);
        frame.add(panel);
        //关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        //frame.add(panel);
    }
}
3、布局

流式布局

package com.wang.layout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFloat {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setBounds(100,100,300,300);
        //流式布局,一行不够到下一行,铺
        //居中
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setVisible(true);
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

一行放不下会转到下一行

东西南北中

        package com.wang.layout;

  import java.awt.*;

  public class TestBoarderLayout {
      public static void main(String[] args) {
      frame frame = new frame("BorderLayout");
      frame.setBounds(100,100,300,300);
      frame.setVisible(true);
      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);
  
  }

}

必须有个中间的那块,不然会留出来

表格布局

public class TestGridLayout {
public static void main(String[] args) {
frame frame = new frame();
frame.setBounds(100,100,300,300);
frame.setVisible(true);
//表格布局,3行2列
frame.setLayout(new GridLayout(3,2));
//大小自动填充
frame.pack();

Button button1 = new Button("btn1");
Button button2 = new Button("btn2");
Button button3 = new Button("btn3");
Button button4 = new Button("btn4");
Button button5 = new Button("btn5");
Button button6 = new Button("btn6");

frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

}

4、事件监听
package com.wang.listener;

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 TestActionListener {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setBounds(100,100,100,100);
        frame.setVisible(true);

        Button button = new Button();
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button);

        //关闭窗口的两种方法1、
//        frame.addWindowListener(new WindowAdapter() {
//            @Override
//            public void windowClosing(WindowEvent e) {
//                System.exit(1);
//            }
//        });
       // 2、
        windowClose(frame);
    }

    public static void windowClose(frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("wwwwwww");
    }
}

两个按钮一个监听事件

package com.wang.listener;

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 TestActionTwo {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setBounds(100,100,100,100);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        button1.setActionCommand("start");
        button2.setActionCommand("stop");
        MyActionListener1 myActionListener = new MyActionListener1();
        button1.addActionListener(myActionListener);
        button2.addActionListener(myActionListener);
        frame.add(button1);
        frame.add(button2);
        windowClose(frame);
    }
    public static void windowClose(frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}
//两个按钮一个监听事件
class MyActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }
}

5、文本框
package com.wang.listener;

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

public class TestText {
    public static void main(String[] args) {
        Myframe myframe = new Myframe();
        windowClose(myframe);

    }
    public static void windowClose(Myframe frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

class Myframe extends frame{
    public Myframe() throws HeadlessException {
        TextField textField = new TextField();
        add(textField);
        //监听文本框输入的字符
        MyActionListener3 myActionListener3 = new MyActionListener3();
        //按enter键会触发
        textField.addActionListener(myActionListener3);
        textField.setEchoChar('*');
        setVisible(true);
        pack();

    }
}
class MyActionListener3 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        //获得资源,返回一个对象
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText());
        textField.setText("");
    }
}

实现简易计算器
package com.wang.listener;

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 TestCalc {
    public static void main(String[] args) {
        new Calculator().loadframe();
    }
}
//计算器类
class Calculator extends frame{
    TextField num1,num2,num3;
    public void loadframe() {
        //文本框
        num1 = new TextField(10);//字符数,也是长度
        num2 = new TextField(10);
        num3 = new TextField(10);
        //一个按钮
        Button button = new Button("=");
        MyCalcultorListener myCalcultorListener = new MyCalcultorListener(this);
        button.addActionListener(myCalcultorListener);
        //一个标签
        Label label = new Label("+");
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        setVisible(true);
        pack();
        //关闭
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });

    }
}
//监听类
class MyCalcultorListener implements ActionListener{
    Calculator c = null;
    //传递参数
    public MyCalcultorListener(Calculator c) {
        this.c = c;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1、获得加数和被加数,并计算
        int x = Integer.parseInt(c.num1.getText());
        int y = Integer.parseInt(c.num2.getText());
        int z = x+y;
        //返回结果
        c.num3.setText(""+z);

    }
}

6、画笔
package com.wang.paint;

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

public class TestGraphy {
    public static void main(String[] args) {
        new MyPaint().loadframe();
    }
}

class MyPaint extends frame{
    public void loadframe(){
        setBounds(200,200,500,400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }


    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);  //画笔颜色
        g.drawOval(50,50,50,50); //空心圆
        g.fillOval(200,50,50,50);//实心圆
        //画笔用完,要恢复为原来的颜色
    }
}

7、鼠标事件
package com.wang.paint;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseListener {
    public static void main(String[] args) {
        new Myframe().loadframe();
    }
}

class Myframe extends frame{

    ArrayList points; //用于存放点
    public void loadframe(){
        setBounds(100,100,500,400);
        setVisible(true);
        points = new ArrayList();
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
        addMouseListener(new MyMouseListener());
    }

    //画笔将数组照中的点全部画出来
    @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 addPoint(Point point) {
        points.add(point);
    }
}
class MyMouseListener extends MouseAdapter{
    @Override
    public void mouseClicked(MouseEvent e) {
        //因为是当前的窗口,所以返回的对象为Myframe类型
        Myframe frame = (Myframe) e.getSource();
        frame.addPoint(new Point(e.getX(),e.getY()));
        //每次点都要重画
        frame.repaint();//刷新
    }
}

8、窗口监听事件

和窗口关闭的处理是一样的,只是具体实现的不同

9、键盘监听
class Keyframe extends frame{
    public Keyframe() throws HeadlessException {
        setBounds(100,100,400,300);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("上键");
                }
            }
        });
    }
}

SWING 1、Jframe
package com.wang.swing;

import javax.swing.*;

public class TestJframe {
    //初始化
    public void init(){
        //顶级窗口
        Jframe frame = new Jframe("Jframe窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,500,400);
        //设置文字
        JLabel label = new JLabel("JAVA");
        frame.add(label);

        //关闭窗口
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

2、JDialog
package com.wang.swing;

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

public class TestDialog extends Jframe {
    public void init(){
        setBounds(200,200,400,300);
        setVisible(true);
        Container container = getContentPane();
        JButton jButton = new JButton("按钮");
        jButton.setBounds(0,0,100,30);
        container.add(jButton);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestDialog().init();

    }
}

class MyDialog extends JDialog{
    public MyDialog(){
        //需要设置可见
        setVisible(true);
        setBounds(300,300,300,200);
        Container container = getContentPane();
        //设为绝对定位
        container.setLayout(null);
        //d窗不需要设置关闭等,因为,继承的类中已经实现了
        JLabel label = new JLabel("This is my Dialog!");
        //JLabel要设置大小,不然不会显示出来
        label.setBounds(0,0,300,200);
        container.add(label);
    }
}

layui可以优化d窗等

3、图片
package com.wang.swing;

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

public class TestIcon extends Jframe{
    public TestIcon() {
        //一个标签
        JLabel label = new JLabel("ImageIcon");
        //找到地址
        URL url = TestIcon.class.getResource("1.PNG");
        //把地址给一个图片类中
        ImageIcon imageIcon = new ImageIcon(url);
        //再将图片的类放到标签中
        label.setIcon(imageIcon);
        //居中布局
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

    }

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

加载的图片要放到out文件(src上面)中,不然容易出现空指针异常

4、JPanel

面板

package com.wang.swing;

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

public class TestJPanel extends Jframe {
    public TestJPanel() {
        Container container = this.getContentPane();
        //后面两个参数表示的是间隔
        container.setLayout(new GridLayout(2,2,10,10));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        JPanel panel2 = new JPanel(new GridLayout(1,2));
        JPanel panel3 = new JPanel(new GridLayout(1,2));
        JPanel panel4 = new JPanel(new GridLayout(1,2));

        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"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);
        setVisible(true);
        setBounds(100,00,400,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

5、滚轮
package com.wang.swing;

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

public class TestJSroll extends Jframe {
    public TestJSroll() throws HeadlessException {
        Container container = this.getContentPane();
        setBounds(100,100,200,100);
        setVisible(true);

        textarea textarea = new textarea("this is good");
        JScrollPane scrollPane = new JScrollPane(textarea);
        container.add(scrollPane);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


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

6、按钮上加图片
package com.wang.swing;

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

public class TestJButton extends Jframe {
    public TestJButton(){
        Container container = getContentPane();
        //把图片变为图标
        URL url =  TestJButton.class.getResource("1.PNG");
        Icon icon = new ImageIcon(url);
        //把这个图片放到按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        //提示文本
        button.setToolTipText("图片按钮");

        container.add(button);
        setVisible(true);
        setBounds(100,100,300,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

7、单选框,多选框
extends Jframe {
    public TestJButton2(){
        Container container = getContentPane();
        //把图片变为图标
        URL url =  TestJButton.class.getResource("1.PNG");
        Icon icon = new ImageIcon(url);
        //单选框
        JRadioButton radioButton1 = new JRadioButton("JR1");
        JRadioButton radioButton2 = new JRadioButton("JR1");
        JRadioButton radioButton3 = new JRadioButton("JR1");
        //将这些按钮加入一个组里,实现单选
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);

        container.add(radioButton1,BorderLayout.NORTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.SOUTH);

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

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

单选框注意要加上组,

多选框

package com.wang.swing;

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

public class TestJButton3 extends Jframe {
    public TestJButton3(){
        Container container = getContentPane();
        //把图片变为图标
        URL url =  TestJButton.class.getResource("1.PNG");
        Icon icon = new ImageIcon(url);
        //多选框
        JCheckBox checkBox1 = new JCheckBox("1");
        JCheckBox checkBox2 = new JCheckBox("2");
        JCheckBox checkBox3 = new JCheckBox("3");

        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);



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

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


8、列表

下拉列表

package com.wang.swing;

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

public class TestCombobox1 extends Jframe {
    public TestCombobox1(){
        Container container = getContentPane();
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("正在");
        comboBox.addItem("结束");
        //返回项数
        System.out.println(comboBox.getSelectedIndex());
        //返回内容
        System.out.println(comboBox.getSelectedItem());

        container.add(comboBox);

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

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

列表框

package com.wang.swing;

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

public class TestCombobox2 extends Jframe {
    public TestCombobox2(){
        Container container = getContentPane();

        //生成列表的内容
       // String[] contents = {"1","2","3"};
        Vector contents = new Vector();
        //列表中需要放入的内容
        JList list = new JList(contents);
        contents.add("1");
        contents.add("2");
        contents.add("3");
        container.add(list);

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

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

应用场景

下拉列表,选择地区,或者一些单个选项列表,展示信息,一般是动态扩容 9、文本框

文本框、密码框、文本域

package com.wang.swing;

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

public class TestText1 extends Jframe {
    public TestText1(){
        Container container = getContentPane();
        
        //密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        container.add(passwordField);

//
//        TextField textField1 = new TextField("hello");
//        TextField textField2 = new TextField("world",20);
//        container.add(textField1,BorderLayout.NORTH);
//        container.add(textField2,BorderLayout.CENTER);

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5713044.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存