首先Java GUI包括Frame,Panel,Component 等.Button,Frame,Panel,TextField等都是Component.Panel不能单独显示,必须,放在某个Frame上,一个 Frame内可以包含多个Panel,每个Panel又可包含多个Componnet.
每个用来显示的对话框基本都是从Frame或者JFrame类继承,然后设定自己需要显示的Component,并添加到该类中.Frame和Panel 都有自己默认的布局管理器,当然也可根据需要选择不同的,即规定如何规划其上面的Component,Frame默认的Layout(布局管理器)为 BorderLayout,Panel为FlowLayout.另一种比较常见的Layout就是GridLayout(用来规定几行几列,)
eg:public class UsrSelectDialog extends JFrame implements ActionListener{
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t = new JTextField();
public UsrSelectDialog(String s)
{
super(s);
}
public String gett1()
{
return t1.getText();
}
public String gett2()
{
return t2.getText();
}
public String gett3()
{
return t3.getText();
}
public void showGUI()
{
this.setBounds(400, 400, 400, 150);
JPanel contenPanel = new JPanel (new BorderLayout());
JPanel okPanel = new JPanel();
JPanel jp = new JPanel(new GridLayout(3,4));
jp.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
okPanel.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
JButton okButton = new JButton("ok");
okPanel.add(okButton);
okButton.setMnemonic('D');
// NumberOnlyDocument doc1 = new NumberOnlyDocument();
NumberOnlyDocument doc2 = new NumberOnlyDocument();
NumberOnlyDocument doc3 = new NumberOnlyDocument();
// doc1.setRange();
doc2.setRange(60, 1);
doc3.setRange(10, 1);
// t1.setDocument(doc1);
t2.setDocument(doc2);
t3.setDocument(doc3);
JLabel jl = new JLabel(" IP :",10);
JLabel jl1 = new JLabel("*");
JLabel j2 = new JLabel(" Delay :",10);
JLabel j21= new JLabel("");
JLabel j3 = new JLabel(" Period:",10);
JLabel j31= new JLabel("");
jp.add(jl);
jp.add(t1);
jp.add(jl1);
jp.add(j2);
jp.add(t2);
jp.add(j21);
jp.add(j3);
jp.add(t3);
jp.add(j31);
contenPanel.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
contenPanel.add(jp,BorderLayout.NORTH);
contenPanel.add(okPanel,BorderLayout.SOUTH);
this.add(contenPanel);
okButton.addActionListener(this);
this.setResizable(false);
this.setVisible(true);
}
}
该对话框添加了三个TextField域(用来输入某些信息)和一个Button(用来提交输入).整个对话框的布局为contenPanel上有两个单 独的Panel:okPanel(显示按钮)和jp(显示三个TextField域).TextFiled域也可以设定输入字符的限制,本例子限制后两个 域只能输入数字,这种功能通过函数setDocument(Document doc)实现,其中参数可以是自己定义的继承Document类的子类,主要是重写其中的insetString()方法,已达到完成特殊限制的目的.本 文的继承Document类的子类如下:
package netconnect;
import java.awt.*;
import javax.swing.text.*;
/**
* 只能输入数字的Swing文档模型。
* 可以设置可以保存的数字的最大值和最小值。
* @since 0.1
*/
@SuppressWarnings("serial")
public class NumberOnlyDocument extends PlainDocument {
private boolean haveDot = false;
private int length = 0;
private int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
private boolean isLimit = false;
/**
* 构造一个缺省的NumberOnlyDocument。
* @since 0.1
*/
public NumberOnlyDocument() {
}
/**
* 构造一个限制数字大小的NumberOnlyDocument。
* 只有在max不小于min的情况下max和min的设置才生效。
* @param max 最大值
* @param min 最小值
* @since 0.3
*/
public NumberOnlyDocument(int max, int min) {
if (max >= min) {
this.max = max;
this.min = min;
}
this.isLimit = true;
}
/**
* 构造一个限制数字大小的NumberOnlyDocument。
* 只有在max不小于min的情况下max和min的设置才生效。
* @param max 最大值
* @param min 最小值
* @param isLimit 是否限制数字大小
* @since 0.3
*/
public NumberOnlyDocument(int max, int min, boolean isLimit) {
if (max >= min) {
this.max = max;
this.min = min;
}
this.isLimit = isLimit;
}
/**
* 设置是否限制数字的大小。
* 如果参数是true时只有在原来的最大值大于最小值是这个设置才起效,否则此方法调用没有任何效果。
* 如果参数值是false则取消输入数字大小的限制
* @param isLimit 是否限制输入数字的大小
* @since 0.3
*/
public void setLimit(boolean isLimit) {
if (isLimit == true && max >= min) {
this.isLimit = isLimit;
}
else if (isLimit == false) {
this.isLimit = isLimit;
}
}
/**
* 设置可以输入的值的范围。
* 如果max小于min则可以输入的最大值和最小值的限制无效,作为没有大小限制的情况处理。
* @param max 可以输入的最大值
* @param min 可以输入的最小值
* @since 0.3
*/
public void setRange(int max, int min) {
if (max >= min) {
this.max = max;
this.min = min;
isLimit = true;
}
}
/**
* 是否限制数字的大小。
* @return 限制输入大小时返回true,否则返回false。
* @since 0.3
*/
public boolean isLimit() {
return isLimit;
}
/**
* 返回限制的最大值。
* @return 限制的最大值。如果是不限制则返回Double.MIN_VALUE。
* @since 0.3
*/
public double getLimitedMax() {
if (!isLimit) {
return Double.MIN_VALUE;
}
else {
return max;
}
}
/**
* 返回限制的最小值。
* @return 限制的最小值。如果是不限制则返回Double.MAX_VALUE。
* @since 0.3
*/
public int getLimitedMin() {
if (!isLimit) {
return Integer.MAX_VALUE;
}
else {
return min;
}
}
/**
* 插入某些内容到文档中。
* 只有当输入的是数字或者和数字相关的“.”、“-”等符号并且符合构成合法数字的规则时才被插入。
* 此方法是线程安全的。
* @param offs 插入位置的偏移量
* @param str 插入内容
* @param a 属性集合
* @throws BadLocationException 给出的插入位置不是文档中的有效位置
* @since 0.1
*/
public void insertString(int offs, String str, AttributeSet a) throws
BadLocationException {
if (str == null) {
return;
}
char[] number = str.toCharArray();
for (int i = 0; i < number.length; i++) {
if (offs == 0) {
if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.' ||
number[i] == '-' || number[i] == '+')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
return;
}
}
else {
length++;
}
}
else {
if (!haveDot) {
if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
return;
}
}
else {
if (number[i] == '.') {
haveDot = true;
}
length++;
}
}
else {
if (! (number[i] >= '0' && number[i] <= '9')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
Toolkit.getDefaultToolkit().beep();
return;
}
}
else {
length++;
}
}
}
}
if (isLimit == true) {
String text = getText(0, offs) + str + getText(offs, getLength());
double result = Double.parseDouble(text);
if (result > max || result < min) {
return;
}
}
super.insertString(offs, new String(number), a);
}
}
任何一个Component对象都可以指定大小(Size),颜色(Color),字体(Font),是否可工作(setEnable(boolean))等等.
下面介绍事件监听,任何事件监听类都要实现ActionListener接口,并实现其中的actionPerformed(ActionEvent e)方法,例如
class autenBegin implements ActionListener{
public void actionPerformed(ActionEvent e) {
r2.setVisible(false);
waitDialog = new ResutlDialog("Trusted Autentication Result Dialog");
waitDialog.showWait();
String cmd = "java -jar /root/Desktop/workspace/TNCCLog/TNCCLog.jar";
Process p;
BufferedReader in = null;
BufferedReader bis = null;
try {
p = Runtime.getRuntime().exec(cmd);
in =new BufferedReader(new InputStreamReader( p.getInputStream()));
String s = null;
if(in!=null)
{
while((s=in.readLine())!=null)
{
if(s=="Result is isolate")
{
waitDialog.setVisible(false);
waitDialog = null;
authenResultDialog = new ResutlDialog("Trusted Autentication Result Dialog");
ImageIcon img1 =new ImageIcon("/root/Desktop/workspace/NetworkConenctionTest/bad_smelly.jpg");
authenResultDialog.showResult( "Trusted Authentication Result is Isolate!",img1);
System.out.println("Result is isolate");
}
else if(s=="Result is failure")
{
waitDialog.setVisible(false);
waitDialog = null;
authenResultDialog = new ResutlDialog("Trusted Autentication Result Dialog");
ImageIcon img2 =new ImageIcon("/root/Desktop/workspace/NetworkConenctionTest/bad_smelly.jpg");
authenResultDialog.showResult( "Trusted Authentication Failure!",img2);
System.out.println("Result is failure");
}else if(s.contains("Result is full allowed,Trusted Level is")){
waitDialog.setVisible(false);
waitDialog= null;
r1 = new ResutlDialog("Trusted Autentication Result Dialog");
r1.showAuthenResult();
System.out.println("Result is full allowed");
}
}
}
else{
System.out.println("in is null");
}
} catch (IOException ioE) {
ioE.printStackTrace();
}finally{
try {
if(bis!=null)
{
bis.close();
bis = null;
}
if(in!=null)
{
in.close();
in = null;
}
}catch (IOException e1) {
e1.printStackTrace();
}
}
}
主要的功能为,启动一个进程,并根据外部进程的输出,建立不同的对话框,其中waitDialog等变量都是自己继承了Frame类的子类对象,这里不详细给出
来源:http://hi.baidu.com/jingzhang_2007/blog/item/eac35b0983dbe287d1581bc2.html
每个用来显示的对话框基本都是从Frame或者JFrame类继承,然后设定自己需要显示的Component,并添加到该类中.Frame和Panel 都有自己默认的布局管理器,当然也可根据需要选择不同的,即规定如何规划其上面的Component,Frame默认的Layout(布局管理器)为 BorderLayout,Panel为FlowLayout.另一种比较常见的Layout就是GridLayout(用来规定几行几列,)
eg:public class UsrSelectDialog extends JFrame implements ActionListener{
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t = new JTextField();
public UsrSelectDialog(String s)
{
super(s);
}
public String gett1()
{
return t1.getText();
}
public String gett2()
{
return t2.getText();
}
public String gett3()
{
return t3.getText();
}
public void showGUI()
{
this.setBounds(400, 400, 400, 150);
JPanel contenPanel = new JPanel (new BorderLayout());
JPanel okPanel = new JPanel();
JPanel jp = new JPanel(new GridLayout(3,4));
jp.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
okPanel.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
JButton okButton = new JButton("ok");
okPanel.add(okButton);
okButton.setMnemonic('D');
// NumberOnlyDocument doc1 = new NumberOnlyDocument();
NumberOnlyDocument doc2 = new NumberOnlyDocument();
NumberOnlyDocument doc3 = new NumberOnlyDocument();
// doc1.setRange();
doc2.setRange(60, 1);
doc3.setRange(10, 1);
// t1.setDocument(doc1);
t2.setDocument(doc2);
t3.setDocument(doc3);
JLabel jl = new JLabel(" IP :",10);
JLabel jl1 = new JLabel("*");
JLabel j2 = new JLabel(" Delay :",10);
JLabel j21= new JLabel("");
JLabel j3 = new JLabel(" Period:",10);
JLabel j31= new JLabel("");
jp.add(jl);
jp.add(t1);
jp.add(jl1);
jp.add(j2);
jp.add(t2);
jp.add(j21);
jp.add(j3);
jp.add(t3);
jp.add(j31);
contenPanel.setBackground(Color.gray.brighter().brighter().brighter().brighter().brighter().brighter());
contenPanel.add(jp,BorderLayout.NORTH);
contenPanel.add(okPanel,BorderLayout.SOUTH);
this.add(contenPanel);
okButton.addActionListener(this);
this.setResizable(false);
this.setVisible(true);
}
}
该对话框添加了三个TextField域(用来输入某些信息)和一个Button(用来提交输入).整个对话框的布局为contenPanel上有两个单 独的Panel:okPanel(显示按钮)和jp(显示三个TextField域).TextFiled域也可以设定输入字符的限制,本例子限制后两个 域只能输入数字,这种功能通过函数setDocument(Document doc)实现,其中参数可以是自己定义的继承Document类的子类,主要是重写其中的insetString()方法,已达到完成特殊限制的目的.本 文的继承Document类的子类如下:
package netconnect;
import java.awt.*;
import javax.swing.text.*;
/**
* 只能输入数字的Swing文档模型。
* 可以设置可以保存的数字的最大值和最小值。
* @since 0.1
*/
@SuppressWarnings("serial")
public class NumberOnlyDocument extends PlainDocument {
private boolean haveDot = false;
private int length = 0;
private int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
private boolean isLimit = false;
/**
* 构造一个缺省的NumberOnlyDocument。
* @since 0.1
*/
public NumberOnlyDocument() {
}
/**
* 构造一个限制数字大小的NumberOnlyDocument。
* 只有在max不小于min的情况下max和min的设置才生效。
* @param max 最大值
* @param min 最小值
* @since 0.3
*/
public NumberOnlyDocument(int max, int min) {
if (max >= min) {
this.max = max;
this.min = min;
}
this.isLimit = true;
}
/**
* 构造一个限制数字大小的NumberOnlyDocument。
* 只有在max不小于min的情况下max和min的设置才生效。
* @param max 最大值
* @param min 最小值
* @param isLimit 是否限制数字大小
* @since 0.3
*/
public NumberOnlyDocument(int max, int min, boolean isLimit) {
if (max >= min) {
this.max = max;
this.min = min;
}
this.isLimit = isLimit;
}
/**
* 设置是否限制数字的大小。
* 如果参数是true时只有在原来的最大值大于最小值是这个设置才起效,否则此方法调用没有任何效果。
* 如果参数值是false则取消输入数字大小的限制
* @param isLimit 是否限制输入数字的大小
* @since 0.3
*/
public void setLimit(boolean isLimit) {
if (isLimit == true && max >= min) {
this.isLimit = isLimit;
}
else if (isLimit == false) {
this.isLimit = isLimit;
}
}
/**
* 设置可以输入的值的范围。
* 如果max小于min则可以输入的最大值和最小值的限制无效,作为没有大小限制的情况处理。
* @param max 可以输入的最大值
* @param min 可以输入的最小值
* @since 0.3
*/
public void setRange(int max, int min) {
if (max >= min) {
this.max = max;
this.min = min;
isLimit = true;
}
}
/**
* 是否限制数字的大小。
* @return 限制输入大小时返回true,否则返回false。
* @since 0.3
*/
public boolean isLimit() {
return isLimit;
}
/**
* 返回限制的最大值。
* @return 限制的最大值。如果是不限制则返回Double.MIN_VALUE。
* @since 0.3
*/
public double getLimitedMax() {
if (!isLimit) {
return Double.MIN_VALUE;
}
else {
return max;
}
}
/**
* 返回限制的最小值。
* @return 限制的最小值。如果是不限制则返回Double.MAX_VALUE。
* @since 0.3
*/
public int getLimitedMin() {
if (!isLimit) {
return Integer.MAX_VALUE;
}
else {
return min;
}
}
/**
* 插入某些内容到文档中。
* 只有当输入的是数字或者和数字相关的“.”、“-”等符号并且符合构成合法数字的规则时才被插入。
* 此方法是线程安全的。
* @param offs 插入位置的偏移量
* @param str 插入内容
* @param a 属性集合
* @throws BadLocationException 给出的插入位置不是文档中的有效位置
* @since 0.1
*/
public void insertString(int offs, String str, AttributeSet a) throws
BadLocationException {
if (str == null) {
return;
}
char[] number = str.toCharArray();
for (int i = 0; i < number.length; i++) {
if (offs == 0) {
if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.' ||
number[i] == '-' || number[i] == '+')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
return;
}
}
else {
length++;
}
}
else {
if (!haveDot) {
if (! (number[i] >= '0' && number[i] <= '9' || number[i] == '.')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
return;
}
}
else {
if (number[i] == '.') {
haveDot = true;
}
length++;
}
}
else {
if (! (number[i] >= '0' && number[i] <= '9')) {
if (offs == length - 1) {
remove(offs + i, 1);
}
else {
Toolkit.getDefaultToolkit().beep();
return;
}
}
else {
length++;
}
}
}
}
if (isLimit == true) {
String text = getText(0, offs) + str + getText(offs, getLength());
double result = Double.parseDouble(text);
if (result > max || result < min) {
return;
}
}
super.insertString(offs, new String(number), a);
}
}
任何一个Component对象都可以指定大小(Size),颜色(Color),字体(Font),是否可工作(setEnable(boolean))等等.
下面介绍事件监听,任何事件监听类都要实现ActionListener接口,并实现其中的actionPerformed(ActionEvent e)方法,例如
class autenBegin implements ActionListener{
public void actionPerformed(ActionEvent e) {
r2.setVisible(false);
waitDialog = new ResutlDialog("Trusted Autentication Result Dialog");
waitDialog.showWait();
String cmd = "java -jar /root/Desktop/workspace/TNCCLog/TNCCLog.jar";
Process p;
BufferedReader in = null;
BufferedReader bis = null;
try {
p = Runtime.getRuntime().exec(cmd);
in =new BufferedReader(new InputStreamReader( p.getInputStream()));
String s = null;
if(in!=null)
{
while((s=in.readLine())!=null)
{
if(s=="Result is isolate")
{
waitDialog.setVisible(false);
waitDialog = null;
authenResultDialog = new ResutlDialog("Trusted Autentication Result Dialog");
ImageIcon img1 =new ImageIcon("/root/Desktop/workspace/NetworkConenctionTest/bad_smelly.jpg");
authenResultDialog.showResult( "Trusted Authentication Result is Isolate!",img1);
System.out.println("Result is isolate");
}
else if(s=="Result is failure")
{
waitDialog.setVisible(false);
waitDialog = null;
authenResultDialog = new ResutlDialog("Trusted Autentication Result Dialog");
ImageIcon img2 =new ImageIcon("/root/Desktop/workspace/NetworkConenctionTest/bad_smelly.jpg");
authenResultDialog.showResult( "Trusted Authentication Failure!",img2);
System.out.println("Result is failure");
}else if(s.contains("Result is full allowed,Trusted Level is")){
waitDialog.setVisible(false);
waitDialog= null;
r1 = new ResutlDialog("Trusted Autentication Result Dialog");
r1.showAuthenResult();
System.out.println("Result is full allowed");
}
}
}
else{
System.out.println("in is null");
}
} catch (IOException ioE) {
ioE.printStackTrace();
}finally{
try {
if(bis!=null)
{
bis.close();
bis = null;
}
if(in!=null)
{
in.close();
in = null;
}
}catch (IOException e1) {
e1.printStackTrace();
}
}
}
主要的功能为,启动一个进程,并根据外部进程的输出,建立不同的对话框,其中waitDialog等变量都是自己继承了Frame类的子类对象,这里不详细给出
来源:http://hi.baidu.com/jingzhang_2007/blog/item/eac35b0983dbe287d1581bc2.html