AWT中常用组件
基本组件
组件名 |
功能 |
Button |
Button |
Canvas |
用于绘图的画布 |
Checkbox |
复选框组件(也可当做单选框组件使用) |
CheckboxGroup |
用于将多个Checkbox 组件组合成一组, 一组 Checkbox 组件将只有一个可以 被选中 , 即全部变成单选框组件 |
Choice |
下拉选择框 |
Frame |
窗口 , 在 GUI 程序里通过该类创建窗口 |
Label |
标签类,用于放置提示性文本 |
List |
JU表框组件,可以添加多项条目 |
Panel |
不能单独存在基本容器类,必须放到其他容器中 |
Scrollbar |
滑动条组件。如果需要用户输入位于某个范围的值 , 就可以使用滑动条组件 ,比如调 色板中设置 RGB 的三个值所用的滑动条。当创建一个滑动条时,必须指定它的方向、初始值、 滑块的大小、最小值和最大值。 |
ScrollPane |
带水平及垂直滚动条的容器组件 |
TextArea |
多行文本域 |
TextField |
单行文本框 |
这些 AWT 组件的用法比较简单,可以查阅 API 文档来获取它们各自的构方法、成员方法等详细信息。
API 文档地址:https://www.apiref.com/java11-zh/java.desktop/javax/swing/package-summary.html
案例:
实现下图效果:
演示代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import javax.swing.*; import java.awt.*;
public class BasicComponentDemo { Frame frame = new Frame("这里测试基本组件");
Button ok = new Button("确认");
CheckboxGroup cbg = new CheckboxGroup(); Checkbox male = new Checkbox("男", cbg, true);
Checkbox female = new Checkbox("女", cbg, false);
Checkbox married = new Checkbox("是否已婚?", false);
Choice colorChooser = new Choice();
List colorList = new List(6, true);
TextArea ta = new TextArea(5, 20);
TextField tf = new TextField(50);
public void init() { colorChooser.add("红色"); colorChooser.add("绿色"); colorChooser.add("蓝色");
colorList.add("红色"); colorList.add("绿色"); colorList.add("蓝色");
Panel bottom = new Panel(); bottom.add(tf); bottom.add(ok);
frame.add(bottom,BorderLayout.SOUTH);
Panel checkPanel = new Panel(); checkPanel.add(colorChooser); checkPanel.add(male); checkPanel.add(female); checkPanel.add(married);
Box topLeft = Box.createVerticalBox(); topLeft.add(ta); topLeft.add(checkPanel);
Box top = Box.createHorizontalBox(); top.add(topLeft); top.add(colorList);
frame.add(top);
frame.pack(); frame.setVisible(true); }
public static void main(String[] args) {
new BasicComponentDemo().init();
} }
|
对话框Dialog
Dialog
Dialog 是 Window 类的子类,是 一个容器类,属于特殊组件 。 对话框是可以独立存在的顶级窗口, 因此用法与普通窗口的用法几乎完全一样,但是使用对话框需要注意下面两点:
- 对话框通常依赖于其他窗口,就是通常需要有一个父窗口;
- 对话框有非模式(non-modal)和模式(modal)两种,当某个模式对话框被打开后,该模式对话框总是位于它的父窗口之上,在模式对话框被关闭之前,父窗口无法获得焦点。
模式窗体:你必须关闭该窗体,才能操作其它窗体;比如说,必须按确定或取消,或者按关闭。
非模式窗体:不必关闭该窗体,就可转换到其它窗体上进行操作。
方法名称 |
方法功能 |
Dialog(Frame owner, String title, boolean modal) |
创建一个对话框对象: owner:当前对话框的父窗口 title:当前对话框的标题 modal:当前对话框是否是模式对话框,true/false |
案例1:
通过Frame、Button、Dialog实现下图效果:
演示代码1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*;
public class DialogDemo1 {
public static void main(String[] args) {
Frame frame = new Frame("这里测试Dialog");
Dialog d1 = new Dialog(frame, "模式对话框", true); Dialog d2 = new Dialog(frame, "非模式对话框", false);
Button b1 = new Button("打开模式对话框"); Button b2 = new Button("打开非模式对话框");
d1.setBounds(20,30,300,400); d2.setBounds(20,30,300,400);
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); } });
frame.add(b1); frame.add(b2,BorderLayout.SOUTH);
frame.pack(); frame.setVisible(true);
} }
|
在Dialog对话框中,可以根据需求,自定义内容
案例:
点击按钮,弹出一个模式对话框,其内容如下:
演示代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.JButton; import java.awt.*;
public class DialogDemo2 {
public static void main(String[] args) {
Frame frame = new Frame("这里测试Dialog");
Dialog d1 = new Dialog(frame, "模式对话框", true);
Box vBox = Box.createVerticalBox();
vBox.add(new TextField(15)); vBox.add(new JButton("确认")); d1.add(vBox);
Button b1 = new Button("打开模式对话框");
d1.setBounds(20,30,200,100);
b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { d1.setVisible(true); } });
frame.add(b1);
frame.pack(); frame.setVisible(true);
} }
|
FileDialog
Dialog 类还有 一个子类 : FileDialog ,它代表一个文件对话框,用于打开或者保存 文件,需要注意的是FileDialog无法指定模态或者非模态,这是因为 FileDialog 依赖于运行平台的实现,如果运行平台的文件对话框是模态的,那么 FileDialog 也是模态的;否则就是非模态的 。
方法名称 |
方法功能 |
FileDialog(Frame parent, String title, int mode) |
创建一个文件对话框: parent:指定父窗口 title:对话框标题 mode:文件对话框类型,如果指定为FileDialog.load,用于打开文件,如果指定为FileDialog.SAVE,用于保存文件 |
String getDirectory() |
获取被打开或保存文件的绝对路径 |
String getFile() |
获取被打开或保存文件的文件名 |
案例2:
使用 Frame、Button和FileDialog完成下图效果:
演示代码2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| 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("这里测试FileDialog");
FileDialog d1 = new FileDialog(frame, "选择需要加载的文件", FileDialog.LOAD); FileDialog d2 = new FileDialog(frame, "选择需要保存的文件", FileDialog.SAVE);
Button b1 = new Button("打开文件"); Button b2 = new Button("保存文件");
b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { d1.setVisible(true); System.out.println("用户选择的文件路径:"+d1.getDirectory()); System.out.println("用户选择的文件名称:"+d1.getFile()); } });
System.out.println("-------------------------------"); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { d2.setVisible(true); System.out.println("用户选择的文件路径:"+d2.getDirectory()); System.out.println("用户选择的文件名称:"+d2.getFile()); } });
frame.add(b1); frame.add(b2,BorderLayout.SOUTH);
frame.pack(); frame.setVisible(true); } }
|
如果您有什么问题或建议可以在下方的评论区评论,我会及时回复的。
欢迎关注我的公众号,共同学习,共同提升!
您可以通过公众号向我留言,也可以通过邮箱(lklong@88.com)联系我