当前位置:网站首页>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(“”);
}
})
边栏推荐
- 线段树杂谈
- Pain and happiness -nio programming
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- Install K6 test tool
- LeetCode_392_判断子序列
- 多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
- Install software on kubernetes cluster using helm 3 package manager
- Are you still using system. Currenttimemillis()? Take a look at stopwatch
- [acwing周赛复盘] 第 61 场周赛20220723
- LeetCode_6124_第一个出现两次的字母
猜你喜欢

Paper time review MB2: build a behavior model for autonomous databases

技术操作

你还在使用System.currentTimeMillis()?来看看StopWatch吧

在混合云中管理数据库:八个关键注意事项

采坑记录:TypeError: 'module' object is not callable

EF core: self referencing organizational structure tree

Be an artistic test / development programmer and slowly change yourself

Js----- Chapter 4 array
![[英雄星球七月集训LeetCode解题日报] 第24日 线段树](/img/ae/1f3288a99cb07fcbb1836357e0229a.png)
[英雄星球七月集训LeetCode解题日报] 第24日 线段树

Exception, import package and file operation
随机推荐
dpkg : Breaks: libapt-pkg5.0 (< 1.7~b) but 1.6.15 is to be installedE: Broken packages
Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
Install K6 test tool
Code coverage
EF core :自引用的组织结构树
[英雄星球七月集训LeetCode解题日报] 第24日 线段树
做一个文艺的测试/开发程序员,慢慢改变自己......
[leetcode weekly replay] 303rd weekly 20220724
多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
Heap and stack in embedded development
采坑记录:TypeError: 'module' object is not callable
Processing of ffmpeg wasapi can't activate audio endpoint error
云图
With screen and nohup running, there is no need to worry about deep learning code anymore | exiting the terminal will not affect the operation of server program code
Vite3.0 has been released, can you still roll it (list of new features)
Salesforce zero foundation learning (116) workflow - & gt; On flow
LeetCode_392_判断子序列
QT learning - using database singleton to complete login matching + registration function
Docker container Django + MySQL service
Qt项目-安防监控系统(各个界面功能实现)