Results 1 to 17 of 17
Thread: Help in Radion Buttons
- 07-21-2012, 05:05 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
- 07-21-2012, 05:08 PM #2
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
-
Re: Help in Radion Buttons
If you're using Java 1.6 or 1.7, you could always call the ButtonGroup#clearSelection() method.
In the future, please post your real code here using code tags, not images, as it makes it much easier for us to copy, paste and test. Welcome to the java-forums, by the way!
- 07-22-2012, 12:34 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Help in Radion Buttons
sorry I don't know how to use code tags so I copied the whole codeJava Code:package rockpaperscissorsGUI; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; public class RockPaperScissors implements ActionListener { JFrame frame; JPanel panel , radio1panel , radio2panel; JLabel player1 , player2; JRadioButton rock , paper,scissors , rock1 , paper1 , scissors1; JButton winner , reset; Box box; JTextField name1,name2; public RockPaperScissors(){ frame = new JFrame("Rock, Paper, Scissors JUDGE!"); panel = new JPanel(); player1 = new JLabel("Player 1"); player2 = new JLabel("Player 2"); rock = new JRadioButton("Rock"); paper = new JRadioButton("Paper"); scissors = new JRadioButton ("Scissors"); rock1 = new JRadioButton("Rock"); paper1 = new JRadioButton("Paper"); scissors1 = new JRadioButton("Scissors"); winner = new JButton("Check the Winner"); reset = new JButton("Reset"); name1 = new JTextField("Player 1"); name2 = new JTextField("Player 2"); //name1.setSize(10,10); //name2.setSize(10,10); //Group the radio buttons. for player 1 ButtonGroup group = new ButtonGroup(); group.add(rock); group.add(paper); group.add(scissors); // group the radio buttons for player 2 ButtonGroup group2 = new ButtonGroup(); group2.add(rock1); group2.add(paper1); group2.add(scissors1); //group in a panel frame.add(panel); //panel.add(player1); panel.add(name1); panel.add(rock); panel.add(paper); panel.add(scissors); /* panel.add(radio2panel); radio2panel.add(rock1); radio2panel.add(paper1); radio2panel.add(scissors1); panel.add(radio1panel); radio1panel.add(rock); radio1panel.add(paper); radio1panel.add(scissors);*/ //panel.add(player2); panel.add(name2); panel.add(rock1); panel.add(paper1); panel.add(scissors1); panel.add(winner); winner.addActionListener(this); panel.add(reset); reset.addActionListener(this); rock.setMnemonic(KeyEvent.VK_R); paper.setMnemonic(KeyEvent.VK_P); scissors.setMnemonic(KeyEvent.VK_S); rock1.setMnemonic(KeyEvent.VK_O); paper1.setMnemonic(KeyEvent.VK_A); scissors1.setMnemonic(KeyEvent.VK_C); winner.setMnemonic(KeyEvent.VK_W); frame.setSize(300,150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args){ new RockPaperScissors(); } @Override public void actionPerformed(ActionEvent event) { String player1name , player2name; player1name = name1.getText(); player2name = name2.getText(); if (event.getSource()==winner) { if ((rock.isSelected()) && (paper1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((rock.isSelected()) && (scissors1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((rock.isSelected()) && (rock1.isSelected())) JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((paper.isSelected()) && (rock1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((paper.isSelected()) && (scissors1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((paper.isSelected()) && (paper1.isSelected())) JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((scissors.isSelected()) && (rock1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((scissors.isSelected()) && (scissors1.isSelected())) JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else if ((scissors.isSelected()) && (paper1.isSelected())) JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!" ,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(null, "Please Select from the options! \n Thank you" ,"Rock Paper and Scissors Judge", JOptionPane.ERROR_MESSAGE); } if (event.getSource()==reset) {name1.setText("Player 1"); name2.setText("Player 2"); /* rock.setSelected(false); rock1.setSelected(false); paper.setSelected(false); paper1.setSelected(false); scissors1.setSelected(false); scissors.setSelected(false);*/ //to be continued //group.setSelection //group.getSelectedJRadioButton(); /*if (scissors.isSelected()) scissors.setSelected(false);*/ } } }
I'm also new to GUI and I mad a judge for rock paper and scissors..
Is there another way besides clearSelection?Last edited by Fubarable; 07-22-2012 at 12:39 AM. Reason: code tags added
-
Re: Help in Radion Buttons
I've edited your post and have added code tags. To do this yourself in the future, simply highlight your code and press the code button at the top of the editor window, either that or surround your code with [code] [/code] tags.
First please tell us why you're dead set against using clearSelection(). It's the obvious solution to your problem, so why use a kludge without a good reason, and if you have a good reason, then please let us know?I'm also new to GUI and I mad a judge for rock paper and scissors..
Is there another way besides clearSelection?
- 07-22-2012, 01:38 AM #6
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
-
Re: Help in Radion Buttons
- 07-22-2012, 02:42 AM #8
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
-
Re: Help in Radion Buttons
This has nothing to do with Swing, nothing to do with ButtonGroups, and all to do with your variable being out of scope. Your current ButtonGroup variable, group, is declared in the class's constructor and is thus only visible inside of the constructor. Declare it in the class as a non-static field, make sure that you don't re-declare it in the constructor and then you'll be able to use it throughout the class.
- 07-22-2012, 06:54 AM #10
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Help in Radion Buttons
hmm can you show it via coding and where to place it in my code?.. I can't understand what you're saying.. I'm sorry i'm really new :D Thank you
-
Re: Help in Radion Buttons
Simply take ButtonGroup group = new ButtonGroup() outside of the constructor and put it in the class, that's it. If you declare it in the constructor, ButtonGroup group, then it is only visible in the scope in which it was declared, here the constructor. You need it visible outside of the constructor. Please google and read up on variable scope as you really need to fully understand this concept, not just the fix for this program.
- 07-22-2012, 07:13 AM #12
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Help in Radion Buttons
Ok thanks
- 07-22-2012, 07:32 AM #13
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Help in Radion Buttons
Now, it's working thanks..
-
Re: Help in Radion Buttons
You're welcome, but more importantly, do you understand the problem, because it's sure to come up again in a different program?
- 07-22-2012, 08:03 AM #15
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: Help in Radion Buttons
hmm just a little bit. but i'll read more about GUI can you recommend any links or ebooks??? I'm using this link to study about GUI Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) but I get more of the answers to my questions here :D
- 07-22-2012, 01:15 PM #16
Re: Help in Radion Buttons
You have to learn Java before you tackle Swing. Here are some references related to the issue Fubarable helped you with:
Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Chapter*6.*Names
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-22-2012, 02:28 PM #17
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Help....add actionListener to buttons
By jmeats77 in forum New To JavaReplies: 9Last Post: 05-24-2012, 01:11 AM -
Help with Buttons
By wld4ubabay in forum New To JavaReplies: 20Last Post: 05-17-2010, 08:13 AM -
Where To Get Pop Up Buttons
By hitmen in forum AWT / SwingReplies: 7Last Post: 03-26-2009, 04:05 AM -
Applet buttons
By h3ckf1r3 in forum Java AppletsReplies: 6Last Post: 09-22-2008, 09:15 PM -
How to use SWT Buttons
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote



Bookmarks