Results 1 to 9 of 9
  1. #1
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default 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);



    }
    }

  2. #2
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    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;

  3. #3
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    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;

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by university123 View Post
    I am having some trouble with making a GUI and IF statements.

    ....

    Java Code:
    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;
    
             // [color="red"][b]**** this compiles? ****[/b][/color]
             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 ("-----");
             //......

    Your code above won't compile. Please post real compilable code and use code tags so that it will retain its formatting. To see how to do the latter, please see my first signature link immediately below.

  5. #5
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    No it will not compile, that's what I need help with!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If you can't understand the compiler message copy and post the exact and entire message.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by university123 View Post
    No it will not compile, that's what I need help with!
    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.

  8. #8
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    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);



    }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again:
    Quote Originally Posted by Fubarable View Post
    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.
    Again, scrap this code and restart with code where you don't try to have a constructor inside of a method. Seriously, this code is totally messed up. Get rid of it.

Similar Threads

  1. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 01:25 AM
  2. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  3. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  4. Help with if-else statements
    By porchrat in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 04:24 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •