Java Swing interface study notes

Table of contents

1. Inheritance relationship between Java Swing components

2. JFrame

3. JDialog

4. Commonly used panels

5. Commonly used components

1. JLabel label component

2. JButton button component

3. Menu bar

4. Text component

5. Password box component

6. Text field component

6. Commonly used layouts

1. FlowLayout

2. Border LayoutBorderLayout

3. Grid layout

7. Commonly used event listeners

8. JTable


1. Inheritance relationship between Java Swing components

2. JFrame

JFrame is a container, which is the carrier of each component.

1. Create a new JFrame object

JFrame jframe = new JFrame();  //创建没有标题的窗口
JFrame jframe = new JFrame(String s);  //创建标题为 s 的窗口

    //JFrame类的setTitle(String s)方法也可设置标题

2. Set the size of the JFrame form

frame.setLocationRelativeTo(null); can set the window to be centered
//通过JFrame的对象名调用以下方法实现对应功能

public void setSize(int width,int height){}  //设置窗口的大小(单位为px)
public void setLocation(int x,int y){}  //设置窗口的位置,默认位置是(0,0)
public void setBounds(int a,int b,int width,int height){}  //设置窗口的初始位置是(a,b),窗口的宽是width,高是height
public void setVisible(boolean b){}  //设置窗口是否可见,默认是false不可见的
public void setResizable(boolean b){}  //设置窗口是否可被用户调整大小,默认是ture可调整
public void dispose(){}  //撤销当前窗口,并释放当前窗口的所使用的资源

public void setExtentedState(int state){}  //设置窗口的扩展状态
    /**
    *  其中state参数取JFrame类中下列类常量
    *  MAXIMIZED_HORIZ
    *  MAXIMIZED_VERT
    *  MAXIMIZED_BOTH
    */

3. Set the closing method of JFrame

public void setDefaultCloseOperation(int operation)  //该方法用来设置单击窗体右上角的关闭图标后,程序会作出怎样的处理,其中operation参数取JFrame类中的下列int型static常量,程序会根据参数operation的取值作出不同的处理

    /**
    *  DO_NOTHING_ON_CLOSE(什么也不做)
    *  HIDE_ON_CLOSE(隐藏当前窗口)
    *  DISPOSE_ON_CLOSE(隐藏当前窗口,并释放窗口所占有的其他资源)
    *  EXIT_ON_CLOSE(结束窗口所在的应用程序)
    /

3. JDialog

        Inherited from java.awt.Dialog class. It is another form that pops up from one form. It is similar to JFrame and requires

Call getContentPane to convert the form into a container, and then set the form's content in the container.

        JDialog: Can be used as a JFrame, but must be subordinate to JFrame. The methods in the JFrame class are also in JDialog

Yes, the only difference is that JDialog does not have EXIT_ON_CLOSE in the closing method. At the same time, because JDialog is subordinate to JFrame,

So when the JFrame window is closed, JDialog will also be closed, and vice versa will not be affected.

Constructor

JDialog jdialog = new JDialog();
JDialog jdialog = new JDialog(Frame f);  //指定父窗口
JDialog jdialog = new JDialog(Frame f,String title);  //指定父窗口+标题

4. Commonly used panels

        The panel is also a swing container. It can be used as a container to add or accommodate other components, but it must also be in a

Inside the container.

1、JPanel

        JPanel is the simplest panel, which inherits from the java.awt.Container class

JFrame jframe = new JFrame("标题");
jframe.setBounds(400,300,1200,1200);
jframe.setVisible(true);
JButton jb = new JButton("按钮");  //new两个按钮组件
JButton jb1 = new JButton("按钮1");
JPanel jp = new JPanel(new FlowLayout());  //生成一个面板

jp.add(jb);  //将两个按钮加入到面板中
jp.add(jb1);
jframe.add(jp);  //将面板加入到另外的容器中

2、JScrollPane

        JScrollPane is a panel with scroll bars. Therefore, use this panel when you need to put a large component into a smaller space. Only one component can be added to a JScrollPane. Therefore, if you need to add multiple components to a JScrollPane, you need to add the components to be added to the JPanel first, and then add the JPanel to the JScrollPane.

JFrame jframe = new JFrame("标题");
jframe.setBounds(400,300,1200,1200);
jframe.setVisible(true);
JButton jb = new JButton("按钮");
jb.setSize(800,200);  //设置按钮组件的尺寸
JScrollPane jp = new JScrollPane(jb);  //把按钮组件放在JScrollPane中
jframe.add(jp);

5. Commonly used components

1. JLabel label component

        Display text or prompt information

Construction method

JLabel jlabel = new JLabel();
JLabel jlabel = new JLabel(String str);  //设置文本
JLabel jlabel = new JLabel(Icon icon);  //设置图标
JLabel jlabel = new JLabel(String str,int aligment);  //设置文本+水平对齐方式(LEFT、CENTER、RIGHT)
JLabel jlabel = new JLabel(String str,Icon icon,int aligment);  //设置文本+图标+水平对齐方式
JFrame jf = new JFrame("标题");
jf.setBounds(400,300,1000,1000);
JLabel jl = new JLabel();
jl.setText("账号:");
jl.setFont(new Font("宋体",Font.PLAIN,30));  //设置标签的字体,尺寸等
jf.add(jl);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

2. JButton button component

        Construction method

JButton jb = new JButton();
JButton jb = new JButton(String text);  //指定文字
JButton jb = new JButton(Icon icon);  //指定图标
JButton jb = new JButton(String text,Icon icon);  //指定文字+图标

        Other methods

setEnabled(boolean n);  //设置按钮是否可用
setBorderPainted(boolean n);  //设置边界是否显示,默认是显示的

       

        (1) Radio button component

               JRadioButton is a radio button, and the radio button needs to be added to the ButtonGroup button group.

                

                Construction method

JRadioButton jrb = new JRadioButton();  
JRadioButton jrb = new JRadioButton(String text);  //指定文字
JRadioButton jrb = new JRadioButton(String text,boolean selected);  //指定文字+是否选中

                Example

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);
JRadioButton jrb = new JRadioButton("男");
JRadioButton jrb1 = new JRadioButton("女");
ButtonGroup bg = new ButtonGroup();
bg.add(jrb);
bg.add(jrb1);
jf.add(jrb);
jf.add(jrb1);

        

        (2) Check component box

                JCheckBox

                

                Construction method

JCheckBox jb = new JCheckBox();
JCheckBox jb = new JCheckBox(String text,boolean checked);  //指定文字+是否被选中

                Example

JFrame jf = new JFrame("标题");
jf.setBounds(400,300,1000,1000);
JCheckBox jcb1 = new JCheckBox("篮球");
JCheckBox jcb1 = new JCheckBox("游戏");
JCheckBox jcb1 = new JCheckBox("睡觉");
JCheckBox jcb1 = new JCheckBox("学习");
jf.add(jcb1);
jf.add(jcb2);
jf.add(jcb3);
jf.add(jcb4);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        (3) Drop-down list box component

                

                Construction method

JComboBox jb = new JComboBox();

//addItem添加下拉内容

                Example

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JComboBox box = new JComboBox();
box.addItem("--请选择你的学历--");
box.addItem("高中");
box.addItem("大学");
box.addItem("研究生");
jf.add(box);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

3. Menu bar

        (1) Create menu bar: JMenuBar

        (2) Create menu: JMenu

        (3) Create menu item: JMenuItem

//菜单项依附于菜单,菜单依附于菜单栏

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("菜单1");
JMenuItem item1 = new JMenuItem("菜单项1");
JMenuItem item2 = new JMenuItem("菜单项2");
JMenuItem item3 = new JMenuItem("菜单项3");
menu.add(item1);
menu.add(item2);
menu.add(item3);
bar.add(menu);
jf.add(bar);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

4. Text component

Constructor

JTextField text = new JTextField();
JTextField text = new JTextField(String text);  //指定默认文字
JTextField text = new JTextField(int fieldWidth);  //指定文本框长度
JTextField text = new JTextField(String text,int fieldWidth);  //指定默认文字+文本框长度

Example

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JLabel label = new JLabel("账号");
JTextField text = new JTextField("请输入",20);
jf.add(lable);
jf.add(text);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

5. Password box component

Constructor

JPasswordField pwd = new JPasswordField();
JPasswordField pwd = new JPasswordField(String str);  //指定默认文字
JPasswordField pwd = new JPasswordField(int fiedWidth);  //指定文本框长度
JPasswordField pwd = new JPasswordField(String str,int fiedWidth);  //指定默认文字+文本框长度

Example

//setEchoChar('*')  显示回显字符,默认·

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JLabel label = new JLabel("密码:");
JPasswordField text = new JPasswordField("请输入密码",20);
jf.add(lable);
jf.add(text);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

6. Text field component

Constructor

JTextArea area = new JTextArea();
JTextArea area = new JTextArea(String text);  //指定默认文字
JTextArea area = new JTextArea(int rows,int cols);  //指定行+列

Example

//setLineWrap(boolean x);  设置文本域是否自动换行

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JTextArea area = new JTextArea(12,20);
jf.add(area);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

6. Commonly used layouts

1. FlowLayout

        All components are discharged one by one like a stream. After a row is filled, the next row is queued. By default, each component is

It is arranged in the center, but it can also be set.

Constructor

FlowLayout fl = new FlowLayout();
FlowLayout fl = new FlowLayout(int aligment);  //设置对齐方式(默认FlowLayout.CENTER居中)

/**
*  aligment取值:
*  FlowLayout.LEFT = 0
*  FlowLayout.CENTER = 1
*  FlowLayout.RIGHT = 2
*/

Example

//一般都通过setLayout方法来设置布局,例如jf.setLayout(new FlowLayout(FlowLayout.LEFT));

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JButton jb = new JButton("A");
JButton jb1 = new JButton("B");
jf.add(jb);
jf.add(jb1);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

2. Border LayoutBorderLayout

        Border layout is the default layout management method. Border layout divides the container into North (BorderLayout.NORTH) and South

(BorderLayout.SORTH), East (BorderLayout.EAST), West (BorderLayout.WAST), Middle

(BorderLayout.CENTER) 5 areas.

Example

//再给JFrame容器add组件的时候,指定边界

JFrame jf = new JFrame("标题");
jf.setLayout(new BorderLayout());
jf.setBounds(400,300,1000,1000);

JButton jb = new JButton("中部");
JButton jb1 = new JButton("西部");
jf.add(jb,BorderLayout.CENTER);
jf.add(jb1,BorderLayout.WEST);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

3. Grid layout

        Grid layout divides containers into grids. All components are arranged in rows and columns, and the number of grids is determined by the number of rows and columns.

Certainly. Each component will fill in the spaces, changing the size of the container, and the size of the component will change accordingly.

Construction method

GridLayout gl = new GridLayout(int rows,int columns);  //指定行数+列数
GridLayout gl = new GridLayout(int rows,int columns,int horizGap,int vertGap)  //指定行数+列数+水平间隔+垂直间隔

Example

JFrame jf = new JFrame("标题");
jf.setLayout(new GridLayout(2,2));
jf.setBounds(400,300,1000,1000);

JButton jb = new JButton("A");
JButton jb1 = new JButton("B");
JButton jb2 = new JButton("C");
JButton jb3 = new JButton("D");
jf.add(jb);
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

7. Commonly used event listeners

event Listening interface method
action event listener ActionEvent ActionListener

addActionListener();

removeActionListener();

focus event listener FocusEvent FocusListener

addFocusListener();

removeFocusListener();

        Steps for usage

(1) Create a new component (such as Button)

(2) Add the component to the corresponding container (such as JFrame)

(3) Register a listener to listen for events generated by the event source (such as using ActionListener to respond to the user clicking a button)

(4) Define the method to handle the event (such as defining the corresponding method in actionPerformed in ActionListener)

        Example

JFrame jf = new JFrame("标题");
jf.setLayout(new FlowLayout());
jf.setBounds(400,300,1000,1000);

JButton jb = new JButton("A");
    jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
               此处重写方法,要让此按钮做什么
            }
        });

jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

8. JTable

        JTable is a table component and needs to be regarded as two parts, the title of the table and the content of the table, but the actual operation of these two parts

is the DefaultTableModel object, and after the operation is completed, the JTable object needs to be put into the JPanel or JScrollPane panel, etc.

container, so their hierarchical relationship is:

        JScrollPane

                JTable

                        DefaultTableModel

Guess you like

Origin blog.csdn.net/weixin_64709241/article/details/125098387