Results 21 to 33 of 33
Thread: Java help...Checkboxes
- 10-25-2010, 04:09 AM #21
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Here is my new code that I changed! Can I just add the "tie" scenarios in with the other statement that I will post? Is there a way to do that, and if so could someone help me out with that?
Java Code:String setText = null; if (p1 == p2) { if (p1 == p3) setText = "It's a tie between pieces 1,2 and 3"; else setText = "It's a tie between pieces 1 and 2"; } else if (p1 == p3) setText = "It's a tie between pieces 1 and 3"; else if (p3 == p2) setText = "It's a tie between pieces 2 and 3"; if (setText == null) { String winMsg = null; if (p1 > p2 && p1 > p3) { winMsg = "Piece 1 is the winner"; box1.setSelected(true); } else if (p2 > p1 && p2 > p3) { winMsg = "Piece 2 is the winner"; box2.setSelected(true); } else if (p3 > p1 && p3 > p2) { winMsg = "Piece 3 is the winner"; box3.setSelected(true); } result.setText(winMsg); {
- 10-25-2010, 06:08 AM #22
I'm not quite sure I understand what you mean. You want to merge the tie scenarios into the winning scenario statements?
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 06:18 AM #23
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Well I tried what you did, and it woked, but then my statements that just said "piece 1 is the winner" would not show up and none of the check boxes worked! So i'm not quite sure what happened?
- 10-25-2010, 06:26 AM #24
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Here is my code so you see what I mean!!
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String setText = null; if (p1 == p2) { if (p1 == p3) setText = "It's a tie between pieces 1,2 and 3"; else setText = "It's a tie between pieces 1 and 2"; } else if (p1 == p3) setText = "It's a tie between pieces 1 and 3"; else if (p3 == p2) setText = "It's a tie between pieces 2 and 3"; if (setText == null) { if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else if (p2 > p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } result.setText(setText); { { } } } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-25-2010, 06:37 AM #25
This is what your code looks like now for decided the winner, there is a problem with all of them like it:
Java Code:if(p1>p3) { result.setText("Piece 1 is the winner"); // Here is the problem line box1.setSelected(true); }
After this code executes you use the following statement that is overriding the code above so that null is being displayed:
Java Code:result.setText(setText);
Inside the "if" statement, why not just assign setText the output string instead of printing to the screen at that specific time. And to add check boxes to the tie scenarios just use the same code you used for the winning pieces except check both boxes.Last edited by joshdgreen; 10-25-2010 at 06:39 AM.
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 06:42 AM #26
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
So is there a way I can fix this...I am so confused and this project is due later today, so I am straped for time right now. I just need all the statements to work along with the checkboxes. I know it can't be too hard but for some reason I am struggling with this!! here is what I have for my code RIGHT NOW!
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String setText = null; if (p1 == p2) { if (p1 == p3) setText = "It's a tie between pieces 1,2 and 3"; else setText = "It's a tie between pieces 1 and 2"; } else if (p1 == p3) setText = "It's a tie between pieces 1 and 3"; else if (p3 == p2) setText = "It's a tie between pieces 2 and 3"; if (setText == null) { if(p1>p2) { if(p1>p3) { result.setText("Piece 1 is the winner"); box1.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } else if (p2 > p3) { result.setText("Piece 2 is the winner"); box2.setSelected(true); } else { result.setText("Piece 3 is the winner"); box3.setSelected(true); } } result.setText(setText); { { } } } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-25-2010, 06:44 AM #27
You didn't change anything, no one will help if you show no effort to even change anything when given help.
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 06:48 AM #28
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I understand that I need to change it, but that's where I am stuck. I have been at this for days and tried a countless number of ways at yet have come up with the correct way!!
- 10-25-2010, 06:54 AM #29
I told you the line you need to change, make this:
Java Code:result.setText("Piece 1 is the winner");
Look like this:
Java Code:setText = "It's a tie between pieces 1,2 and 3";
Also, the formatting of your code is very confusing. You should work on that and maybe walk through your code line by line and do a hand check on paper and pencil as to what things are doing and why they are doing them. And it's not our fault your code is due and you aren't finished.Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 07:04 AM #30
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I never said it's your fault that the code is due. I have a full time job, and I also go to school full time, so I have lots of priorities to take care of. I was just asking for some simple help. That is all!
- 10-25-2010, 07:39 AM #31
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
I have this for my "tie" statements but it's not working. Can anybody help
Java Code:String winMsg = null; if (p1 > p2 && p1 > p3) { winMsg = "Piece 1 is the winner"; box1.setSelected(true); if (p1 == p2 && p2 == p3) { winMsg = "It's a tie between pieces 1, 2, 3"; box1.setSelected(true); box2.setSelected(true); box3.setSelected(true); } } else if (p2 > p1 && p2 > p3) { winMsg = "Piece 2 is the winner"; box2.setSelected(true); if (p1 == p2) winMsg = "It's a tie between piece 1 and 2"; box1.setSelected(true); box2.setSelected(true); if (p2 == p3) winMsg = "It's a tie between piece 2 and 3"; box2.setSelected(true); box3.setSelected(true); } else if (p3 > p1 && p3 > p2) { winMsg = "Piece 3 is the winner"; box3.setSelected(true); if (p1 == p3) winMsg = "It's a tie between piece 1 and 3"; box1.setSelected(true); box3.setSelected(true); } result.setText(winMsg);
Here is my full code
Java Code:import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.awt.*; import javax.swing.JFrame; import javax.swing.JCheckBox; import java.awt.FlowLayout; class ArtPrizeDemo extends JFrame { private JPanel panel; // A holding panel private JLabel messageLabel1; // A message to the user private JLabel messageLabel2; // A message to the user private JLabel messageLabel3; // A message to the user private JLabel result; // A message to the user private JTextField piece1; // To hold user input private JTextField piece2; // To hold user input private JTextField piece3; // To hold user input private JCheckBox box1; private JCheckBox box2; private JCheckBox box3; private JButton calculate; private final int WINDOW_WIDTH = 400; // Window width private final int WINDOW_HEIGHT = 200; // Window height int num; public ArtPrizeDemo() { // Set the title. setTitle("Art Prize"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame. buildPanel(); // Add the panel to the frame's content pane. add(panel); // Display the window. setVisible(true); } private void buildPanel() { panel= new JPanel(); messageLabel1 = new JLabel("Enter the number of votes for piece 1:"); piece1 = new JTextField(10); box1 = new JCheckBox(" "); messageLabel2 = new JLabel("Enter the number of votes for piece 2:"); piece2 = new JTextField(10); box2 = new JCheckBox(" "); messageLabel3 = new JLabel("Enter the number of votes for piece 3:"); piece3 = new JTextField(10); box3 = new JCheckBox(" "); calculate= new JButton("Calculate"); panel.add(messageLabel1); panel.add(piece1); panel.add(box1); panel.add(messageLabel2); panel.add(piece2); panel.add(box2); panel.add(messageLabel3); panel.add(piece3); panel.add(box3); calculate.addActionListener(new calButtonListener()); panel.add(calculate); result = new JLabel(" "); panel.add(result); } private class calButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String in; int p1,p2,p3; in=piece1.getText(); p1=Integer.parseInt(in); in=piece2.getText(); p2=Integer.parseInt(in); in=piece3.getText(); p3=Integer.parseInt(in); String winMsg = null; if (p1 > p2 && p1 > p3) { winMsg = "Piece 1 is the winner"; box1.setSelected(true); if (p1 == p2 && p2 == p3) { winMsg = "It's a tie between pieces 1, 2, 3"; box1.setSelected(true); box2.setSelected(true); box3.setSelected(true); } } else if (p2 > p1 && p2 > p3) { winMsg = "Piece 2 is the winner"; box2.setSelected(true); if (p1 == p2) winMsg = "It's a tie between piece 1 and 2"; box1.setSelected(true); box2.setSelected(true); if (p2 == p3) winMsg = "It's a tie between piece 2 and 3"; box2.setSelected(true); box3.setSelected(true); } else if (p3 > p1 && p3 > p2) { winMsg = "Piece 3 is the winner"; box3.setSelected(true); if (p1 == p3) winMsg = "It's a tie between piece 1 and 3"; box1.setSelected(true); box3.setSelected(true); } result.setText(winMsg); } } { { } } } class ArtPrize { public static void main(String[] args) { ArtPrizeDemo frm = new ArtPrizeDemo(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(350,100); frm.setVisible(true); } }
- 10-25-2010, 12:11 PM #32
You're missing brackets {}.
Sincerely, Joshua Green
Please REP if I help :)
- 10-25-2010, 12:39 PM #33
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Try making it less confusing
1. Check for an individual win
2. Check for tie between two pieces
3. Check for tie between all pieces
Use separate if's for all of the above; makes code less messy
im going to give a sample of checking for tie between two pieces:
*note the check that the tied pieces are greater than the other piece...
Java Code:if(p3==p2 && p3>p1){ winMsg = "It is a tie between Piece 2 and 3"; box2.setSelected(true); box3.setSelected(true); }Last edited by al_Marshy_1981; 10-25-2010 at 12:41 PM. Reason: horrible indent
Similar Threads
-
ComboBox with CheckBoxes
By heba.farouk in forum AWT / SwingReplies: 14Last Post: 06-09-2010, 11:03 AM -
CheckBoxes and JTables
By lakshayghai in forum AWT / SwingReplies: 1Last Post: 03-16-2010, 08:01 PM -
How to use Mnemonic for CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:45 PM -
How to use Swing CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:44 PM -
Problem with checkboxes
By carl in forum Java AppletsReplies: 1Last Post: 08-06-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks