Swing编程

一、 窗口、面板

点击查看代码
public class JFrameDemo {

    //init();初始化
    public void init(){
        //顶级窗口
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100,100,200,200);
        frame.setBackground(Color.GREEN);  //不会有颜色显示

        //容器实例化
        Container container = frame.getContentPane();
        container.setBackground(Color.GREEN);


        //设置文字
        JLabel label = new JLabel("欢迎来到JAVA的世界!");
        frame.add(label);

        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();

    }
}

二、弹窗

点击查看代码
//主窗口
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 btn1 = new JButton("点击弹出一个对话框");  //创建对象
        btn1.setBounds(30,30,200,50);

        //点击这个按钮的时候,弹出一个弹窗,需要监听事件
        btn1.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();

            }
        });

        container.add(btn1);

    }


    public static void main(String[] args) {

        new DialogDemo();
    }
}


//弹窗的窗口
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("JAVA基础学习!!!"));
    }
}

三、标签

labe: new Jlabel()
图标:icon

点击查看代码
public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("1.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);
    }

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

    }

}

四、面板

点击查看代码
public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));  //后面的参数的意思:间距


        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel panel1 = new JPanel(new GridLayout(1,2));
        JPanel panel2 = new JPanel(new GridLayout(2,1));
        JPanel panel3 = new JPanel(new GridLayout(3,2));

        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel1.add(new JButton("2"));
        panel1.add(new JButton("2"));
        panel2.add(new JButton("3"));
        panel2.add(new JButton("3"));
        panel3.add(new JButton("4"));
        panel3.add(new JButton("4"));
        panel3.add(new JButton("5"));
        panel3.add(new JButton("5"));
        panel3.add(new JButton("6"));
        panel3.add(new JButton("6"));

        container.add(panel);
        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

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

    }
}

JScrollPanel

点击查看代码
public class JScrollPanelDemo extends JFrame {
    public JScrollPanelDemo() {
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("欢迎学习java");

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

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

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

五、按钮、单选框、多选框

图片按钮

点击查看代码
public class JBurronDemo01 extends JFrame {

    public JBurronDemo01(){
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL resource = JBurronDemo01.class.getResource("1.png");
        Icon icon = new ImageIcon(resource);

        //把图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        //add
        container.add(button);

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


    }

    public static void main(String[] args) {

        new JBurronDemo01();
    }
}

单选框

点击查看代码
public class JButtonDemo02 extends JFrame {

    public JButtonDemo02(){
        Container container = this.getContentPane();

        URL resource = JButtonDemo02.class.getResource("1.png");
        Icon icon = new ImageIcon(resource);

        //单选框
        JRadioButton radioButton1 = new JRadioButton("这是一个单选框1");
        JRadioButton radioButton2 = new JRadioButton("这是一个单选框2");
        JRadioButton radioButton3 = new JRadioButton("这是一个单选框3");

        //由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1,BorderLayout.SOUTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.NORTH);
        this.setVisible(true);
        this.setBounds(200,200,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }


    public static void main(String[] args) {

        new JButtonDemo02();
    }


}

复选框:

点击查看代码
public class JButtonDemo03 extends JFrame {
    public JButtonDemo03()  {
        Container container = this.getContentPane();
        URL resource = JButtonDemo03.class.getResource("1.png");
        Icon icon = new ImageIcon(resource);

        //复选框
        JCheckBox checkBox01 = new JCheckBox("这是一个多选框");
        JCheckBox checkBox02 = new JCheckBox("这是一个多选框");

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


        this.setVisible(true);
        this.setBounds(200,300,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

六、列表

下拉框
应用场景:选择地区或者一些单个选项

点击查看代码
public class TestCombobox extends JFrame {
    public TestCombobox() {
        Container container = this.getContentPane();

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

        container.add(status);

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



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

    }
}

列表框
应用场景:展示信息,一般是动态扩容!

点击查看代码
public class TestCombobox02 extends JFrame {
    public TestCombobox02() {
        Container container = this.getContentPane();

        //生成列表的内容
//        String[] contents = {"1","2","3"};

        Vector contents = new Vector();
        //列表中需要放入内容
        JList jList = new JList(contents);
        contents.add("zhangsan");
        contents.add("lisi");
        contents.add("wangwu");

        container.add(jList);

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

    }

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

七、文本框、密码框、文本域

点击查看代码
public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container container = this.getContentPane();
//        container.setLayout(null);   //绝对布局

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


    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
点击查看代码
public class TestTextDemo02 extends JFrame {
    public TestTextDemo02() throws HeadlessException {

        Container container = this.getContentPane();

        //密码框

        //面板
        JPasswordField jPasswordField = new JPasswordField();  //***
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);


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


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

文本域:TextArea

posted on 2025-04-10 17:56  sharline  阅读(5)  评论(2)    收藏  举报