|
In this program I have to write a number in a text field and then choose either if I want to press an button or press enter to get the results. No matter what I try I can't connect the button "push" to the text field. I both post my scripth panel and the program and hopefully someone can help me :)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FahrenheitModifyPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
private JButton push;
public FahrenheitModifyPanel()
{
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener (new TempListener());
push = new JButton ("push");
add(push);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
------------------------------------
import javax.swing.JFrame;
public class FahrenheitModify {
// -----------------------------------------------------------------
// Creates and displays the temperature converter GUI.
// -----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FahrenheitModifyPanel panel = new FahrenheitModifyPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
|
|
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FTest extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
private JButton push;
public FTest()
{
// initialize components
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
Dimension d = resultLabel.getPreferredSize();
d.width = 35;
resultLabel.setPreferredSize(d);
fahrenheit = new JTextField (5);
push = new JButton ("push");
// add listeners
TempListener l = new TempListener();
fahrenheit.addActionListener (l);
push.addActionListener(l);
// layout components
add(push);
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text = fahrenheit.getText();
int fahrenheitTemp = Integer.parseInt (text);
int celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FTest panel = new FTest();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
|
|
|
|
|
|
|
|
|
|