Continue with my previous section and learn how to use another Java package. Swing is supplemented and improved on the basis of the original AWT.
Previous section: [GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)
Learning articles: Introduction to Java swing programming
3 Swing package
Swing is a GUI toolkit designed for Java.
Swing includes graphical user interface (GUI) devices such as text boxes, buttons, separate panes and tables.
Swing provides many better screen display elements than AWT. The disadvantage of lightweight components is the slow execution speed, and the advantage is that they can be used on all platforms.
Swing gui contains two elements: components and containers.
- Component (control): a component is a separate control element, such as a key or a text edit box. Components can only be displayed when they are placed in a container.
Inherits from the JComponent class. Common components include label JLabel, key JButton, input box JTextField, check box JCheckBox and list JList. - Container: a container is a special component that can contain components. Since containers are also components, containers can also be placed in other containers. Therefore, components and containers constitute a containment hierarchy.
There are two main types of containers in Swing.
(1) Heavyweight containers / top-level container s, which do not inherit from JComponent. They include JFrame, JApplet, JWindow and JDialog. Their biggest feature is that they cannot be contained by other containers, and can only be used as the top container of the interface program to contain other components.
(2) Lightweight containers / middle tier containers, which inherit from JComponent, including JPanel, JScrollPane, etc. The middle tier container is used to put several associated components together. Because the middle tier containers inherit from JComponent, they are also components, and they must be included in other containers.
3.1 JFrame window
- The JFrame window is different from the Frame of AWT package. It also needs to obtain a container to set the heap window color and so on.
- The default implementation window launches listening.
- method:
Container container = this.getContentPane();// Get a container
jLabel.setHorizontalAlignment(SwingConstants.CENTER);// Center label
package study.gui.swing; import javax.swing.*; import java.awt.*; public class JFrameText { public static void main(String[] args) { new MyJFrame().init(); } } //window class MyJFrame extends JFrame { public void init(){//A good programming habit defines an initialization method this.setBounds(100,100,400,300); this.setVisible(true); // this.setBackground(Color.black);// Cannot set color without adding container // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// The default implementation window launches listening JLabel jLabel = new JLabel("Welcome JFrame!"); this.add(jLabel); jLabel.setHorizontalAlignment(SwingConstants.CENTER);//Center label //Get a container Container container = this.getContentPane(); container.setBackground(Color.YELLOW);//Set container color } }
3.2 JDialog pop up window
- Use events to bind pop-up windows, for example, press a button to trigger an event.
- Pop ups require the aid of containers.
- There is no need to write pop-up.
package study.gui.swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JDialogText { public static void main(String[] args) { new MyJFrame2().init(); } } //window class MyJFrame2 extends JFrame{ public void init(){ this.setTitle("window"); this.setVisible(true); this.setBounds(10,10,400,300); //Set up containers to store things Container container = this.getContentPane(); container.setBackground(Color.PINK); //Absolute layout container.setLayout(null); //Create a pop-up button JButton jButton = new JButton("get into"); jButton.setBounds(30,30,100,50); container.add(jButton); //Binding monitor for button - pop up jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new MyDialogDemo(); } }); } } //Popup class MyDialogDemo extends JDialog{ public MyDialogDemo() { this.setTitle("Popup"); this.setVisible(true); this.setBounds(100,100,200,200); Container container = this.getContentPane(); container.setBackground(Color.GREEN); container.add(new JLabel("Helo")); } }
3.3 labels
3.3.1 JLabel label
JLabel jLabel = new JLabel("xxx"); jLabel.setHorizontalAlignment(SwingConstants.CENTER);//Center label
3.3.2 Icon
- Icon is an interface that needs to be implemented by JFrame.
- method:
(1) Implementation class iconDemo = new implementation class (width, height)// Icon
(2)JLabel label = new JLabel("point",iconDemo,SwingConstants.CENTER);// Put the icon on the label or on the button. Format: display text + icon name + position
package study.gui.swing; import javax.swing.*; import java.awt.*; public class IconText { public static void main(String[] args) { new MyIconFrame().init(); } } //window class MyIconFrame extends JFrame implements Icon{ int width; int height; //structure public MyIconFrame() throws HeadlessException { } public MyIconFrame(int width, int height) { this.width = width; this.height = height; } public void init(){ MyIconFrame iconDemo = new MyIconFrame(20,20);//Icon //Put the icon on the label or on the button. Format: display text + icon name + position JLabel label = new JLabel("point",iconDemo,SwingConstants.CENTER); Container container = this.getContentPane(); container.add(label); this.setVisible(true); this.setBounds(100,100,500,400); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } //Icon interface method that must be implemented @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.drawOval(x,y,width,height);//Draw a hollow circle } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.height; } }
3.3.3 picture labels
- You do not need to inherit the Icon interface
- method:
(1) ImageIcon imageIcon = new ImageIcon("picture address")// Construct picture labels
(2)label.setIcon(imageIcon);// Put the icon on the label
package study.gui.swing; import javax.swing.*; import java.awt.*; public class ImageText extends JFrame { public ImageText() { JLabel label = new JLabel("This is a picture label"); //Get the address of the picture ImageIcon imageIcon = new ImageIcon("G:\\Java\\JavaSE\\JavaText\\src\\study\\gui\\swing\\1.jpg"); label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container = this.getContentPane(); container.add(label); setVisible(true); setBounds(100,100,700,600); } public static void main(String[] args) { new ImageText(); } }
3.4 JPanel and JScrollPane panels
- Two panel forms:
(1) Normal panel: JPanel panel = new JPanel(new GridLayout(1,3));
(2) Scrollable panel: JScrollPane scrollPane = new JScrollPane(textArea);
package study.gui.swing; 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));//The following parameters represent the spacing between panels //panel 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); //Scrollable panel //Text field JTextArea textArea = new JTextArea(30,50);//Parameters represent rows and columns textArea.setText("If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success."); //Scroll panel JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JPanelDemo(); } }
3.6 buttons
- Three button forms:
(1) Normal button new JButton(): you can add pictures, etc.
(2) Radio button new JRadioButton();: You can only select one in a group.
(3) Check button new JCheckBox();: You can choose more than one in one box.
package study.gui.swing; import javax.swing.*; import java.awt.*; public class ButtonText extends JFrame { public ButtonText() { Container container = this.getContentPane(); container.setLayout(new GridLayout(1,3,10,10));//The following parameters represent the spacing between panels //Panel 1: display picture button JPanel panel1 = new JPanel(new GridLayout(1,1)); //Add picture to button Icon imageIcon = new ImageIcon("G:\\Java\\JavaSE\\JavaText\\src\\study\\gui\\swing\\1.jpg"); JButton button1 = new JButton(); button1.setIcon(imageIcon); button1.setToolTipText("Picture button");//Levitation prompt panel1.add(button1); container.add(panel1); //Panel 2: Show radio buttons JPanel panel2 = new JPanel(new GridLayout(3,1)); //Radio JRadioButton radioButton1 = new JRadioButton("GUCCI"); JRadioButton radioButton2 = new JRadioButton("LV"); JRadioButton radioButton3 = new JRadioButton("CHANEL"); //Only single selection can be made when radio boxes are grouped: after grouping, only one can be selected in a group ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton1); buttonGroup.add(radioButton2); buttonGroup.add(radioButton3); //add to panel2.add(radioButton1,BorderLayout.CENTER); panel2.add(radioButton2,BorderLayout.SOUTH); panel2.add(radioButton3,BorderLayout.NORTH); container.add(panel2); //Panel 3: Show check buttons JPanel panel3 = new JPanel(new GridLayout(3,1)); //Checkbox JCheckBox checkBox1 = new JCheckBox("Java"); JCheckBox checkBox2 = new JCheckBox("Python"); JCheckBox checkBox3 = new JCheckBox("C"); panel3.add(checkBox1,BorderLayout.CENTER); panel3.add(checkBox2,BorderLayout.NORTH); panel3.add(checkBox3,BorderLayout.SOUTH); container.add(panel3); this.setVisible(true); this.setTitle("Button"); this.setBounds(100,100,500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new ButtonText(); } }