Java graphical interface --- basic components

Table of contents

1. Introduction of basic components

Two, Diaolg dialog box

(1)Dialog

(2) FileDialog


1. Introduction of basic components

Button Button
Canvas Canvas for drawing
Checkbox Checkbox component
CheckboxGroup Used to combine multiple Checkbox components into a group, only one of a group of Checkbox components can be selected, that is, all become radio button components
Choice drop-down selection box
Frame window, In the GUI program, use this class to create
the label label class of the window, which is used to place the list box component of the prompt text
, and can add multiple items.
The basic container class of Panel, which cannot exist alone, must be placed in other containers
. If the user needs to input a value in a certain range, the slider component can be used, such as the slider used to set the three values ​​​​of RGB in the palette. When creating a slider, you must specify its orientation, initial value, slider size, minimum and maximum values
​​ScrollPane Container component with horizontal and vertical scroll bars
TextArea Multi-line text field
TextField single line text box

The case is shown in the figure:

class Solution {
    //制造组件
    Frame s=new Frame("tittle");
    Box di=Box.createHorizontalBox();
    Box leftdi=Box.createHorizontalBox();
    Choice cho=new Choice();
    CheckboxGroup gr=new CheckboxGroup();
    Checkbox c1=new Checkbox("男",gr,true);
    Checkbox c2=new Checkbox("女",gr,false);
    Checkbox c3=new Checkbox("是否已婚?");
    Box lefttop=Box.createVerticalBox();
    Box top=Box.createHorizontalBox();

    public void init(){
        //先组装最底下的
        di.add(new TextField(30));
        di.add(new Button("确认"));
        s.add(di,BorderLayout.SOUTH);
        cho.add("红色");
        cho.add("蓝色");
        cho.add("白色");
        leftdi.add(cho);
        leftdi.add(c1);
        leftdi.add(c2);
        leftdi.add(c3);
        lefttop.add(new TextArea("你好",6,20,TextArea.SCROLLBARS_VERTICAL_ONLY));
        lefttop.add(leftdi);
        top.add(lefttop);
        top.add(new TextArea("hello",10,20,TextArea.SCROLLBARS_VERTICAL_ONLY));
        s.add(top);
        s.pack();
        s.setVisible(true);
    }

}






public class Test {
    public static void main(String[] args){
       Solution ss=new Solution();
       ss.init();

    }
}

Two, Diaolg dialog box

(1)Dialog

Dialog is a subclass of the Window class, a container class, and a special component. Dialog boxes are top-level windows that can exist independently, so the usage is almost the same as that of ordinary windows, but dialog boxes need to pay attention to the following two points:
(1) Dialog boxes usually depend on other windows, that is, they usually need to have a parent window
(2) 0 There are two types of dialog boxes: non-modal and modal. When a modal dialog box is opened, the modal dialog box is always located on top of its parent window. Before the modal dialog box is closed, the parent window cannot get the focus.

 Constructor
Dialog(Frame ower, String title boolean modal) Create a dialog object, where owner is the parent window of the current dialog, title is the title of the dialog, and modal is whether the current dialog is a modal dialog

 

public static void main(String[] arsg){
        Frame s=new Frame("title");
        //创建水平
        Dialog d1=new Dialog(s,"模式对话框",true);
        Dialog d2=new Dialog(s,"非模式对话框",false);
        Button b1=new Button("模式对话框");
        Button b2=new Button("非模式对话框");

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                 d1.setVisible(true);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d2.setVisible(true);
            }
        });
        s.add(b1,BorderLayout.NORTH);
        s.add(b2);
        s.pack();
        s.setVisible(true);
    }

(2) FileDialog

The Dialog class also has a subclass: FieldDialog, which represents a file dialog box for opening or saving files. It should be noted that FileDialog cannot specify modal or non-modal, because FileDialog depends on the implementation of the running platform. If If the file dialog box on the running platform is modal, then FileDialog is also modal, otherwise it is non-modal.


Method:
FileDialog(Frame parent, String title, int mode) Create a text dialog box
                                                                          parent specifies the parent window
                                                                          title: dialog
                                                                          title mode: file dialog type, if specified as FileDialog.LOAD, used to open the file, if specified as FileDialog .SAVE is used to save the file


String getDirectory() Get the absolute path of the opened or saved file
String getFile() Get the file name of the opened or saved file

 

The case is shown in the figure:

 

public static void main(String[] args) {
        Frame s=new Frame("title");
        Button b2=new Button("保存文件");
        Button b1=new Button("打开文件");
        FileDialog f1=new FileDialog(s,"打开文件",FileDialog.LOAD);
        FileDialog f2=new FileDialog(s,"保存文件",FileDialog.SAVE);
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f1.setVisible(true);
                System.out.println("打开的文件路径是"+f1.getDirectory()+",文件名="+f1.getFile());
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f2.setVisible(true);
                System.out.println("保存的文件路径="+f2.getDirectory()+",文件名="+f2.getFile());
            }
        });
        s.add(b1);
        s.add(b2,BorderLayout.SOUTH);
        s.pack();
        s.setVisible(true);
    }

 

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128599103