当我尝试从ActionListener调用JDialog时出现此错误,我尝试了几种解决方案,但它没有工作 这里的代码是针对JDialog类的:
package Ex2;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class dialog extends JDialog {
private JTextField f1;
private JTextField f2;
private JLabel label0;
private JLabel label1;
private JLabel label2;
private JCheckBox check;
private ButtonPanel buttons;
public dialog() {
setTitle("title");
this.setLayout(new BorderLayout());
JPanel pNorth = new JPanel();
JPanel pCenter2 = new JPanel();
JPanel pCenter3 = new JPanel();
label0 = new JLabel("entrer le texte a rechercher");
pNorth.add(label0);
add(pNorth,BorderLayout.NORTH);
JPanel pCentre = new JPanel();
setLayout(new BoxLayout(pCentre,BoxLayout.X_AXIS));
JPanel pCenter1 = new JPanel();
setLayout(new BoxLayout(pCenter1,BoxLayout.Y_AXIS));
buttons = new ButtonPanel(4, 'y');
pCenter1.add(buttons);
f1=new JTextField(30);
f2=new JTextField(30);
setLayout(new BoxLayout(pCenter2,BoxLayout.Y_AXIS));
pCenter2.add(f1);
pCenter2.add(f2);
label1=new JLabel("Rechercher: ");
label2=new JLabel("Remplacer par: ");
setLayout(new BoxLayout(pCenter3, BoxLayout.Y_AXIS));
pCenter3.add(label1);
pCenter3.add(label2);
pCentre.add(pCenter1);
pCentre.add(pCenter2);
pCentre.add(pCenter3);
add(pCentre,BorderLayout.CENTER);
setSize(100,100);
setVisible(true);
}}
调用JDialog的ActionListener类
package Ex2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
public class actionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
new dialog();
}
}
测试类是JFrame,点击按钮3应该调用事件
package Ex2;
import javax.swing.JFrame;
public class test extends JFrame{
public test(){
setTitle("title");
ButtonPanel a = new ButtonPanel(5, 'y');
a.setAction(3, new actionListener());
add(a);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
}
public static void main(String args []){
new test();
}
}