2012-03-18 8 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

私は新しいコンピュータがほしいです...何が問題なのですか?スタックオーバーフローはあなたのためにあなたのコードを右にはない –

+5

あなたのコードを書く*ためにここにはありません。 ;-) –

+2

@HovercraftFullOfEelsそれは恥ずかしいです...あなたは、スタックオーバーフローが実際にあなたのコードを '右に'ここにもあると言うことができる... –

答えて

5

使用JOptionPane、あなたはあなたがあなたのアクション実行される方法にいくつかのコードを追加することができます希望の回避策

+0

私はJOptionPaneで動作するようになってくれてありがとう、私のプログラマーはもっとプロフェッショナルに見えます。 – Sirnur

5

を返すことができることを、JOptionPane#showXxxDialogを見ています。メソッドのリストについては、APIのJFrameをご覧ください。しかし、おそらく何が必要なのかは、this.dispose();です。

thisなど既にスタックオーバーフローに関する既存の回答がいくつかあります。

編集:これはあなたが使用しているクラスも廃棄することに注意してください。それでもまだ忙しいロジックが必要な場合は、代わりにJFrameを隠すメソッドが必要です。私はあなた自身でこれを見つけることができると確信していますが、ビジネスロジックからGUIを分離する方が良い考えです。

+0

dam.dev非常に私はAPIを見ていただきありがとうございます、これは非常に重要な説明とすべてのクラスが含まれています。おかげで多く – Sirnur

+0

心配する必要はありません、正確に次の部分であなたが問題を抱えている部分を言うと、人々はすぐにより良いヘルプを提供することができます –

関連する問題