使用BoxLayout、BorderLayout以及一些常用组件制作了一个调查表格
BasicComponentDemo
代码如下:
public class BasicComponentDemo {
Frame frame = new Frame();
TextArea textArea = new TextArea(7,20);
Choice colorChooser = new Choice();
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox male = new Checkbox("男",checkboxGroup,true);
Checkbox female = new Checkbox("女",checkboxGroup,false);
Checkbox isMarried = new Checkbox("是否已婚?");
TextField textField = new TextField(50);
Button ok = new Button("确认");
List colorList = new List(6,true);
public void init(){
Box box = Box.createHorizontalBox();
box.add(textField);
box.add(ok);
frame.add(box,BorderLayout.SOUTH);
colorChooser.add("红色");
colorChooser.add("绿色");
colorChooser.add("蓝色");
Box box1 = Box.createHorizontalBox();
box1.add(Box.createHorizontalGlue());
box1.add(colorChooser);
box1.add(Box.createHorizontalGlue());
box1.add(male);
box1.add(Box.createHorizontalGlue());
box1.add(female);
box1.add(Box.createHorizontalGlue());
box1.add(isMarried);
box1.add(Box.createHorizontalGlue());
Box box2 = Box.createVerticalBox();
box2.add(textArea);
box2.add(box1);
colorList.add("红色");
colorList.add("绿色");
colorList.add("蓝色");
Box box3 = Box.createHorizontalBox();
box3.add(box2);
box3.add(colorList);
frame.add(box3);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new BasicComponentDemo().init();
}
}
运行结果:
DialogDemo(对话框)
Dialog是window类的子类,是一个容器类,属于特殊组件。是可以独立存在的顶级窗口。用法和frame类似,但是需要依赖于其他窗口。有非模式和模式两种,模式对话框打开后总是位于它的父窗口之上。
代码如下:
package com.one.component;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo {
public static void main(String[] args) {
Frame frame = new Frame("测试Dialog");
Dialog d1 = new Dialog(frame, "模式对话框", true);
Dialog d2 = new Dialog(frame, "非模式对话框", false);
Box box = Box.createVerticalBox();
box.add(new TextField(20));
box.add(new Button("确认"));
d1.add(box);
d1.setBounds(20,30,300,200);
d2.setBounds(20,30,300,200);
Button b1 = new Button("打开模式对话框");
Button b2 = new Button("打开非模式对话框");
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,BorderLayout.NORTH);
frame.add(b2);
frame.pack();
frame.setVisible(true);
}
}
结果如下:
FileDialogDemo(文件对话框)
有FileDialog.LOAD(打开文件)和FileDialog.SAVE(保存文件)两种模式。
String getDirectory()获取文件路径
String getFile()获取文件名
代码如下:
package com.one.component;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDialogDemo {
public static void main(String[] args) {
Frame frame = new Frame("测试FileDialog");
FileDialog fileDialog1 = new FileDialog(frame,"选择要打开的文件",FileDialog.LOAD);
FileDialog fileDialog2 = new FileDialog(frame,"选择要保存的文件",FileDialog.SAVE);
Button button1 = new Button("打开文件");
Button button2 = new Button("保存文件");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileDialog1.setVisible(true);
String directory = fileDialog1.getDirectory();
String file = fileDialog1.getFile();
System.out.println("打开的文件路径为:"+directory);
System.out.println("打开的文件名为:"+file);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fileDialog2.setVisible(true);
String directory = fileDialog2.getDirectory();
String file = fileDialog2.getFile();
System.out.println("打开的文件路径为:"+directory);
System.out.println("打开的文件名为:"+file);
}
});
frame.add(button1,BorderLayout.NORTH);
frame.add(button2);
frame.pack();
frame.setVisible(true);
}
}
结果如下: