Java Swing UI writing graphical interface


Writing Graphical Interfaces with Swing UI


Example code:

package rzjm;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
public class 界面3 extends JFrame {
private JPanel contentPane;
private JFrame frame=new JFrame("graphical interface");//Voice control system based on mobile robot
    private JPanel imagePanel;
private ImageIcon background;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
interface3 frame = new interface3();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace ();
}
}
});
}
public interface 3() {
//background image#
background = new ImageIcon("C:\\Users\\lenovo\\Desktop\\5445c436621e19c3128b4eb7-1-hd_mh1517213885532.jpg");
        //Display the background image in a label#
JLabel label = new JLabel(background); //Set the size and position of the label so that the image just fills the entire panel #         label.setBounds(0, 0, background.getIconWidth(),


background.getIconHeight());
//Convert the content pane to JPanel, otherwise the method setOpaque() cannot be used to make the content pane transparent #         imagePanel = (JPanel) frame.getContentPane();

imagePanel.setOpaque(false);
        //The default layout manager of the content pane is BorderLayout#
imagePanel.setLayout(new BorderLayout(5,5));
JButton b1 = new JButton("Go forward");
b1.setFont(new Font("宋体", Font.PLAIN, 30));
b1.setForeground(Color.black);
b1.setBackground(Color.white);
frame.getContentPane().add(b1,BorderLayout.NORTH);
//Add button b1 to the back of the bottommost layout BorderLayout

JPanel p=new JPanel();
p.setLayout(new FlowLayout(1,30,30));
//Set new grid layout FlowLayout

JButton b2=new JButton("OK to send command");
b2.setFont(new Font("宋体", Font.PLAIN, 20));
b2.setForeground(Color.black);
b2.setBackground(Color.white);
//Set the font size and color of button b2
p.add(b2);
//Add b2 to the P layout

JButton b3=new JButton("Cancel this command");
b2.setFont(new Font("宋体", Font.PLAIN, 20));
b2.setForeground(Color.black);
b2.setBackground(Color.white);
p.add(b3);


frame.getContentPane().add(p,BorderLayout.SOUTH);
frame.setBounds(300, 200, 450, 300);
//Set the initial position and width of the window
frame.getLayeredPane (). setLayout (null);
//Add the background image to the bottom layer of the layered pane as the background#
frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// window close operation
frame.setSize(background.getIconWidth(), background.getIconHeight());
frame.setResizable(false);
//Set the window size to be immutable
frame.setVisible(true);

}
}


Run the screenshot:









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443847&siteId=291194637