首页 > 编程语言 > 详细

Java进阶06GUI

时间:2021-09-23 23:54:47      阅读:35      评论:0      收藏:0      [点我收藏+]

Java进阶06GUI

概述

技术分享图片
技术分享图片

AWT

技术分享图片
技术分享图片

Frame

一个窗口

package com.mingmao.gui.amt;

import java.awt.*;

public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个java图形界面窗口");
        //设置可见性
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);

        //设置背景颜色
        //frame.setBackground(new Color(100,100,100));
        frame.setBackground(Color.ORANGE);

        //弹出的初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);
        
    }
}

技术分享图片

多个窗口

package com.mingmao.gui.amt;

import java.awt.*;

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.ORANGE);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.GREEN);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.MAGENTA);
    }
}

class MyFrame extends Frame{
    static int id=0;//窗口id

    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面板讲解

package com.mingmao.gui.amt;

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

//面板可以看成一个空间,但是不能单独存在,需要放在frame上
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);

        frame.setBounds(100,100,500,500);
        frame.setBackground(new Color(222, 215, 208));

        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(234, 201, 227));

        //frame添加面板
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,窗口关闭
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

技术分享图片

3种布局管理器

  1. 流式布局
  2. 东西南北中
  3. 表格布局
    流式布局:
package com.mingmao.gui.amt;

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件--按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置为流式布局
        //frame.setLayout(new FlowLayout());//默认是居中
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));

        frame.setBounds(100,100,500,500);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

技术分享图片

东南西北中

package com.mingmao.gui.amt;

import java.awt.*;

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.setBounds(100,100,500,500);

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

技术分享图片

表格布局

package com.mingmao.gui.amt;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");

        frame.setLayout(new GridLayout(3,2));

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

        frame.setBounds(100,100,500,500);
        //frame.pack();//自动优化布局
        frame.setVisible(true);
    }
}

技术分享图片

练习

技术分享图片

package com.mingmao.gui.amt;

import java.awt.*;

public class TestLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestLayout");
        frame.setBounds(100,100,500,300);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));

        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));

        panel1.add(new Button("b1-1"),BorderLayout.WEST);
        panel1.add(new Button("b1-2"),BorderLayout.EAST);
        panel2.add(new Button("z1-b1"));
        panel2.add(new Button("z1-b2"));
        panel1.add(panel2,BorderLayout.CENTER);

        panel3.add(new Button("b2-1"),BorderLayout.WEST);
        panel3.add(new Button("b2-2"),BorderLayout.EAST);
        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("z2-b"+(i+1)));
        }
        panel3.add(panel4,BorderLayout.CENTER);

        frame.add(panel1);
        frame.add(panel3);
    }
}

技术分享图片

技术分享图片

监听

事件监听

一个按钮

package com.mingmao.gui.monitor;

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 TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame("TestActionEvent");
        Button button1 = new Button("button1");

        frame.add(button1,BorderLayout.CENTER);
        frame.setBounds(100,100,300,300);
        frame.setVisible(true);
        windowClose(frame);

        MyActionListener myActionListener = new MyActionListener();
        button1.addActionListener(myActionListener);
    }

    //关闭窗体的方法
    private static void windowClose(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("这是监听器");
    }
}

技术分享图片
两个按钮

package com.mingmao.gui.monitor;

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 TestActionEvent2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button start = new Button("start");
        Button stop = new Button("stop");

        frame.setBounds(100,100,300,300);
        frame.setVisible(true);
        windowClose(frame);

        //如果不显示定义,就走按钮上的默认值
        //start.setActionCommand("start");
        //stop.setActionCommand("stop");

        frame.setLayout(new FlowLayout());
        frame.add(start);
        frame.add(stop);

        MyMonitor myMonitor = new MyMonitor();
        start.addActionListener(myMonitor);
        stop.addActionListener(myMonitor);
    }

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

class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("start")){
            System.out.println("开始");
        }else if(e.getActionCommand().equals("stop")){
            System.out.println("结束");
        }
    }
}

技术分享图片

输入框事件监听TextField

package com.mingmao.gui.monitor;

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 TestTextField {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame{
    public MyFrame()  {
        setBounds(100,100,500,700);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        TextField textField = new TextField();//一行,不能换行
        add(textField);
        textField.addActionListener(new MyActionListener1());

        //设置替换编码
        textField.setEchoChar(‘*‘);
    }
}

class MyActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField= (TextField) e.getSource();
        System.out.println(textField.getText());//获取输入的文本
        textField.setText("");//自动清空文本
    }
}

技术分享图片

简易计算器

技术分享图片

技术分享图片

package com.mingmao.gui.monitor;

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 MyCalc();
    }
}

class MyCalc extends Frame{
    public MyCalc() {
        setBounds(100,100,800,100);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Panel panel = new Panel();
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(30);
        Label label = new Label("+");
        Button button = new Button("=");

        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        MyCalcListener myCalcListener = new MyCalcListener(textField1,textField2,textField3);
        button.addActionListener(myCalcListener);
    }
}

class MyCalcListener implements ActionListener{
    private TextField textField1,textField2,textField3;

    public MyCalcListener(TextField textField1,TextField textField2,TextField textField3){
        this.textField1=textField1;
        this.textField2=textField2;
        this.textField3=textField3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int num1=Integer.parseInt(textField1.getText());
        int num2=Integer.parseInt(textField2.getText());
        int num3=num1+num2;

        textField3.setText(String.valueOf(num3));
        //textField3.setText(""+num3);

        textField1.setText("");
        textField2.setText("");
    }
}

技术分享图片
优化后代码

package com.mingmao.gui.monitor;

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 MyCalc().loadFrame();
    }
}

class MyCalc extends Frame{
    //属性
    TextField textField1,textField2,textField3;

    //方法
    public void loadFrame(){
        //构建所有组件加监听
        Panel panel = new Panel();
        textField1 = new TextField(10);
        textField2 = new TextField(10);
        textField3 = new TextField(30);
        Label label = new Label("+");
        Button button = new Button("=");
        button.addActionListener(new MyCalcListener());

        //布局
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        //设置及显示
        setBounds(100,100,800,100);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private class MyCalcListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            int num1=Integer.parseInt(textField1.getText());
            int num2=Integer.parseInt(textField2.getText());

            textField3.setText(String.valueOf(num1+num2));
            textField1.setText("");
            textField2.setText("");
        }
    }
}

画笔paint

package com.mingmao.gui.monitor;

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

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

class MyPaint extends Frame{

    public void loadFrame(){
        setBounds(100,100,800,500);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(100,100,100,200);//椭圆
        g.fillOval(300,300,50,50);//实心圆
        g.setColor(Color.green);
        g.fill3DRect(200,200,30,60,true);//矩形
    }
}

技术分享图片

鼠标监听

技术分享图片

package com.mingmao.gui.monitor;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouse {
    public static void main(String[] args) {
        new MyFrame1("画板");
    }
}

class MyFrame1 extends Frame{
    //需要画笔,需要监听鼠标,需要集合存出点
    ArrayList points;

    public MyFrame1(String title){
        super(title);
        setBounds(100,100,500,400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        points= new ArrayList();

        this.addMouseListener(new MyMouse());
    }

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

    @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);
        }
    }

    private class MyMouse extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame1 myFrame1 = (MyFrame1) e.getSource();
            myFrame1.addPaint(new Point(e.getX(), e.getY()));

            myFrame1.repaint();
        }
    }
}

技术分享图片

窗口监听

package com.mingmao.gui.monitor;

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

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

class MyFrame2 extends Frame{
    public MyFrame2(){
        setBounds(100,100,500,400);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("窗口正在关闭");
                System.exit(0);
            }

            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("窗口激活");
            }
        });
    }
}

键盘监听

package com.mingmao.gui.monitor;

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

public class TestKey {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(100,100,500,400);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {//一直按住会一直输出
                if(e.getKeyCode()==KeyEvent.VK_UP){
                    System.out.println("你按下了up键");
                }
            }
        });
    }
}

Swing

Swing之JFrame窗体

package com.mingmao.gui.swing;

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

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

class MyJframe extends JFrame {
    public void init(){
        Container contentPane = getContentPane();
        setTitle("一个Jframe窗口");
        setBounds(100,100,500,400);
        setVisible(true);
        contentPane.setBackground(Color.ORANGE);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JLabel label = new JLabel("欢迎");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        add(label);

    }
}

弹窗

package com.mingmao.gui.swing;

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

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

class DialogJframe extends JFrame{
    public void init(){
        Container contentPane = getContentPane();
        setTitle("一个Jframe窗口");
        setBounds(100,100,500,400);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //绝对布局
        contentPane.setLayout(null);

        JButton jbutton = new JButton("jbutton");
        jbutton.setBounds(200,200,80,30);
        contentPane.add(jbutton);

        jbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog().init();
            }
        });
    }

    private class MyDialog extends JDialog{
        public void init(){
            setVisible(true);
            setBounds(300,300,300,100);

            Container contentPane = getContentPane();
            JLabel jLabel=new JLabel("欢迎");
            jLabel.setHorizontalAlignment(SwingConstants.CENTER);
            contentPane.add(jLabel);
        }
    }
}

标签

package com.mingmao.gui.swing;

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

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

class IconJFrame extends JFrame implements Icon{
    private int width;
    private int height;

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

    public void init(){
        setBounds(100,100,300,300);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();

        IconJFrame iconFrame = new IconJFrame(15, 15);
        //图标可以放在标签上,也可以放在按钮上。
        JLabel jLabel = new JLabel("icon", iconFrame, SwingConstants.CENTER);
        contentPane.add(jLabel);
    }

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

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

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

图片标签

package com.mingmao.gui.swing;

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

public class TestImageIcon {
    public static void main(String[] args) {
        new ImageIconJframe(new JFrame()).init();
    }
}

class ImageIconJframe{
    JFrame jFrame=new JFrame();

    public ImageIconJframe(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init(){
        jFrame.setBounds(100,100,300,300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //把图片放在此类目录下
        URL url = ImageIconJframe.class.getResource("touxiang.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JLabel jLabel = new JLabel("imageIcon");
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(jLabel);
    }
}

面板

package com.mingmao.gui.swing;

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

public class TestJpanel {
    public static void main(String[] args) {
        new JpanelJframe(new JFrame()).init();
    }
}

class JpanelJframe{
    JFrame jFrame=new JFrame();

    public JpanelJframe(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init(){
        jFrame.setBounds(100,100,800,800);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //布局
        contentPane.setLayout(new GridLayout(2,1,10,10));//后面两个参数是上下间距
        JPanel jPanel1 = new JPanel(new GridLayout(1,3));
        JPanel jPanel2 = new JPanel(new GridLayout(2,1));
        JPanel jPanel3 = new JPanel(new GridLayout(1,2));
        JPanel jPanel4 = new JPanel(new GridLayout(2,3));
        for (int i = 0; i < 3; i++) {
            jPanel1.add(new JButton("jbutton1"));
        }
        for (int i = 0; i < 2; i++) {
            jPanel2.add(new JButton("jbutton2"));
        }
        for (int i = 0; i < 2; i++) {
            jPanel3.add(new JButton("jbutton3"));
        }
        for (int i = 0; i < 6; i++) {
            jPanel4.add(new JButton("jbutton4"));
        }
        contentPane.add(jPanel1);
        contentPane.add(jPanel2);
        contentPane.add(jPanel3);
        contentPane.add(jPanel4);
    }
}

技术分享图片

滚动条的实现

package com.mingmao.gui.swing;

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

public class TestJscroll {
    public static void main(String[] args) {
        new JscrollJframe(new JFrame()).init();
    }
}

class JscrollJframe{
    JFrame jFrame=new JFrame();

    public JscrollJframe(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 100);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //文本域-可换行
        JTextArea jTextArea = new JTextArea(10, 50);
        jTextArea.setText("欢迎");
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        contentPane.add(jScrollPane);

    }
}

图片按钮 单选框 多选框

图片按钮

package com.mingmao.gui.swing;

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

public class TestJbutton01 {
    public static void main(String[] args) {
        new JbuttonJframe(new JFrame()).init();
    }
}

class JbuttonJframe{
    JFrame jFrame=new JFrame();

    public JbuttonJframe(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        URL url = JbuttonJframe.class.getResource("touxiang.jpg");
        Icon icon = new ImageIcon(url);
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setToolTipText("图片按钮");//提示文字
        contentPane.add(jButton);
    }
}

单选框

package com.mingmao.gui.swing;

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

public class TestJbutton02 {
    public static void main(String[] args) {
        new JbuttonJframe2(new JFrame()).init();
    }
}

class JbuttonJframe2{
    JFrame jFrame=new JFrame();

    public JbuttonJframe2(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //单选框
        JRadioButton jradiobutton01 = new JRadioButton("jradiobutton01");
        JRadioButton jradiobutton02 = new JRadioButton("jradiobutton02");
        JRadioButton jradiobutton03 = new JRadioButton("jradiobutton03");

        //分组,只能选一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jradiobutton01);
        buttonGroup.add(jradiobutton02);
        buttonGroup.add(jradiobutton03);

        //布局
        contentPane.add(jradiobutton01,BorderLayout.NORTH);
        contentPane.add(jradiobutton02,BorderLayout.CENTER);
        contentPane.add(jradiobutton03,BorderLayout.SOUTH);
    }
}

多选框

package com.mingmao.gui.swing;

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

public class TestJbutton03 {
    public static void main(String[] args) {
        new JbuttonJframe3(new JFrame()).init();
    }
}

class JbuttonJframe3{
    JFrame jFrame=new JFrame();

    public JbuttonJframe3(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

       //多选框
        JCheckBox jcheckbox01 = new JCheckBox("jcheckbox01");
        JCheckBox jcheckbox02 = new JCheckBox("jcheckbox02");

        contentPane.add(jcheckbox01,BorderLayout.NORTH);
        contentPane.add(jcheckbox02,BorderLayout.CENTER);

    }
}

下拉框 列表框

下拉框

package com.mingmao.gui.swing;

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

public class TestComboBox01 {
    public static void main(String[] args) {
        new ComboBoxJframe(new JFrame()).init();
    }
}

class ComboBoxJframe {
    JFrame jFrame = new JFrame();

    public ComboBoxJframe(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("即将下架");
        jComboBox.addItem("已下架");

        contentPane.add(jComboBox);

    }
}

列表框

package com.mingmao.gui.swing;

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

public class TextComboBox02 {
    public static void main(String[] args) {
        new ComboBoxJframe2(new JFrame()).init();
    }
}

class ComboBoxJframe2 {
    JFrame jFrame = new JFrame();

    public ComboBoxJframe2(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //生成数组类型
        String[] contents={"1","2","3"};
        //放入数据
        JList jList = new JList(contents);
        contentPane.add(jList,BorderLayout.WEST);

        Vector vector = new Vector();
        JList jList1 = new JList(vector);
        vector.add("张三");
        vector.add("李四");
        vector.add("王五");
        contentPane.add(jList1,BorderLayout.EAST);
    }
}

文本框 密码框 文本域

文本框

package com.mingmao.gui.swing;

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

public class TestText01 {
    public static void main(String[] args) {
        new TextJframe01(new JFrame()).init();
    }
}

class TextJframe01 {
    JFrame jFrame = new JFrame();

    public TextJframe01(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //文本框
        JTextField text1 = new JTextField("text1");
        JTextField text2 = new JTextField("text2",20);
        contentPane.add(text1,BorderLayout.NORTH);
        contentPane.add(text2,BorderLayout.CENTER);
    }
}

密码框

package com.mingmao.gui.swing;

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

public class TestText02 {
    public static void main(String[] args) {
        new TextJframe02(new JFrame()).init();
    }
}

class TextJframe02 {
    JFrame jFrame = new JFrame();

    public TextJframe02(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public void init() {
        jFrame.setBounds(100, 100, 400, 300);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = jFrame.getContentPane();

        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar(‘*‘);
        contentPane.add(jPasswordField);
    }
}

文本域

Java进阶06GUI

原文:https://www.cnblogs.com/mingmao/p/15294622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!