栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

GUI编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUI编程

GUI编程

组件:

窗口

弹窗

面板

文本框

列表框

按钮

图片

监听事件

鼠标事件、键盘事件

破解工具

1、简介

GUI的核心技术:Swing AWT

1.界面不美观

2.需要jre环境

用处:

可以写一些自己的小工具

工作时候可能需要维护到swing界面,概率极小

了解MVC架构,了解监听

2、AWT

2.1 AWT介绍

    包含了很多类和接口:GUI

    元素,窗口,按钮,文本框

    java.awt

     

2.2组件和容器

2.3布局管理器

1.frame

public class Testframe {
    public static void main(String[] args) {
        //frame  JDK
        frame frame = new frame("第一个java图像界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(18, 175, 238));
        //弹出的初始位置
        frame.setLocation(20,20);
        //设置大小固定
        frame.setResizable(false);
    }
}

问题:弹窗关不掉;解决:直接停止运行

回顾封装

import java.awt.*;
//封装
//展示多个窗口 new
public class Testframe2 {
    public static void main(String[] args) {
        //new
        new Myframe(100,100,200,200,Color.blue);
        new Myframe(100,200,200,200,Color.yellow);
        new Myframe(200,100,200,200,Color.green);
        new Myframe(200,200,200,200,Color.white);
    }
}
class Myframe extends frame {
    static int id= 0;//定义一个计数器,可能需要多个窗口
    public Myframe(int x,int y,int w,int h,Color color){
        super("Myframe+"+(++id));//调用父类方法
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);//可见性
    }
}

    Panel

    //panel可以看成是一个空间,但是不能单独的的存在
    public class TestPanel {
        public static void main(String[] args) {
            frame frame = new frame();
            //布局的概念
            Panel panel = new Panel();
            //设置布局
            frame.setLayout(null);
            //坐标
            frame.setBounds(300,300,600,800);
            //背景
            frame.setBackground(new Color(252, 114, 114));
            //panel设置坐标,相对于frame
            panel.setBounds(50,50,400,400);
            panel.setBackground(new Color(52, 170, 201, 141));
            //frame.add()
            frame.add(panel);
            //需要设置可见性
            frame.setVisible(true);
          //  panel.setVisible(true);
            //监听事件:监听窗口关闭事件,System.exit(0)
            //适配器模式:
            frame.addWindowListener(new WindowAdapter() {
                //窗口点击关闭的时候需要做的事
                @Override
                public void windowClosing(WindowEvent e) {
                    //结束程序
                    System.exit(0);
                }
            });
        }
    }
    ​

解决了关闭问题

3.布局方式:

流式布局

//布局方式:流式布局
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.RIGHT));//居右
        //frame.setLayout(new FlowLayout());
        frame.setSize(200,300);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
​
        frame.setVisible(true);
        //监听事件:监听窗口关闭事件,System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

东西南北中布局

//东西南北中布局
public class TestBorderLayout {
    public static void main(String[] args) {
        frame frame = new frame();
​
        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()
        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 TestGridLayout {
    public static void main(String[] args) {
        frame frame = new frame("TestGridLayout");
​
        Button btn1= new Button("btn1");
        Button btn2= new Button("btn2");
        Button btn3= new Button("btn3");
        Button btn4= new Button("btn4");
​
        frame.setLayout(new GridLayout(2,2));
​
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
​
        frame.pack();//java函数 自适应
        frame.setVisible(true);
​
    }
}

2.4监听事件

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        frame frame = new frame();
        Button button = new Button();
        //因为addActionListener需要一个ActionListener,所以需要构造一个
        MyActionListen myActionListen = new MyActionListen();
        button.addActionListener(myActionListen );
​
        frame.add(button,BorderLayout.CENTER);
        frame.pack();//自适应
        frame.setVisible(true);//可见性;
        windowClose(frame);
    }
    //关闭窗体的事件
    public static void windowClose(frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//事件监听
class MyActionListen implements ActionListener{
​
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("hello hh");
    }
}

两个按钮实现同一个监听

**
 * 两个按钮  实现同一个监听
 */
public class TestActionTwo {
    public static void main(String[] args) {
        frame frame = new frame("开始-停止");
        Button button1 = new Button("START");
        Button button2 = new Button("STOP");
​
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
        //可以多个按钮只写一个监听类
        button1.setActionCommand("shen");
        button2.setActionCommand("shen");
​
​
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);
        windowClose(frame);
        frame.pack();
        frame.setVisible(true);
    }
}
//监听类
class MyMonitor implements ActionListener{
​
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点击了按钮:"+e.getActionCommand());
    }
}

2.5输入框TextField监听

public class TestText01 {
    public static void main(String[] args) {
        //主类只干一件事情,启动
        Myframe myframe = new Myframe();
        windowClose(myframe);//这里可以关闭窗口呀,啊哈哈,但是在Myframe中就不可以哦
    }
}
​
class Myframe extends frame {
    public Myframe(){//构造器
        TextField textField = new TextField();//文本框
        add(textField);
​
        //监听这个文本框输入的文字
        MyActionListener2 listener2 = new MyActionListener2();
        //按下enter, 就会触发这个输入框的事件
        textField.addActionListener(listener2);
​
        //设置替换编码
        textField.setEchoChar('*');
        pack();
        setVisible(true);
​
    }
}
class MyActionListener2 implements ActionListener{
​
    @Override
    public void actionPerformed(ActionEvent e) {
      TextField textField = (TextField)e.getSource();//获取一些资源  返回的一些对象
        System.out.println(textField.getText());//获得输入框的文本
        textField.setText("");//null""
    }
}

2.6简易计算器,组合+内部类回顾学习

组合

当前代码:

public class TestCalc {
    public static void main(String[] args) {
          new Calculator();
          windowClose(new Calculator());
    }
}
//计算器类
class Calculator extends frame {
    public Calculator() {
        //3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(10);
​
        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(num1,num2,num3));//实现等号
​
        //1个标签
        Label label = new Label("+");
​
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
​
        pack();
        setVisible(true);
​
    }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    private TextField num1,num2,num3;
​
    public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }
​
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //2.将这个值进行+运算后,放到第三个框
        num3.setText(""+(n1+n2));
        //3.将前两个框职位空
        num1.setText("");
        num2.setText("");
    }
}

改进之后:完全面向对象的写法

public class TestCalc {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.loadframe();
        windowClose(calculator);
    }
}
//计算器类
class Calculator extends frame {
        //属性
        TextField num1,num2,num3;
        //方法
        public void loadframe(){
            //3个文本框
            num1 = new TextField(10);
            num2 = new TextField(10);
            num3 = new TextField(10);
            Button button = new Button("=");
            Label label = new Label("+");
​
            button.addActionListener(new MyCalculatorListener(this));//实现等号
​
            //布局
            setLayout(new FlowLayout());
            add(num1);
            add(label);
            add(num2);
            add(button);
            add(num3);
​
            pack();
            setVisible(true);
​
        }
}
//监听器类
class MyCalculatorListener implements ActionListener{
    //获取三个变量
    Calculator calculator=null;//组合,在一个类中组合另一个类
​
    public MyCalculatorListener( Calculator calculator) {
        this.calculator=calculator;
    }
​
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        //2.将这个值进行+运算后,放到第三个框
        //3.将前两个框职位空
        int n1=Integer.parseInt(calculator.num1.getText());
        int n2=Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText(""+(n1+n2));
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

最终版本:改进为内部类

public class TestCalc {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.loadframe();
        windowClose(calculator);
    }
}
//计算器类
class Calculator extends frame {
        //属性
        TextField num1,num2,num3;
        //方法
        public void loadframe(){
            //3个文本框
            num1 = new TextField(10);
            num2 = new TextField(10);
            num3 = new TextField(10);
            Button button = new Button("=");
            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{
        //获取三个变量
        Calculator calculator=null;//组合,在一个类中组合另一个类
​
        public MyCalculatorListener() {
            this.calculator=calculator;
        }
​
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            //2.将这个值进行+运算后,放到第三个框
            //3.将前两个框职位空
            int n1=Integer.parseInt(num1.getText());
            int n2=Integer.parseInt(num2.getText());
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}

2.7画笔Paint

//画笔
public class TestPaint {
    public static void main(String[] args) {
        MyPaint myPaint = new MyPaint();
        myPaint.loadframe();
        windowClose(myPaint);
​
    }
}
class MyPaint extends frame{
    public void loadframe(){
        setBounds(200,200,600,500);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
       // super.paint(g);
        //画笔 需要有颜色  可以画画
        g.setColor(Color.blue);
        g.drawOval(100,100,100,100);
       //养成习惯,画笔用完要恢复到最初的颜色
    }
}

2.8鼠标监听事件

public class TestMouseListener {
    public static void main(String[] args) {
        Myframe2 myframe = new Myframe2("画图");
    }
}
//自己的类
class Myframe2 extends frame{
    //画画需要画笔,需要监听鼠标当当前的位置,需要集合来存储这个点
    ArrayList points;
​
    public Myframe2(String title){
        super(title);
        setBounds(200,200,400,400);
        //存鼠标点击的点
        points=new ArrayList<>();
​
        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.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 addPaint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter{
      //鼠标  按下  弹起  按住不放
​
        @Override
        public void mousePressed(MouseEvent e) {
            Myframe frame = (Myframe) e.getSource();
            //这里我们点击的时候就会在画面上产生一个点
            //这个点就是鼠标的点
            addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            frame.repaint();
        }
    }
}

2.9窗口监听

public class TestWindow {
    public static void main(String[] args) {
        new Windowframe();
    }
}
class Windowframe extends frame {
    public Windowframe(){
        setBackground(Color.blue);
        setBounds(200,200,200,200);
        setVisible(true);
        //第一种方法 内部类
        //addWindowListener(new MyWindowListener());
        //第二种方法:匿名内部类
        this.addWindowListener(new WindowAdapter(){
//                                   @Override
//                                   public void windowClosing(WindowEvent e) {
//                                       setVisible(false);//隐藏窗口,通过按钮隐藏当前窗口
//                                       System.exit(0);//正常退出
//                                   }
            //重写一些方法
            @Override//关闭
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
            }
            @Override//激活
            public void windowActivated(WindowEvent e) {
                System.out.println("windowActivated");
            }
        });
    }
//    //内部类
//    class MyWindowListener extends WindowAdapter {
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false);//隐藏窗口,通过按钮隐藏当前窗口
//            System.exit(0);//正常退出
//        }
//    }
}

2.10键盘监听

public class TestKey {
    public static void main(String[] args) {
       new Keyframe();
    }
}
class Keyframe extends frame{
    public Keyframe()  {
        setBounds(200,200,200,200);
        setVisible(true);
        //匿名内部类
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘下的键是哪一个,当前的码
                int keyCode = e.getKeyCode();//不需要去记当前这个值,直接用静态属性VK_XXX
                System.out.println(keyCode);
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("按下了上键");
                }
                //根据按下不同操作,产生不同结果
            }
        });
    }
}
3.swing

3.1窗口、标签

//窗口、标签
public class JframeDemo {
    //init();初始化
    public void init(){
        //顶级窗口
        Jframe jf = new Jframe("Jframe窗口");
        jf.setVisible(true);
        jf.setBounds(200,200,200,200);
        jf.setBackground(Color.blue);
        //设置文字JLable
        JLabel label = new JLabel("狂神小课堂");

        jf.add(label);

        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
         //建立一个窗口
        JframeDemo demo = new JframeDemo();
        demo.init();
    }

标签居中

public class JframeDemo02 {
    public static void main(String[] args) {
        new MyJframe().init();
    }
}
class MyJframe extends Jframe{
    public void init(){
        setVisible(true);
        setBounds(200,200,200,200);
        //设置文字JLable
        JLabel label = new JLabel("狂神小课堂");
        add(label);
​
        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.YELLOW);
    }
}

3.2弹窗

//主窗口
public class DialogDemo extends Jframe {
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
​
        //Jframe  放东西  容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
​
        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(500,300,300,620);
​
        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
               new MyDialogDemo();
            }
        });
        container.add(button);
    }
​
    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
​
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("java yyds"));
    }
}

3.3标签

label

new Label("xxx")

icon

图标Icon

//图标  ,需要实现类  frame继承
public class IconDemo extends Jframe implements Icon {
    private int width;
    private int height;
​
    public IconDemo(){
    }
    public IconDemo(int width, int height){
        this.width = width;
        this.height = height;
    }
​
​
    public void init(){//初始化
    IconDemo iconDemo=new IconDemo(15,15);
    //图标放在标签,也可以放在按钮上
        JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER);
​
        Container container=this.getContentPane();
        container.add(label);
​
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
       new IconDemo().init();
    }
​
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }
​
    @Override
    public int getIconWidth() {
        return this.width;
    }
​
    @Override
    public int getIconHeight() {
        return this.height;
    }
}

图片Icon

public class ImageIconDemo extends Jframe {
    public ImageIconDemo(){
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("IMG_1639.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();
    }
}

3.4面板

public class JPanelDemo extends Jframe {
    public JPanelDemo(){
        Container container = getContentPane();
        container.setLayout(new GridLayout(1,1,10,10));
​
        JPanel panel = new JPanel(new GridLayout(2,1));
​
        Component component = panel.add(new JButton("1"));
​
        container.add(component);
​
        setVisible(true);
        setBounds(100,100,100,100);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JPanelDemo();
    }
}
​

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,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JScrollDemo();
    }
}

3.5按钮

图片按钮

public class JButtonDemo01 extends Jframe {
    public JButtonDemo01(){
        Container container = getContentPane();
        //获取图片资源
        URL url = JButtonDemo01.class.getResource("2.png");
        Icon icon = new ImageIcon(url);
​
        //把图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
​
        //add
        container.add(button);
​
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

单选按钮

public class JButtonDemo02 extends Jframe{
    public JButtonDemo02(){
        Container container = getContentPane();
        //获取图片资源
        URL url = JButtonDemo01.class.getResource("2.png");
        Icon icon = new ImageIcon(url);
​
        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
​
        //把框放在按钮上,由于只能选择一个,所以要建一个组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);
​
        //add
        container.add(jRadioButton1,BorderLayout.SOUTH);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.CENTER);
​
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JButtonDemo02();
    }
​
}

复选按钮

public class JButtonDemo03 extends Jframe{
    public JButtonDemo03(){
        Container container = getContentPane();
        //获取图片资源
        URL url = JButtonDemo01.class.getResource("2.png");
        Icon icon = new ImageIcon(url);
​
        //多选框
        JCheckBox checkBox1 = new JCheckBox("JCheckBox1");
        JCheckBox checkBox2 = new JCheckBox("JCheckBox2");
        JCheckBox checkBox3 = new JCheckBox("JCheckBox3");
​
        container.add(checkBox1,BorderLayout.SOUTH);
        container.add(checkBox2,BorderLayout.NORTH);
        container.add(checkBox3,BorderLayout.CENTER);
​
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JButtonDemo03();
    }
}

3.6列表

下拉框

public class TestComboboxDemo extends Jframe {
    public TestComboboxDemo() {
        Container container = getContentPane();
​
        //code
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在上映");
        status.addItem("敬请期待");
        status.addItem("已下架");
​
        container.add(status);
​
​
        setVisible(true);
        setSize(300,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new TestComboboxDemo();
    }
}

列表框

public class TestComboboxDemo02 extends Jframe {
    public TestComboboxDemo02() {
        Container container = getContentPane();
​
        //code
        //生成列表的内容
        //String[] contents = {"1","2","3"};
        Vector contents = new Vector();
        //列表中需要放内容
        JList list = new JList(contents);
        contents.add("熊大");
        contents.add("熊二");
        contents.add("光头强");
​
        container.add(list);
​
​
        setVisible(true);
        setSize(300,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}

用途

1、选择地区,或者一个单个选项

2、列表,展示信息,一般是动态扩容

3.7文本框

文本框

public class TestTextDemo01 extends Jframe {
    public TestTextDemo01() {
        Container container = getContentPane();
​
        //code
        JTextField textField1 = new JTextField("熊大");
        JTextField textField2 = new JTextField("熊二");
​
        container.add(textField1,BorderLayout.CENTER);
        container.add(textField2,BorderLayout.NORTH);
​
        setVisible(true);
        setSize(500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

密码框

public class TestTextDemo02 extends Jframe {
    public TestTextDemo02() {
        Container container = getContentPane();
​
        //code
        JPasswordField field = new JPasswordField();
        field.setEchoChar('*');
        container.add(field);
​
        setVisible(true);
        setSize(500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new TestTextDemo02();
    }
}

文本域

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,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
​
    public static void main(String[] args) {
        new JScrollDemo();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786122.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号