Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    The truth, I have absolutely no idea
    what you want the buttons in your
    GUI to do.

    Your primary concerns where how to
    program an actionListener, and how
    to get global access to the variables.

    Those are some programming concepts
    I hope I have made clear in this thread.

    If you don't like what I have the radiobuttons
    doing, change it. That would be a day to day,
    minute by minute decision you would be making
    when programming any algorithm into code.


    I also was checking something, and so I ran the debugger, and the program never went into the actionpreformed class to check my if statements, it just stopped and displayed the GUI after going through ArtPrizeGUI class.

    I have run the code and it is working just fine.
    Last edited by paul pasciak; 10-18-2010 at 04:38 PM.

  2. #22
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Again, sorry if I seem ansy, the project is due in two days, and I know this is super easy, but I just don't get this basic concept yet...so I can't really do too much until I full understand this action listener stuff. You have been by the far greatest help though.


    Here is the actual project details, (scroll down for the "A Grade" part).
    The PDF was bigger then there upload size allowed, so I stuck it on MediaFire
    Project 3.pdf


    Thanks again thus far,
    -Austin

  3. #23
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Yes, I see some concepts of the action/listener, my biggest issue, is seeing HOW the JButton is actually running the actionpreformed class, and then how the variables in the actionpreformed get UPDATED within the GUI.

    My issue, is seeing how this actually works...I'm more of a visual person and so I know this hard to explain via words.

  4. #24
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I know I can do this, JAVA is easy so long as you have the basic building blocks. I know logic, logic is easy, but until I grasp this action listener stuff, I'm pretty much useless at this point. The second I see how this works, the whole project will just click in my head. And I feel like I'm getting close, and your the only helpful person I've meet regarding this issue.

  5. #25
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I GOT IT!!!!!! One of my friends helped cause I made some very stupid errors. The reason I couldn't get my buttons to work at first was because I was redefining things (which compiled) but overwrote key variables that linked the code.

    But anywho, here is the new code I have, which works, and my prof approved it for an A. ;)
    Thank you SOOOOOO MUCH PAUL!!!! and everyone else, you really helped me a lot, I will for sure use/contribute to this forum from now on.

    Thanks so MUCH!
    -Austin

    Java Code:
    import java.awt.*;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.*;
    import javax.swing.*;
    
    /*******************************************************************************************
     * @author Austin Anderson This program will take in the votes of three
     *         art pieces, and then calculate the winner.
     *******************************************************************************************/
    public class ArtPrizeEngine {
    
    	/*******************************************************************************************
    	 * Contains the variables used to track the votes for when finding
    	 * the winner.
    	 *******************************************************************************************/
    	int vote1 = 0, vote2 = 0, vote3 = 0;
    
    	String vote1_string = "", vote2_string = "", vote3_string = "",
    			winner = null;
    
    	JLabel jtextlabel_winner = new JLabel("     And the winner is... "
    			+ winner);
    
    	JRadioButton button1 = new JRadioButton("", false);
    
    	JRadioButton button2 = new JRadioButton("", false);
    
    	JRadioButton button3 = new JRadioButton("", false);
    
    	JTextField textlabel4_in = new JTextField(5);
    
    	JTextField textlabel5_in = new JTextField(5);
    
    	JTextField textlabel6_in = new JTextField(5);
    
    	JButton button;
    
    
    	/*******************************************************************************************
    	 * The main method calls the GUI.
    	 *******************************************************************************************/
    	public static void main(String arg[]) {
    		ArtPrizeEngine t = new ArtPrizeEngine();
    		t.ArtPrizeEngineGUI();
    		System.out.println("Hello1");
    	}
    
    
    	/*******************************************************************************************
    	 * The GUI below is the engine of the program, and the GUI itself.
    	 * It will display the voting GUI, count the entered votes, and
    	 * then calculate the winner by displaying text and checking the
    	 * appropriate radio box.
    	 *******************************************************************************************/
    	public void ArtPrizeEngineGUI() {
    		/*******************************************************************************************
    		 * The frame and the three main panels used to build the
    		 * overall GUI, are created here.
    		 *******************************************************************************************/
    		JFrame frame = new JFrame("Art Prize Voter!");
    		JPanel panelmain = new JPanel();
    		JPanel panelmain_votes = new JPanel();
    		JPanel panelmain_button = new JPanel();
    		frame.setPreferredSize(new Dimension(475, 175));
    		/*******************************************************************************************
    		 * GUI for panel1. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 1.
    		 *******************************************************************************************/
    		JPanel panel1 = new JPanel();
    		JLabel jtextlabel1 = new JLabel(
    				"Enter your votes for piece # 1: ");
    		panel1.add(jtextlabel1);
    		/*******************************************************************************************
    		 * GUI for panel2. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 2.
    		 *******************************************************************************************/
    		JPanel panel2 = new JPanel();
    		JLabel jtextlabel2 = new JLabel(
    				"Enter your votes for piece # 2: ");
    		panel2.add(jtextlabel2);
    		/*******************************************************************************************
    		 * GUI for panel3. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 3.
    		 *******************************************************************************************/
    		JPanel panel3 = new JPanel();
    		JLabel jtextlabel3 = new JLabel(
    				"Enter your votes for piece # 3: ");
    		panel3.add(jtextlabel3);
    		/*******************************************************************************************
    		 * GUI for panel4. It creates the JTextfield and JPanel for
    		 * piece 1's voting input.
    		 *******************************************************************************************/
    		JPanel panel4 = new JPanel();
    		panel4.add(textlabel4_in);
    		textlabel4_in.setText("0");
    		/*******************************************************************************************
    		 * GUI for panel5. It creates the JTextfield and JPanel for
    		 * piece 2's voting input.
    		 *******************************************************************************************/
    		JPanel panel5 = new JPanel();
    		panel5.add(textlabel5_in);
    		textlabel5_in.setText("0");
    		/*******************************************************************************************
    		 * GUI for panel6. It creates the JTextfield and JPanel for
    		 * piece 3's voting input.
    		 *******************************************************************************************/
    		JPanel panel6 = new JPanel();
    		panel6.add(textlabel6_in);
    		textlabel6_in.setText("0");
    		/*******************************************************************************************
    		 * GUI for panel7. It creates the radio button field for piece
    		 * 1.
    		 *******************************************************************************************/
    		JPanel panel7 = new JPanel();
    		panel7.add(button1);
    		/*******************************************************************************************
    		 * GUI for panel8. It creates the radio button field for piece
    		 * 2.
    		 *******************************************************************************************/
    		JPanel panel8 = new JPanel();
    		panel8.add(button2);
    		/*******************************************************************************************
    		 * GUI for panel9. It creates the radio button field for piece
    		 * 3.
    		 *******************************************************************************************/
    		JPanel panel9 = new JPanel();
    		panel9.add(button3);
    		/*******************************************************************************************
    		 * The panelmain_votes, holds all of the panels above, creating
    		 * the "top" portion of the GUI. Panels are organized in their
    		 * respective sets for the Jlabel, textfield, and radio button.
    		 * Panels 1, 4, and 7 are the components for piece 1. Panels 2,
    		 * 5, and 8 are the components for piece 2. Panels 3, 6, and 9
    		 * are the components for piece 3.
    		 *******************************************************************************************/
    		panelmain_votes.setLayout(new GridLayout(3, 3));
    		panelmain_votes.add(panel1);
    		panelmain_votes.add(panel4);
    		panelmain_votes.add(panel7);
    		panelmain_votes.add(panel2);
    		panelmain_votes.add(panel5);
    		panelmain_votes.add(panel8);
    		panelmain_votes.add(panel3);
    		panelmain_votes.add(panel6);
    		panelmain_votes.add(panel9);
    		/*******************************************************************************************
    		 * GUI for panelmain_button. It creates the button "Calculate".
    		 *******************************************************************************************/
    
    		JPanel panel_button = new JPanel();
    		button = new JButton("Calculate");
    		panel_button.add(button);
    		button.addActionListener(new e());
    		/*******************************************************************************************
    		 * The JLabel below creates a JLabel that displays who the
    		 * winner is.
    		 *******************************************************************************************/
    
    		panel_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * The panelmain_button holds the Calculate button as well as
    		 * the winner text and creates the "bottom" portion of the
    		 * overall GUI.
    		 *******************************************************************************************/
    		panelmain_button.setLayout(new GridLayout(1, 2));
    		panelmain_button.add(button);
    		panelmain_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * panelmain adds the "top" section of the GUI
    		 * (panelmain_votes), with the "bottom" section of the GUI
    		 * (panelmain_button); to create the overall GUI.
    		 *******************************************************************************************/
    		panelmain.add(panelmain_votes);
    		panelmain.add(panelmain_button);
    		/*******************************************************************************************
    		 * panelmain is added to the frame here, and is then packed,
    		 * and set visible for client use.
    		 *******************************************************************************************/
    		frame.getContentPane().add(panelmain);
    		frame.pack();
    		frame.setVisible(true);
    
    	}
    
    	public class e implements ActionListener {
    
    		public void actionPerformed(ActionEvent event) {
    			System.out.println("Hello");
    
    			vote1_string = textlabel4_in.getText();
    			vote1 = Integer.parseInt(vote1_string);
    			vote2_string = textlabel5_in.getText();
    			vote2 = Integer.parseInt(vote2_string);
    			vote3_string = textlabel6_in.getText();
    			vote3 = Integer.parseInt(vote3_string);
    
    			if (vote1 > vote2) {
    				if (vote1 > vote3) {
    					jtextlabel_winner
    							.setText("  The winner is: piece 1!");
    					button1.setSelected(true);
    					button2.setSelected(false);
    					button3.setSelected(false);
    				}
    			}
    			if (vote2 > vote1) {
    				if (vote2 > vote3) {
    					jtextlabel_winner
    							.setText("  The winner is: piece 2!");
    					button1.setSelected(false);
    					button2.setSelected(true);
    					button3.setSelected(false);
    				}
    			}
    			if (vote3 > vote1) {
    				if (vote3 > vote2) {
    					jtextlabel_winner
    							.setText("  The winner is: piece 3!");
    					button1.setSelected(false);
    					button2.setSelected(false);
    					button3.setSelected(true);
    				}
    			}
    			if (vote1 == vote3) {
    				jtextlabel_winner
    						.setText("  Piece 1 and piece 3 tied!");
    				button1.setSelected(true);
    				button2.setSelected(false);
    				button3.setSelected(true);
    			}
    			if (vote1 == vote2) {
    				jtextlabel_winner
    						.setText("  Piece 1 and piece 2 tied!");
    				button1.setSelected(true);
    				button2.setSelected(true);
    				button3.setSelected(false);
    			}
    			if (vote2 == vote3) {
    				jtextlabel_winner
    						.setText("  Piece 2 and piece 3 tied!");
    				button1.setSelected(false);
    				button2.setSelected(true);
    				button3.setSelected(true);
    			}
    			if (vote1 == vote2 && vote1 == vote3) {
    				jtextlabel_winner.setText("  Piece 1, 2 and 3 tied!");
    				button1.setSelected(true);
    				button2.setSelected(true);
    				button3.setSelected(true);
    			}
    
    		}
    	}
    
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  2. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 09:17 PM
  3. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  4. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 AM
  5. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 02:02 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
  •