2012-03-18 80 views
-2

我希望窗口在輸入完成後立即關閉,然後單擊添加按鈕。 另外我想要一條消息來通知用戶輸入數據已保存。此時此代碼鏈接到將存儲輸入的數據庫對象。我希望在輸入數據時關閉窗口

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); 

}

+0

我想要一臺新電腦......問題是什麼?堆棧溢出不是在這裏爲你的代碼正確 – 2012-03-18 22:09:13

+5

也不是在這裏*寫你的代碼給你。 ;-) – 2012-03-18 22:10:58

+2

@HovercraftFullOfEels這是令人尷尬的...你甚至可以說堆棧溢出實際上是在'正確'你的代碼以及... – 2012-03-18 22:57:36

回答

5

您可以添加一些代碼到你的動作執行的方法。查看JFrame的API以獲取方法列表,但您可能需要的是致電this.dispose();

堆棧溢出已經有一些現有的答案,如this

編輯:請注意,這將處理您正在使用的類;所以如果你仍然需要一些繁忙的邏輯,那麼你需要一種隱藏JFrame的方法。我相信你可以自己找到它,但將GUI與商業邏輯分開是一個更好的主意。

+0

dam.dev非常感謝你我通過API看,這是非常重要的包含所有類與解釋。非常感謝 – Sirnur 2012-03-19 21:34:24

+0

不用擔心,試着明確下一次你遇到哪個問題,那麼人們可以更快地提供更好的幫助 – 2012-03-19 22:24:08