欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GUI05标签

程序员文章站 2022-06-19 15:07:00
JPanel面板package lesson03;import lesson02.JFrameDemo;import javax.swing.*;import java.awt.*;public class JPanelDemo extends JFrame { public JPanelDemo() { Container container = this.getContentPane(); container.setLayout(new Gri...

JPanel面板

package lesson03;

import lesson02.JFrameDemo;

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

public class JPanelDemo extends JFrame {


    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));//2行2列10间距

        JPanel panel = new JPanel(new GridLayout(1, 3));
        panel.add(new Button("1"));
        panel.add(new Button("1"));
        panel.add(new Button("1"));

        container.add(panel);

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

    }

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

    }

}

JScrollPanel文本域

package lesson03;

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

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

    public JScrollDemo(){
        Container container=this.getContentPane();
        //文本域
        JTextArea textArea=new JTextArea();
        textArea.setText("文本域的学习");
        //Scroll面板
        JScrollPane scrollPane=new JScrollPane(textArea);
        container.add(scrollPane);

        container.add(scrollPane);
        this.setVisible(true);
        this.setBounds(100,100,300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

按钮

package lesson03;
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("snow.jpg");
        Icon icon = new ImageIcon(resource);
        //图片大小是否可以设置呢?
        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();

    }
}

JButton单选框

package lesson03;

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

public class JButtonDemo02 extends JFrame {

    public JButtonDemo02() {
        Container container = this.getContentPane();
        //将一个图片变为图标
//        URL resource = JButtonDemo01.class.getResource("snow.jpg");
//        Icon icon = new ImageIcon(resource);
        //图片大小是否可以设置呢?
        //单选框
        JRadioButton radioButton1= new JRadioButton("01");
        JRadioButton radioButton2 = new JRadioButton("02");
        JRadioButton radioButton3= new JRadioButton("03");
        //分组一个组中只能选择一个
        ButtonGroup group=new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        //add
        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();

    }
}

JButton复选框

package lesson03;

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

public class JButtonDemo03 extends JFrame {

    public JButtonDemo03() {
        Container container = this.getContentPane();
        //将一个图片变为图标
//        URL resource = JButtonDemo01.class.getResource("snow.jpg");
//        Icon icon = new ImageIcon(resource);
        //图片大小是否可以设置呢?
        //复选框
        JCheckBox checkBox1 = new JCheckBox();
        JCheckBox checkBox2= new JCheckBox();
        JCheckBox checkBox3= new JCheckBox();
        //add
        container.add(checkBox1,BorderLayout.NORTH);
        container.add(checkBox2,BorderLayout.CENTER);
        container.add(checkBox3,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setSize(50,30);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo03();

    }
}

JComboBox下拉框

package lesson03;

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

public class TestComboxDemo01 extends JFrame {
    public TestComboxDemo01() {
        //下拉框
        Container container=this.getContentPane();
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("start");
        status.addItem("stop");
        status.addItem("restart");
        container.add(status);

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

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

    }
}

列表框JList

package lesson03;

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

public class TestComboxDemo02 extends JFrame {
    public TestComboxDemo02() {
        //列表框
        Container container=this.getContentPane();
        //生成列表内容 稀疏数组压缩静态写死
        String[]  contents={"1","2","3"};
        //动态
        Vector vector = new Vector();
        //JList jList = new JList(contents);
        JList jList = new JList(vector);

        vector.add("one");
        vector.add("two");
        vector.add("three");
        container.add(jList); 
        this.setVisible(true);
        this.setSize(500,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

    }
}
  • 应用场景
    • 选择地区,单个选项
    • 列表展示信息,动态扩容

文本框

package lesson03;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01() {
        Container container=this.getContentPane();
        //文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);
        container.add(textField1,BorderLayout.SOUTH);
        container.add(textField2,BorderLayout.CENTER);

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

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

    }
}

密码框

package lesson03;

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() {
        Container container=this.getContentPane();
        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');
        container.add(jPasswordField);
        this.setVisible(true);
        this.setSize(500,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

    }
}

文本域

 JTextArea textArea = new JTextArea();
        textArea.setText("文本域");
				container.add(textArea);

本文地址:https://blog.csdn.net/zs_pnzzz/article/details/112254328

相关标签: Java gui