我對actionListener有問題。我創建了自己的簡單對話框,它只有兩個JButtons - 是和否。當我點擊按鈕時,actionListener不響應。JButton對actionListener沒有反應
這是我的代碼:
private void showInfoNewUML() {
Dimension buttonsSize = new Dimension(60, 25);
Dimension programSize = new Dimension(1200, 700);
final JDialog dialogWindow = new JDialog(this, "Erase actual UML diagram"
+ " with his files", true);
JTextArea descDialogWindow = new JTextArea("Do you really erase actual\n"
+ "UML diagram with his files? ");
descDialogWindow.setEditable(false);
descDialogWindow.setBackground(new Color(220, 220, 220));
descDialogWindow.setBorder(null);
dialogWindow.getContentPane().setBackground(new Color(220, 220, 220));
dialogWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
dialogWindow.setModal(true);
dialogWindow.setResizable(false);
dialogWindow.setLayout(new FlowLayout());
dialogWindow.setSize(310, 100);
dialogWindow.setLocation((int) programSize.getWidth()/2,
(int) programSize.getHeight()/2);
JButton buttonYes = new JButton("Yes");
JButton buttonNo = new JButton("No");
buttonYes.setPreferredSize(buttonsSize);
buttonNo.setPreferredSize(buttonsSize);
dialogWindow.add(descDialogWindow);
dialogWindow.add(buttonYes);
dialogWindow.add(buttonNo);
dialogWindow.setVisible(true);
buttonYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonAnoActionPerformed(e);
}
private void buttonAnoActionPerformed(ActionEvent e) {
dialogWindow.setVisible(false);
}
});
buttonNo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonNeActionPerformed(e);
}
private void buttonNeActionPerformed(ActionEvent e) {
dialogWindow.setVisible(false);
}
});
}
我想關閉這個對話框後,我按一下按鈕。當我點擊十字的右上角按鈕時,對話窗口關閉。
謝謝你幫助解決這個問題。
創建一個[SSCCE](http://sscce.org)將幫助我們理解您的問題。 – Jeffrey 2012-04-05 21:38:56
''你真的用他的文件擦除了實際的\ n「 +」UML圖嗎?「'這似乎更適合['JOptionPane.showConfirmDialog(...)'](http://docs.ora .com/javase/7/docs/api/javax/swing/JOptionPane.html#showConfirmDialog%28java.awt.Component,%20java.lang.Object%29)(以及重載的變體)。 – 2012-04-05 21:44:25
是的,我願意。不幸的是,我正在做一個捷克語的應用程序,它可以響應showConfirmDialog Yes [in czech「Ano」]和No [in czech「Ne」]進行調整。這是我創建自己的對話框的原因。 – avalagne 2012-04-05 21:48:20