Java 我的JButton不会对ActionListener和actionPerformed做出反应
作为我项目的一部分,我编写了这部分代码,当按下按钮时应该采取行动。在我添加新代码之前,它一直正常工作,即变量设置为“”。现在它不起作用了。我的意思是,代码不会等到按钮被按下(我也放了一个标志,在控制台中写了一个“ok”,但执行的操作似乎不会启动。 怎么了?我找不到任何理由不让它等待按钮。 谢谢 我在下面添加了一些代码,希望能有所帮助。我仍然遇到一些问题,Java 我的JButton不会对ActionListener和actionPerformed做出反应,java,swing,Java,Swing,作为我项目的一部分,我编写了这部分代码,当按下按钮时应该采取行动。在我添加新代码之前,它一直正常工作,即变量设置为“”。现在它不起作用了。我的意思是,代码不会等到按钮被按下(我也放了一个标志,在控制台中写了一个“ok”,但执行的操作似乎不会启动。 怎么了?我找不到任何理由不让它等待按钮。 谢谢 我在下面添加了一些代码,希望能有所帮助。我仍然遇到一些问题,ActionPerformed方法显示错误。代码中有详细信息 public class Creaframe extends JFrame imp
ActionPerformed
方法显示错误。代码中有详细信息
public class Creaframe extends JFrame implements ActionListener {
public static JPanel con = new JPanel();
public static JFrame frame =new Creaframe();
public static String radio = "";
public static String speed = "";
public static void main(String[] args) {
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing (java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog (frame,
" Vuoi veramente Uscire ?") ==
JOptionPane.YES_OPTION)
{
System.exit (0);
}
}
});
frame.setSize (1300, 900);
frame.setLocation (200,100);
con = new JPanel();
con.setBackground(Color.YELLOW);
public Creaframe () {
super (" My program " );
menuBar = new JMenuBar();
menuBar.add(makeFileMenu());
menuBar.add(cerca());
menuBar.add(trova());
menuBar.add(excel());
setJMenuBar(menuBar);
pack();
}
试试下面的代码 使用ActionListerner实现,您不需要使用匿名constructor多次编写actionperformed方法。 您可以使用一个actionPerformed方法来处理多个事件 1) 在类中实现ActionListener接口
con = new JPanel();
JButton bt = new JButton (" Insert ");
con.add (bt);
bt.addActionListener(this);
frame.add (con)
frame.setVisible(true);
@Override
public void actionPerformed(ActionEvent evt) {
radio = rb.getSelection().getActionCommand();
speed = "";
//extra code//
}
您可以使用下面的方法删除侦听器
bt.removeActionListener(this);
< > > >编辑< /强>为您的帮助添加了完整的类,我认为您的构造函数的正确实现可以获得MenuBar
选中用于创建JMenubar的链接
actionlistener在任何情况下都可以工作,而当您在action侦听之前设置帧时,这可能就是问题所在。尝试将您的框架代码发布到actionlistener方法。请提供一个示例,以便我们也可以重现您的错误行为并提供解决方案。您的实际问题对我们来说毫无意义,因为我们不熟悉您的代码。
con = new JPanel();
JButton bt = new JButton (" Insert ");
con.add (bt);
bt.addActionListener(this);
frame.add (con)
frame.setVisible(true);
@Override
public void actionPerformed(ActionEvent evt) {
radio = rb.getSelection().getActionCommand();
speed = "";
//extra code//
}
bt.removeActionListener(this);
public class Creaframe extends JFrame implements ActionListener {
public static JPanel con = new JPanel();
public static JFrame frame =new Creaframe();
public static String radio = "";
public static String speed = "";
ButtonGroup rb ;
public static void main(String[] args) {
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing (java.awt.event.WindowEvent windowEvent) {
if (JOptionPane.showConfirmDialog (frame,
" Vuoi veramente Uscire ?") ==
JOptionPane.YES_OPTION)
{
System.exit (0);
}
}
});
frame.setSize (1300, 900);
frame.setLocation (200,100);
con = new JPanel();
//con.setBackground(Color.YELLOW);
}
public Creaframe () {
super (" My program " );
//Your menubar related code ned to add here
}
public void button () {
JRadioButton rb1 = new JRadioButton ("CD");
rb1.setActionCommand("CD");
JRadioButton rb2 = new JRadioButton ("SACD");
rb2.setActionCommand("SACD");
JRadioButton rb3 = new JRadioButton ("Vinile",true);
rb3.setActionCommand("Vinile");
JRadioButton rb4 = new JRadioButton ("Vinile 180");
rb4.setActionCommand("180");
JRadioButton rb5 = new JRadioButton ("45 Giri");
rb5.setActionCommand("Y");
JRadioButton rb6 = new JRadioButton ("33 Giri",true);
rb6.setActionCommand("N");
rb= new ButtonGroup();
rb.add(rb1);
rb.add(rb2);
rb.add(rb3);
rb.add(rb4);
ButtonGroup rsp = new ButtonGroup();
rsp.add(rb5);
rsp.add(rb6);
con.add(rb1);
con.add(rb2);
con.add(rb3);
con.add(rb4);
JButton bt = new JButton (" Insert ");
con.add (bt);
bt.addActionListener(this);
frame.add (con);
frame.setVisible(true);
}
@Override
public void actionPerformed (ActionEvent event) {
radio = rb.getSelection().getActionCommand();
speed = "";
System.out.println (radio + " ok");
}
}