GUI编程--3 Swing

GUI编程-3 Swing

3.1 JFrame 窗口

窗口:

package com.ssl.lesson04;

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

public class JframeDemo {

    //init():初始化
    public void init(){
        JFrame jf = new JFrame("这是一个JFrame窗口!");
        jf.setVisible(true);
        jf.setBounds(100,100,400,400);
        jf.setBackground(new Color(11, 81, 231));   //这个设置不了颜色
        //通过获取容器设置背景颜色
        Container contentPane = jf.getContentPane();
        contentPane.setBackground(Color.BLUE);

        //设置文字JLabel
        JLabel label = new JLabel("这是一个Jlabel的标签");

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

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


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

    }
}

3.2 弹窗

package com.ssl.lesson04;

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

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 button = new JButton("弹出一个对话框");

        button.setBounds(30,30,200,50);

        this.add(button);

        button.addActionListener(new ActionListener() {   //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialogDemo();
            }
        });



    }

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


//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        
        this.setVisible(true);
        this.setBounds(100,100,100,100);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //默认有关闭事件

        Container container = this.getContentPane();
        container.setLayout(null);
        
        //container.add(new Label("111"));   为啥不显示内容
    }
}

3.3 标签

label

new JLabel("xxx");

设置图标:ICON

图标

package com.ssl.lesson04;

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

//图标:需要实现类,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.setBounds(40,40,200,200);

        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,this.width,this.height);
        
    }

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

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


}

图片

package com.ssl.lesson04;

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

public class ImageIconDemo extends JFrame {

    public ImageIconDemo(){
        JLabel label = new JLabel("ImageIcon");
        //获取图片地址
        URL url = ImageIconDemo.class.getResource("T.png");

        ImageIcon imageIcon = new ImageIcon(url);

        label.setIcon(imageIcon);
        //标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = this.getContentPane();
        container.add(label);

        this.setBounds(40,40,200,200);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

}

3.4 面板

package com.ssl.lesson05;

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个参数,间距

        JPanel panel = new JPanel(new GridLayout(1,3));

        panel.add(new JButton("1"));
        panel.add(new JButton("2"));
        panel.add(new JButton("3"));

        //添加面板
        container.add(panel);
        this.setVisible(true);
        this.setBounds(40,40,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}
  • 滑动窗口JScroll
package com.ssl.lesson05;

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

public class JScrollDemo extends JFrame {

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

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

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

        container.add(jScrollPane);

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

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

3.5 按钮

  • 图片按钮
package com.ssl.lesson05;

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("T.png");
        ImageIcon imageIcon = new ImageIcon(resource);

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

        //加到容器上
        container.add(button);

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

    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}
  • 单选按钮
package com.ssl.lesson05;

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

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

        //单选按钮
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton03");

        //单选框一般只能选择一个,一般都要分组。一个组中只能选择一个。
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

        container.add(jRadioButton1,BorderLayout.CENTER);
        container.add(jRadioButton2,BorderLayout.NORTH);
        container.add(jRadioButton3,BorderLayout.SOUTH);


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

    }

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

  • 复选按钮
package com.ssl.lesson05;

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

public class JButtonDemo03 extends  JFrame{
    public JButtonDemo03(){
        Container container = this.getContentPane();

        //多选按钮
        JCheckBox jCheckBox01 = new JCheckBox("1");
        JCheckBox jCheckBox02 = new JCheckBox("2");
        JCheckBox jCheckBox03 = new JCheckBox("3");

        container.add(jCheckBox01,BorderLayout.SOUTH);
        container.add(jCheckBox02,BorderLayout.NORTH);
        container.add(jCheckBox03,BorderLayout.CENTER);


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

    }

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

3.6 列表

  • 下拉框
package com.ssl.lesson06;

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

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container container = this.getContentPane();
        //列表
        JComboBox status = new JComboBox();

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

        container.add(status);

        this.setVisible(true);
        this.setBounds(400,400,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}
  • 列表框
package com.ssl.lesson06;

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

public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02(){
        Container container = this.getContentPane();
        //生成列表内容
        String[] contents = {"1","2","3"};

        //列表中需要放入内容
        JList jList = new JList(contents);

        container.add(jList);

        this.setVisible(true);
        this.setBounds(400,400,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

应用场景:

  • 选择地区,或者一些单个选项。
  • 列表,展示信息,一般是动态扩容。

3.7 文本框

  • 文本框
package com.ssl.lesson06;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container container = this.getContentPane();

        //文本框
        JTextField jTextField = new JTextField("hello");
        container.add(jTextField);
        
        this.setVisible(true);
        this.setBounds(400,400,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}
  • 密码框
package com.ssl.lesson06;

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        Container container = this.getContentPane();

        //密码框
        JPasswordField jPasswordField = new JPasswordField();
        //可以设置
        jPasswordField.setEchoChar('*');
        container.add(jPasswordField);

        this.setVisible(true);
        this.setBounds(400,400,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
  • 文本域
package com.ssl.lesson06;

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

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03(){
        Container container = this.getContentPane();

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

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

        container.add(jScrollPane);

        this.setVisible(true);
        this.setBounds(400,400,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

前端页面HTML+监听器serverlet

C/S

B/S 主流

posted @ 2022-09-29 13:43  林每天都要努力  阅读(24)  评论(0)    收藏  举报