【问题标题】:i want the window to close when the data has been inputed [closed]我希望在输入数据后关闭窗口[关闭]
【发布时间】:2012-04-03 11:59:32
【问题描述】:

我希望在输入输入并单击添加按钮后立即关闭窗口。 我还希望出现一条消息,通知用户输入数据已保存。目前,此代码已链接到将存储输入的数据库对象。

public class Add extends JFrame
             implements ActionListener {

/** {@link JTextField} where the user name is entered */
JTextField Inputusername = new JTextField(7);

/** {@link JTextField} where the user age is entered */
JTextField age = new JTextField(2);

/** {@link JTextField} where the user ID is entered */
JTextField inputuserid = new JTextField(4);

/** Add Client button */

JButton addnewclient = new JButton("Add Client");
/** male Jradiobutton */
JRadioButton male = new JRadioButton("Male");
/** female Jradiobutton */
JRadioButton female = new JRadioButton("Female");
/** label for the gender selection */
Label genders = new Label("please select gender of client");

/** call the database constructor*/

private Database db; 
public Add(Database db) 
    { this.db = db; 


    //allows the positioning 
    setLayout(new BorderLayout()); 

  //setting the size of the window
    setBounds(100, 100, 500, 200); 

 // the title of the window
    setTitle("add new Client"); 

    // dispose of the window when the close button is clicked
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

 // declared new panel  
    JPanel top = new JPanel(); 
    top.add(new JLabel("Enter username :"));
    top.add(Inputusername);
    top.add(new JLabel("Enter age:"));
    top.add(age);
    top.add(new JLabel("Enter userid:"));
    top.add(inputuserid);
    add("North",top);
    // declared new panel 
    JPanel bottom = new JPanel(); 
 // add the veritable of JButton to the top panel
    bottom.add(addnewclient); 
 // add the bottom panel to the bottom of the screen
    add("South",bottom); 

    JPanel middle = new JPanel();
    ButtonGroup bg = new ButtonGroup();
    bg.add(male);
    bg.add(female);
    middle.add(male);
    middle.add(female);
    add("Center",middle);

 // do not allow user to set the size of the screen
    setResizable(false);
 // make the program  visible
    setVisible(true);
 // listen to the button
   addnewclient.addActionListener(this);   
}  

public void actionPerformed(ActionEvent e) {

    String selection = "female"; 

    if (this.male.isSelected()) 
    {
         selection = "male"; 
    }

    User u = new User(Inputusername.getText(),  selection , age.getText(), inputuserid.getText());
    db.addUser(u); 

}

【问题讨论】:

  • 我想要一台新电脑...请问有什么问题?堆栈溢出不是在这里为您纠正您的代码
  • 这里也不是为您编写您的代码。 ;-)
  • @HovercraftFullOfEels 这很尴尬......你甚至可以说堆栈溢出实际上也是为了“正确”你的代码......
  • @dann:是的,我可以理解,因为我犯了同样的错误一百万次。哦哦,看起来西穆尔开始大喊大叫了。这会让我想帮助他。
  • 感谢大家的帮助,我已经设法解决了我的申请问题,再次非常感谢

标签: java swing actionlistener


【解决方案1】:

使用JOptionPane,你必须查看JOptionPane#showXxxDialog,这可以返回所需的解决方法

【讨论】:

  • 非常感谢我让它与 JOptionPane 一起工作,我的程序看起来更专业。
【解决方案2】:

您可以在您的操作执行方法中添加一些代码。查看 JFrame 的 API 以获取方法列表,但您可能想要调用 this.dispose();

还有一些关于堆栈溢出的现有答案,例如this

编辑:请注意,这也会处理您正在使用的类;因此,如果您仍然需要一些繁忙的逻辑,那么您需要一个隐藏JFrame 的方法。我相信您可以自己找到它,但最好将 GUI 与业务逻辑分开。

【讨论】:

  • dam.dev 非常感谢我查看了API,这非常重要,包含所有带有解释的类。非常感谢
  • 别着急,下次尽量说清楚你在哪个部分有问题,然后人们可以更快地提供更好的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
相关资源
最近更新 更多