as part of my project, I wrote this part of code that should take an action when a Button is pressed. It worked correctly until I added a new code, that is the variable set to "". Now it did not work. I mean, the code does not wait until the button is pressed (I also put a flag , writing an "ok" in console, but it seem the the actionPerformed does not start. What's wrong ? I cannot find any reason why it should not wait the button. Thanks
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 = "";
}
}
);
I added some more code, here below, I hope it helps. I still some trouble, the ActionPerformed
method shows an error. Details in the code
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");
}
Try out below code,
using ActionListerner implemenation you dont need to write actionperformed method multiple time using anonymous consturctor. You can have one actionPerformed method for handle multiple events
1) implements ActionListener interface in your class
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//
}
and you can remove listener by using below method.
bt.removeActionListener(this);
EDIT Added the complete class for your help,I am consider that you have proper implementation of your constructor to get MenuBar
check link for creating JMenubar Click here
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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.