Results 1 to 9 of 9
Thread: Help with IF Statements and GUI
- 10-13-2010, 08:52 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Help with IF Statements and GUI
I am having some trouble with making a GUI and IF statements.
I need to be able to enter a number of votes for 3 particular "Art Piece's" and hit a calculate button and have the program put a checkmark next to the winner. Once the user presses the "calculate" button, the program will update the window to indicate which piece received the most votes. Ex) "piece 2 is the winner, or if there is a tie: "it is a tie between pieces 1, 2, 3".
I have some starting code here but I am unsure of how to do if statements very well. If anyine can give me some direction I would really appreciate it.
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
public class ArtPrize
{
public static void main (String[] args)
{
private JLabel inputLabel, inputLabel2, inputLabel3, resultLabel;
private JTextField ArtPrize;
public ArtPrize()
{
inputLabel = new JLabel ("Enter the number of votes for piece 1:");
inputLabel2 = new JLabel ("Enter the number of votes for piece 2:");
inputLabel3 = new JLabel ("Enter the number of votes for piece 3:");
resultLabel = new JLabel ("-----");
ArtPrize = new JTextField (5);
ArtPrize.addActionListener (new TempListener());
add (inputLabel);
add (inputLabel2);
add (inputLabel3);
add (resultLabel);
setPreferredSize (new Dimension(300, 200));
setBackground (Color.white);
}
private class ArtListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int
JFrame frame = new JFrame ("Art Prize");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ArtPrizePanel = new ArtPrizePanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
- 10-13-2010, 10:41 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Does this look right for one of the IF statements? I need to enter any number into all 3 of the voting sections and have the winning art piece displayed with a check mark next to it.
int piece1, piece2, piece3, num = 0;
Scanner scan = new Scanner (System.in);
piece1 = scan.nextInt();
piece2 = scan.nextInt();
piece3 = scan.nextInt();
if (piece1 > piece2)
if (piece1 > piece3)
num = piece1;
- 10-13-2010, 10:51 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Would this be a good way to handle it then? What about if there is a tie?
if(piece1>piece2 && piece1>piece3)
num = piece1;
if(piece2>piece3 && piece2>piece1)
num = piece2;
if(piece3>piece1 && piece3>piece2)
num = piece3;
-
- 10-14-2010, 12:13 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
No it will not compile, that's what I need help with!
- 10-14-2010, 12:29 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
If you can't understand the compiler message copy and post the exact and entire message.
-
You are trying to place your class's constructor inside of a method, the main method, and this won't work as constructors and methods can't be directly inside of other methods or constructors. So get it out. If it were my code, I'd restart from scratch.
Next time, please let us know what errors you're having so we don't have to guess.
- 10-14-2010, 03:36 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Where it is bolded it is saying illegal start of an expression.
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class ArtPrize
{
public static void main (String[] args)
{
int piece1, piece2, piece3, num = 0;
Scanner scan = new Scanner (System.in);
piece1 = scan.nextInt();
piece2 = scan.nextInt();
piece3 = scan.nextInt();
if(piece1>piece2 && piece1>piece3)
num = piece1;
if(piece2>piece3 && piece2>piece1)
num = piece2;
if(piece3>piece1 && piece3>piece2)
num = piece3;
private JLabel inputLabel, inputLabel2, inputLabel3, resultLabel;
private JTextField ArtPrize;
public ArtPrize()
{
inputLabel = new JLabel ("Enter the number of votes for piece 1:");
inputLabel2 = new JLabel ("Enter the number of votes for piece 2:");
inputLabel3 = new JLabel ("Enter the number of votes for piece 3:");
resultLabel = new JLabel ("-----");
ArtPrize = new JTextField (5);
ArtPrize.addActionListener (new ArtPrizeListener());
add (inputLabel);
add (inputLabel2);
add (inputLabel3);
add (resultLabel);
setPreferredSize (new Dimension(300, 200));
setBackground (Color.white);
}
private class ArtListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int
JFrame frame = new JFrame ("Art Prize");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ArtPrizePanel = new ArtPrizePanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
-
Similar Threads
-
Help with if else statements
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 02:25 AM -
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 08:02 PM -
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 10:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 05:24 PM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 08:56 PM
Bookmarks