1 Attachment(s)
Simple Action/Listener Help GUI
Ok, I have been searching around for awhile now for examples on Action/Listeners for a JButton, and JtextField update. All of the examples I have found are too complex for me to follow.
So, basically I am making a program that has THREE textfields. You enter in a number of votes, and then click the Jbutton CALCULATE. that will trigger a class to check for the winner (have not made that yet). Then the radio box next to the winner will by checked, and the text "The winner is..." will update with the piece's number.
(see image below for GUI view)
So, all I am confused on is how to connect the JButton's action with the rest of the project, to update the winner, and the radio box to become checked.
(if you notice anything else I can do with the source code that would help, feel free to share)
Thanks for the help,
-Austin
Here's my source code:
Oh, and ignore that string to Int thing, it was just my fail way of trying to get something to work awhile back. :P I'm still new to JAVA.
Just cleaned the source code up a bit, and added some comments.
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.
*******************************************************************************************/
static int vote1 = 0, vote2 = 0, vote3 = 0;
static String vote1_string = "", vote2_string = "",
vote3_string = "", winner = null;
/*******************************************************************************************
* The main method calls the GUI.
*******************************************************************************************/
public static void main(String arg[]) {
ArtPrizeEngineGUI();
}
/*******************************************************************************************
* 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 static 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(400, 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();
JTextField textlabel4_in = new JTextField(5);
panel4.add(textlabel4_in);
textlabel4_in.setText("" + 0 + "");
vote1_string = textlabel4_in.getText();
vote1 = Integer.parseInt(vote1_string);
/*******************************************************************************************
* GUI for panel5. It creates the JTextfield and JPanel for
* piece 2's voting input.
*******************************************************************************************/
JPanel panel5 = new JPanel();
JTextField textlabel5_in = new JTextField(5);
panel5.add(textlabel5_in);
textlabel5_in.setText("" + 0 + "");
vote2_string = textlabel5_in.getText();
vote2 = Integer.parseInt(vote2_string);
/*******************************************************************************************
* GUI for panel6. It creates the JTextfield and JPanel for
* piece 3's voting input.
*******************************************************************************************/
JPanel panel6 = new JPanel();
JTextField textlabel6_in = new JTextField(5);
panel6.add(textlabel6_in);
textlabel6_in.setText("" + 0 + "");
vote3_string = textlabel6_in.getText();
vote3 = Integer.parseInt(vote3_string);
/*******************************************************************************************
* GUI for panel7. It creates the radio button field for piece
* 1.
*******************************************************************************************/
JPanel panel7 = new JPanel();
JRadioButton button1 = new JRadioButton("", false);
panel7.add(button1);
/*******************************************************************************************
* GUI for panel8. It creates the radio button field for piece
* 2.
*******************************************************************************************/
JPanel panel8 = new JPanel();
JRadioButton button2 = new JRadioButton("", false);
panel8.add(button2);
/*******************************************************************************************
* GUI for panel9. It creates the radio button field for piece
* 3.
*******************************************************************************************/
JPanel panel9 = new JPanel();
JRadioButton button3 = new JRadioButton("", false);
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".
*******************************************************************************************/
JButton button;
JPanel panel_button = new JPanel();
button = new JButton("Calculate");
panel_button.add(button);
// button.addActionListener(this);
/*******************************************************************************************
* The JLabel below creates a JLbael that displays who the
* winner is.
*******************************************************************************************/
JLabel jtextlabel_winner = new JLabel(
" And the winner is... " + winner);
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 void actionPerformed(ActionEvent e) {
if (vote1 > vote2) {
if (vote1 > vote3) {
winner = "piece 1!";
}
}
if (vote2 > vote1) {
if (vote2 > vote3) {
winner = "piece 2!";
}
}
if (vote3 > vote1) {
if (vote3 > vote2) {
winner = "piece 3!";
}
}
if (vote1 == vote3) {
winner = "piece 1 and piece 3 tied!";
}
if (vote1 == vote2) {
winner = "piece 1 and piece 2 tied!";
}
if (vote2 == vote3) {
winner = "piece 2 and piece 3 tied!";
}
if (vote1 == vote2 && vote1 == vote3) {
winner = "piece 1, 2 and 3 tied!";
}
}
}
Also, I have this image below "Art Prize Voter", that is what the display looks like.
Is there a simple way to make the space between the JLabel, Jtextfield, and Radio Buttons closer together? As you can see they are spread quite far apart.
Thanks again!
My static void main(String[] args) error
I am looking over your questions
and I will make more suggestions
on how to bring order to your code.
In my first post, I accidentally included
"static void main(String[] args)"
as one of the lines you should remove
"static" from.
This was a mistake.
The "main(String[] args)" method is
the 'launching point' for your code, and
it is ALWAYS static.