我一个java程序,需弹出一个Swing窗体,窗体里只有一个文本输入框和确定按钮,单击"确定"按钮,就关闭窗口并把值返回给main()函数,我的解决办法是这样的import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;/**
* 输入验证码弹出窗口
* @author peidw 2008-10-17
*
*/
public class InputValidCodeJFrame extends JFrame implements ActionListener {
private String input_code;
JTextField txt_code = new JTextField("");
public InputValidCodeJFrame(){
super();
setSize(500, 200);
setTitle("请输入验证码");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,5));
p2.add(new JLabel("验证码:"));
p2.add(new JLabel(new ImageIcon(this.getClass().getResource("17.gif"))));
p2.add(new JLabel("请输入验证码:"));
p2.add(txt_code);
JButton btn_ok = new JButton("确定");
btn_ok.addActionListener(this);
p2.add(btn_ok);
c.add(p2);
} public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if(button.getText().equals("确定")){
this.setVisible(false);
input_code=txt_code.getText();
System.out.println("input_code=="+input_code);
}
}
public static void main(String args[]) throws Exception{
InputValidCodeJFrame ot = new InputValidCodeJFrame();
ot.setVisible(true);
System.out.println("ot.isShowing()----------->"+ot.isShowing());
System.out.println("ot.input_code------------>"+ot.input_code);
Thread.currentThread().sleep(5000);
if(ot.isShowing()==false){
String a=ot.input_code;
System.out.println("输入值="+a);
} }
}=======================
让线程暂停等待,我觉得这样不好,肯定有更好的方法,这是我第一交写GUI的程序,请大家帮忙一下,还有什么别的办法
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;/**
* 输入验证码弹出窗口
* @author peidw 2008-10-17
*
*/
public class InputValidCodeJFrame extends JFrame implements ActionListener {
private String input_code;
JTextField txt_code = new JTextField("");
public InputValidCodeJFrame(){
super();
setSize(500, 200);
setTitle("请输入验证码");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,5));
p2.add(new JLabel("验证码:"));
p2.add(new JLabel(new ImageIcon(this.getClass().getResource("17.gif"))));
p2.add(new JLabel("请输入验证码:"));
p2.add(txt_code);
JButton btn_ok = new JButton("确定");
btn_ok.addActionListener(this);
p2.add(btn_ok);
c.add(p2);
} public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if(button.getText().equals("确定")){
this.setVisible(false);
input_code=txt_code.getText();
System.out.println("input_code=="+input_code);
}
}
public static void main(String args[]) throws Exception{
InputValidCodeJFrame ot = new InputValidCodeJFrame();
ot.setVisible(true);
System.out.println("ot.isShowing()----------->"+ot.isShowing());
System.out.println("ot.input_code------------>"+ot.input_code);
Thread.currentThread().sleep(5000);
if(ot.isShowing()==false){
String a=ot.input_code;
System.out.println("输入值="+a);
} }
}=======================
让线程暂停等待,我觉得这样不好,肯定有更好的方法,这是我第一交写GUI的程序,请大家帮忙一下,还有什么别的办法
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货