当前位置:网站首页>GUI basic application
GUI basic application
2022-07-25 00:04:00 【Mr. Rabbit.】
1.GUI summary
at present , The programs we write are all console based programs
GUI(Graphical User Interface) Graphical user interface , It can make the application look more friendly
2.Swing Concept
be located javax.swing
Swing Is pure java Components , Make applications have the same appearance and behavior in different platforms
3. What is a component , Containers ?
java The basic composition of the graphical user interface is components , Component is a graphic display on the screen , Objects that can interact with users
Components cannot be displayed separately , So it must be placed in a certain container (container) in , To show that
Containers can add multiple components , You can call the container's add() Method , Add different components to the container
4 Classification of components :
window (Frame) And panel (Panel) Called container component
Text input box (JTextField), Button (Button) Called functional components
5. Programming steps of graphical user interface :
When we create a graphical user interface, we first need to create a window , Usually use JFrame stay Swing Create a window in the program
You can add panels in the window , Usually use Jpanel Create a panel in the created window , Multiple panels can be created in one window
Different components can be added to the panel , You can also set the layout , We usually use nested method to realize layout
Create a demo of the window , And common methods :
import javax.swing.*;
public class JFrameDemo1 extends JFrame {
public JFrameDemo1(){
this.setSize(500,300); // Set the size of the window
this.setLocationRelativeTo(null); // Set window center
this.setTitle(" Login screen "); // Set the window title
this.setIconImage(new ImageIcon(" Wechat pictures _20211117191028.jpg").getImage()); // Set window icons , You need to copy the picture directly under the project , Can't put it in the bag
}
public static void main(String[] args) {
JFrame jFrame = new JFrameDemo1(); // Use JFrame Create a window object
}
}
There are four layouts
The way :
FlowLayout
// Fluid layout , That is, a horizontal layout , Based on behavior , Arrange the components one by one , When a row is full , Automatically arrange to the next line
Flow layout example :
package javaGUI;
import javax.swing.;
import java.awt.;
public class JFrameDemo2_1 extends JFrame {
public JFrameDemo2_1(){
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setTitle(" Login screen ");
this.setIconImage(new ImageIcon(" Wechat pictures _20211117191028.jpg").getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel jp = new JPanel(); // Use JPanel Create a panel
jp.setLayout(new FlowLayout(FlowLayout.LEFT)); // Create an anonymous flow layout object
JButton jButton1 = new JButton(" Button 1"); // Use JButton Create a button object
JButton jButton2 = new JButton(" Button 2");
JButton jButton3 = new JButton(" Button 3");
JButton jButton4 = new JButton(" Button 4");
jp.add(jButton1);
jp.add(jButton2);
jp.add(jButton3);
jp.add(jButton4);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame jFrame = new JFrameDemo2_1();
}
}
BorderLayout // Boundary layout , A way of layout with southeast, northwest and middle
Example of boundary layout :
package javaGUI;
import javax.swing.;
import javax.swing.border.Border;
import java.awt.;
public class JFrameDemo2_2 extends JFrame {
public JFrameDemo2_2(){
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setTitle(" Login screen ");
this.setIconImage(new ImageIcon(" Wechat pictures _20211117191028.jpg").getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout()); // Use BorderLayout() Create an anonymous object with boundary layout
JButton jButton1 = new JButton(" Button 1");
JButton jButton2 = new JButton(" Button 2");
JButton jButton3 = new JButton(" Button 3");
JButton jButton4 = new JButton(" Button 4");
JButton jButton5 = new JButton(" Button 5");
jp.add(jButton1, BorderLayout.NORTH); // Set the position of each button separately
jp.add(jButton2, BorderLayout.EAST);
jp.add(jButton3, BorderLayout.SOUTH);
jp.add(jButton4, BorderLayout.WEST);
jp.add(jButton5, BorderLayout.CENTER);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame jFrame = new JFrameDemo2_2();
}
}
GridLayout // Grid layout , A way of layout in grid form
Grid layout example :
package javaGUI;
import javax.swing.;
import java.awt.;
public class JFrameDemo2_3 extends JFrame {
public JFrameDemo2_3(){
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setTitle(" Login screen ");
this.setIconImage(new ImageIcon(" Wechat pictures _20211117191028.jpg").getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
JPanel jp = new JPanel(new GridLayout(2,2)); // Use GridLayout(), How to create a grid layout , The number of rows and columns required is in brackets
JButton jButton1 = new JButton(" Button 1");
JButton jButton2 = new JButton(" Button 2");
JButton jButton3 = new JButton(" Button 3");
JButton jButton4 = new JButton(" Button 4");
jp.add(jButton1);
jp.add(jButton2);
jp.add(jButton3);
jp.add(jButton4);
this.add(jp);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame jFrame = new JFrameDemo2_3();
}
}
setLayout(null); // Custom location , That is, define the location of the component by yourself
Custom layout example :
package javaGUI;
import javax.swing.;
import java.awt.;
public class JFrameDemo2_4 extends JFrame {
public JFrameDemo2_4(){
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setTitle(" Login screen ");
this.setIconImage(new ImageIcon(" Wechat pictures _20211117191028.jpg").getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
// Custom layout , You need to use the original window for layout
Container container = this.getContentPane();
JButton jButton2 = new JButton(" Button 2");
// Two sides and two lines are the way to create the original window
jButton2.setLocation(100,50);
jButton2.setSize(80,50);
container.add(jButton2);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame jFrame = new JFrameDemo2_4();
}
}
Java GUI Introduction to common components
Be careful :
After all components are created and set, you must use add() Method to the window , Otherwise, it will not show
jp.add(jButton4); // Add the set button to the window
this.add(jp);
JLable( label ) // Used to display text or pictures
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel accountLabel = new JLabel(" account number ");
accountLabel.setFont(new Font(" Song style ",Font.BOLD,20));
// accountLabel.setIcon(new ImageIcon(" Wechat pictures _20211117191028.jpg"));
jp.add(accountLabel);
The renderings are as follows :
JtextField( The text box ) // Used to enter text
JTextField accountText = new JTextField(20);
jp.add(accountText);
The renderings are as follows :
JPasswordField( Password box ) // Used to enter the password
JLabel passwordLabel = new JLabel(“ password ”);
passwordLabel.setFont(new Font(“ Song style ”,Font.BOLD,20));
jp.add(passwordLabel);
JPasswordField jPasswordField = new JPasswordField(20);
jp.add(jPasswordField);
The renderings are as follows :
JTextArea( Multi-line text box ) / Used to input multiline text
JTextArea jTextArea = new JTextArea(5,20);
// Scroll panel , Scroll bars appear , Keep the size of the multiline text box unchanged
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jp.add(jScrollPane);
The renderings are as follows :
JMenuBar( menu bar ) // Used to create a menu bar
JMenuBar jMenuBar = new JMenuBar();
JMenu( menu ) // Used to create a menu
JMenu jMenu1 = new JMenu(“ file ”);
JMenu jMenu2 = new JMenu(“ edit ”);
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
JMenuItem( A menu item ) // Used to create menu items
JMenuItem jMenuItem = new JMenuItem(“ newly build ”);
jMenu1.add(jMenuItem);
Java GUI Event handling
So far, what we have written is only a graphical user interface , There is no special function , In order to realize certain functions, event processing must be carried out
When users and GUI Components interact , Events will occur , such as : Press a button , Click the mouse, etc
One . Event handling ideas :
A source ( Event source ) Produce an event ( Event object ) And send it to the monitor , The listener simply waits , Until it receives an event , Once the event is accepted , The listener will handle these events
An event source must register a listener so that the listener can receive notifications about a particular event
Two . Common event handling methods :
Add event listeners for buttons
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String account = jButton1.getText();
String password = new String(jPasswordField.getPassword());
System.out.println(account);
System.out.println(password);
}
});
Add event monitoring for the mouse
// Add mouse event
jButton1.addMouseListener(new MouseAdapter() { //Adapter For adapter you , Use this method , You can choose the event type that needs to be rewritten , Instead of rewriting all the methods
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(“ Mouse click ”);
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println(" The mouse click ");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println(" Mouse release ");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println(" Mouse migration ");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println(" Mouse removal ");
}
});
Add event monitoring for keyboard
// Add keyboard events
jTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println(“ Keyboard entry ”+e.getKeyCode());
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(" Press the keyboard ");
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println(" Keyboard release ");
}
});
Add event listening for the reset button
// Add event listening for the reset button
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextField.setText(“”);
jPasswordField.setText(“”);
}
})
边栏推荐
- HTB-Aragog
- 技术操作
- Weekly summary (*66): next five years
- Answers to some problems encountered in the process of Xperia XZ (f8332) brushing and root
- Horizontally centered element
- EF core :自引用的组织结构树
- Does opengauss support using Sqlalchemy connections?
- 2022 Henan Mengxin League game (2): Henan University of technology d-pair
- Use of serial queues
- Mandatory interview questions: 1. shallow copy and deep copy_ Shallow copy
猜你喜欢

Horizontally centered element

Notes of Teacher Li Hongyi's 2020 in-depth learning series 6

【无标题】

QT learning - using database singleton to complete login matching + registration function

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

指针与数组

Go basic notes_ 4_ map

Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge

Notes of Teacher Li Hongyi's 2020 in-depth learning series 7

Wechat applet development learning 5 (custom components)
随机推荐
Advanced function of postman
Are you still using system. Currenttimemillis()? Take a look at stopwatch
Let me introduce you to the partition automatic management of data warehouse
Fast development board for Godson solid state drive startup (burning system to solid state) - partition
Excel file processing tool class (based on easyexcel)
[untitled]
Does opengauss support using Sqlalchemy connections?
Architecture design of multi live shopping mall
每周小结(*66):下一个五年
MATLAB basic grammar (II)
剖析kubernetes集群内部DNS解析原理
2022 Henan Mengxin League game 2: Henan University of technology I - 22
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
LeetCode_ 6124_ The first letter that appears twice
Bug summary
来自大佬洗礼!2022 头条首发纯手打 MySQL 高级进阶笔记, 吃透 P7 有望
Live broadcast preview | online seminar on open source security governance models and tools
2. Load test
痛并快乐的-NIO编程
The new version of SSM video tutorial in shangsilicon valley was released