![](/img/trans.png)
[英]When testing JButton my lambda expression works, but actionPerformed does not
[英]My JButton does not react with ActionListener and actionPerformed
作为我项目的一部分,我编写了这部分代码,当按下Button时应该采取行动。 它正常工作,直到我添加了一个新代码,即设置为“”的变量。 现在它没有用。 我的意思是,代码不会等到按下按钮(我也放了一个标志,在控制台中写了一个“确定”,但似乎actionPerformed没有启动。出了什么问题?我找不到任何理由为什么它不应该等一下按钮。谢谢
con = new JPanel();
JButton bt = new JButton (" Insert ");
con.add (bt);
frame.add (con)
frame.setVisible(true);
bt.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
radio = rb.getSelection().getActionCommand();
speed = "";
}
}
);
我添加了一些代码,如下所示,我希望它有所帮助。 我还有些麻烦, 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();
}
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");
ButtonGroup 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);
txt11.setVisible(false);
frame.setVisible(true);
@Override
public void actionPerformed (ActionEvent event) {
**// this method is in fatal error, here below the message.
// Multiple markers at this line
// - Syntax error on token "(", ; expected
// - void is an invalid type for the variable
// actionPerformed
// - Syntax error on token ")", ; expected**
radio = rb.getSelection().getActionCommand();
speed = "";
System.out.println (radio + " ok");
}
试试下面的代码,
使用ActionListerner实现,您不需要使用匿名consturctor多次编写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的链接单击此处
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.