魁拔3战神崛起海问香:关于GUI的表达方式

来源:百度文库 编辑:高考问答 时间:2024/04/19 16:55:54
do
{ dStr=JOptionPane.showInputDialog("Please enter "+name+"'s demand:");
d=console.nextInt();
if (d<0)
System.out.println("Demand can't be negative.");
}while(d<0);

怎样能用GUI的形式表示啊?

你的意思是如果用户输入是负数就要求重新输入对吧。
这很好办啊,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener{

/**
*
*/
JTextField t;
public Test() {
super("Test");
t=new JTextField(20);
this.getContentPane().add(t);
t.addActionListener(this);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

// TODO Auto-generated constructor stub
}
public void actionPerformed(ActionEvent e){
try{
if(Integer.parseInt(t.getText())>0)
JOptionPane.showMessageDialog(this,"正确!");
else throw new Exception("要是正数就好了!");
}
catch(Exception ex){
if(ex.getMessage().equals("要是正数就好了!")){
JOptionPane.showMessageDialog(this,ex.getMessage());
t.setSelectionStart(0);
t.setSelectionEnd(t.getText().length());
}
else JOptionPane.showMessageDialog(this,"程序出错"+ex.getMessage());
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
new Test();
}
}

);
}

}