当前位置:网站首页>AWT common components, FileDialog file selection box
AWT common components, FileDialog file selection box
2022-07-04 05:53:00 【Gentle ~】
Recommended reading :Java Detailed explanation of graphical interface (AWT、Swing) special column
List of articles
Common components
Component name | function |
---|---|
Button | Button |
Canvas | A canvas for drawing |
Checkbox | Check box components ( It can also be used as a radio box component ) |
CheckboxGroup | Used to transfer multiple Checkbox Components are assembled into a group , A group of Checkbox There will be only one component that can To be selected , That is to say, they all become radio box components |
Choice | Drop down the selection box |
Frame | window , stay GUI Program through this class to create a window |
Label | The tag class , Used to place suggestive text |
List | JU Frame components , You can add multiple entries |
Panel | Basic container classes cannot exist alone , It has to be put in another container |
Scrollbar | Slide bar component . If the user is required to enter a value in a range , You can use the slider component , For example, tune Set... In the palette RGB The slider used for the three values of . When you create a slider , You have to specify its direction 、 Initial value 、 The size of the slider 、 Min and Max . |
ScrollPane | Container components with horizontal and vertical scrollbars |
TextArea | Multiline text field |
TextField | Single line text box |
these AWT The usage of component is relatively simple , You can refer to API Documents to get their respective construction methods 、 Details such as member methods .
Code example
import javax.swing.*;
import java.awt.*;
public class BasicComponentDemo {
Frame frame = new Frame(" Here we test the basic components ");
// Define a button
Button ok = new Button(" confirm ");
// Define a check box group
CheckboxGroup cbg = new CheckboxGroup();
// Define a radio box , Initially in the selected state , To add to cbg In the group
Checkbox male = new Checkbox(" male ", cbg, true);
// Define a radio box , Initially in the unselected state , To add to cbg In the group
Checkbox female = new Checkbox(" Woman ", cbg, false);
// Define a check box , Initially in the unselected state
Checkbox married = new Checkbox(" Married or not ?", false);
// Define a drop-down selection box
Choice colorChooser = new Choice();
// Define a list selection box
List colorList = new List(6, true);
// Define a 5 That's ok ,20 The multiline text field of the column
TextArea ta = new TextArea(5, 20);
// Define a 50 The single line text field of the column
TextField tf = new TextField(50);
public void init() {
// Pull down the selection box to add content
colorChooser.add(" Red ");
colorChooser.add(" green ");
colorChooser.add(" Blue ");
// Add content to the list selection box
colorList.add(" Red ");
colorList.add(" green ");
colorList.add(" Blue ");
// Create a load button and text box Panel Containers
Panel bottom = new Panel();
bottom.add(tf);
bottom.add(ok);
// hold bottom Add to Frame The bottom of
frame.add(bottom,BorderLayout.SOUTH);
// Create a Panel Containers , Load drop-down selection box , Radio and check boxes
Panel checkPanel = new Panel();
checkPanel.add(colorChooser);
checkPanel.add(male);
checkPanel.add(female);
checkPanel.add(married);
// Create a vertically arranged Box Containers , load Multiline text fields and checkPanel
Box topLeft = Box.createVerticalBox();
topLeft.add(ta);
topLeft.add(checkPanel);
// Create a horizontally arranged Box Containers , load topLeft And list selection box
Box top = Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
// take top Add to frame In the middle of
frame.add(top);
// Set up frame Best size and visible
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new BasicComponentDemo().init();
}
}
Dialog Dialog box
Concept 、 Method
Dialog yes Window Subclasses of classes , yes A container class , It belongs to a special component . Dialog box is a top-level window that can exist independently , Therefore, the usage is almost the same as that of ordinary windows , However, you need to pay attention to the following two points when using the dialog box :
1. Dialog boxes usually depend on other windows , It is usually necessary to have a parent window ;
2. Dialog box has non mode (non-modal) And pattern (modal) Two kinds of , When a mode dialog is opened , The modal dialog is always above its parent window , Before the Mode dialog is closed , The parent window cannot get focus ;
Method name | Method function |
---|---|
Dialog(Frame owner, String title, boolean modal) | Create a dialog object : owner: The parent window of the current dialog title: The title of the current dialog modal: Whether the current dialog is a modal dialog ,true/false |
Code example
adopt Frame、Button、Dialog Achieve the following effect :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo1 {
public static void main(String[] args) {
Frame frame = new Frame(" Test here Dialog");
Dialog d1 = new Dialog(frame, " Mode dialog ", true);
Dialog d2 = new Dialog(frame, " Non modal dialog ", false);
Button b1 = new Button(" Open the Mode dialog ");
Button b2 = new Button(" Open the modeless dialog box ");
// Set the size and location of the dialog box
d1.setBounds(20,30,300,400);
d2.setBounds(20,30,300,400);
// to b1 and b2 Bind listening event
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);
}
});
// Add a button to frame in
frame.add(b1);
frame.add(b2,BorderLayout.SOUTH);
// Set up frame Best size and visible
frame.pack();
frame.setVisible(true);
}
}
FileDialog Dialog box
summary 、 Method
Dialog Class and A subclass : FileDialog , It represents a file dialog , Used to open or save file , It should be noted that FileDialog Cannot specify modal or modeless , This is because FileDialog Depending on the implementation of the running platform , If the running platform's file dialog is modal , that FileDialog It's also modal ; Otherwise, it's non modal .
Method name | Method function |
---|---|
FileDialog(Frame parent, String title, int mode) | Create a file dialog : parent: Specify the parent window title: Dialog title mode: File dialog type , If specified as FileDialog.load, Used to open a file , If specified as FileDialog.SAVE, Used to save files |
String getDirectory() | Get the absolute path of the opened or saved file |
String getFile() | Get the file name of the opened or saved file |
Code example
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDialogTest {
public static void main(String[] args) {
Frame frame = new Frame(" Test here FileDialog");
FileDialog d1 = new FileDialog(frame, " Select the file to load ", FileDialog.LOAD);
FileDialog d2 = new FileDialog(frame, " Select the file to save ", FileDialog.SAVE);
Button b1 = new Button(" Open file ");
Button b2 = new Button(" Save the file ");
// Add events to the button
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
// Print the file path and name selected by the user
System.out.println(" User selected file path :"+d1.getDirectory());
System.out.println(" File name selected by the user :"+d1.getFile());
}
});
System.out.println("-------------------------------");
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
// Print the file path and name selected by the user
System.out.println(" User selected file path :"+d2.getDirectory());
System.out.println(" File name selected by the user :"+d2.getFile());
}
});
// Add button to frame in
frame.add(b1);
frame.add(b2,BorderLayout.SOUTH);
// Set up frame Best size and visible
frame.pack();
frame.setVisible(true);
}
}
Recommended reading :Java Detailed explanation of graphical interface (AWT、Swing) special column
边栏推荐
- How much computing power does transformer have
- 光模塊字母含義及參數簡稱大全
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- C # character similarity comparison general class
- Penetration tool - sqlmap
- VB. Net simple processing pictures, black and white (class library - 7)
- 1480. Dynamic sum of one-dimensional array
- APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
- SQL performance optimization skills
- 十二. golang其他
猜你喜欢
[microservice] Nacos cluster building and loading file configuration
Upper computer software development - log information is stored in the database based on log4net
BUU-Crypto-[GUET-CTF2019]BabyRSA
C # character similarity comparison general class
BUU-Real-[PHP]XXE
Uninstall Google drive hard drive - you must exit the program to uninstall
buuctf-pwn write-ups (8)
Solar insect killing system based on single chip microcomputer
AWT常用组件、FileDialog文件选择框
JS execution mechanism
随机推荐
19.Frambuffer应用编程
Input displays the currently selected picture
Flask
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
win10清除快速访问-不留下痕迹
Install pytoch geometric
ES6 模块化
C # character similarity comparison general class
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
Leetcode question brushing record | 206_ Reverse linked list
[microservice] Nacos cluster building and loading file configuration
C语言中的函数(详解)
BUU-Crypto-[HDCTF2019]basic rsa
Principle and practice of common defects in RSA encryption application
How much computing power does transformer have
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
Take you to quickly learn how to use qsort and simulate qsort
Impact relay jc-7/11/dc110v
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming