Problem with JText Fields and using action listener
Code Java:
/**
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class Artpanel extends JPanel
{
private JButton calculate;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField input1;
private JTextField input2;
int piece1;
int piece2;
/**
* Constructor for objects of class ArtprizePanel
**/
public Artpanel()
{
label1 = new JLabel ("Enter amount of votes for piece 1:");
label2 = new JLabel ("Enter amount of votes for piece 2:");
input1 = new JTextField (5);
input2 = new JTextField (5);
calculate.addActionListener (new ButtonListener());
add (label1);
add (input1);
add (label2);
add (input2);
add (calculate);
add (label3);
setPreferredSize (new Dimension(400, 100));
setBackground (Color.blue);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
this is what I have so far. I need to be able to use the numbers input in to the two jtext fields, say 80 and 70, and use an action listener with if statements to have the program say 80 was the winner. i'm quite new at this stuff, so i'm really confused. im pretty sure i need to set two global integers and have a listener for each text field then set those text fields equal to those integers, then write the if statements. i'm just lost on how to do this.
Re: Problem with JText Fields and using action listener
Quote:
Originally Posted by
toble
Code :
/**
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class Artpanel extends JPanel
{
private JButton calculate;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField input1;
private JTextField input2;
int piece1;
int piece2;
/**
* Constructor for objects of class ArtprizePanel
**/
public Artpanel()
{
label1 = new JLabel ("Enter amount of votes for piece 1:");
label2 = new JLabel ("Enter amount of votes for piece 2:");
input1 = new JTextField (5);
input2 = new JTextField (5);
calculate.addActionListener (new ButtonListener());
add (label1);
add (input1);
add (label2);
add (input2);
add (calculate);
add (label3);
setPreferredSize (new Dimension(400, 100));
setBackground (Color.blue);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
this is what I have so far. I need to be able to use the numbers input in to the two jtext fields, say 80 and 70, and use an action listener with if statements to have the program say 80 was the winner. i'm quite new at this stuff, so i'm really confused. im pretty sure i need to set two global integers and have a listener for each text field then set those text fields equal to those integers, then write the if statements. i'm just lost on how to do this.
It seems that you've never defined calculate.
Perhaps
calculate = new JButton("Calculate");
might solve the problem.
Also, nothing seems to be setVisible(true);
And you never imported Color.
Also,
Code java:
/**
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
public class Artpanel extends JPanel
{
private JButton calculate;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField input1;
private JTextField input2;
int piece1;
int piece2;
/**
* Constructor for objects of class ArtprizePanel
**/
public Artpanel()
{
setVisible(true);
// or maybe just setVisible(); can't recall which
label1 = new JLabel ("Enter amount of votes for piece 1:");
label2 = new JLabel ("Enter amount of votes for piece 2:");
input1 = new JTextField (5);
// also, you might be able to ditch the JLabels by doing this
// input1.setToolTipText("Enter amount of votes for piece 1:");
input2 = new JTextField (5);
piece1 = Integer.parseInt(input1.getText());
piece2 = Integer.parseInt(input2.getText());
calculate = new JButton("Calculate");
calculate.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
add (label1);
label.setVisible(true);
add (input1);
input1.setVisible(true);
add (label2);
label2.setVisible(true);
add (input2);
input2.setVisible(true);
add (calculate);
calculate.setVisible(true);
add (label3);
label3.setVisible(true);
setPreferredSize (new Dimension(400, 100));
setBackground (Color.BLUE);
}
}
I think that you may have to have ButtonListener in the declarations before your constructor or else it won't be able to get it if I do it the other way.
Re: Problem with JText Fields and using action listener
Code :
/**
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
public class Artpanel extends JPanel
{
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField input1;
private JTextField input2;
private JButton calculate;
int piece1 = 0;
int piece2 = 0;
/**
* Constructor for objects of class ArtprizePanel
**/
public Artpanel()
{
label1 = new JLabel ("Enter amount of votes for piece 1:");
label2 = new JLabel ("Enter amount of votes for piece 2:");
label3 = new JLabel ("---");
calculate = new JButton ("Calculate");
calculate.addActionListener (new ButtonListener());
input1 = new JTextField (5);
input2 = new JTextField (5);
add (label1);
add (input1);
add (label2);
add (input2);
add (calculate);
add (label3);
setPreferredSize (new Dimension(400, 100));
setBackground (Color.yellow);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text = input1.getText();
String text1 = input2.getText();
piece1 = Integer.parseInt (text);
piece2 = Integer.parseInt (text1);
}
}
}
okay here's what i have now. i got the thing to launch, but now i need to figure out how to set an if else statement to determine whether piece 1 or piece 2 is bigger, then display appropriate text in label 3 declaring which one is greater. how would i go about that?