Java温度转换摇摆
问题描述:
学习,善待...第一次摇摆桂。我正在尝试使用SWING来制作这个温度转换GUI。一般框架似乎工作,按钮是我想要的地方,等我的问题是与按钮...我不能让他们读取温度字段,并显示在不可编辑的文本字段。我有工作的AWT版本,但我现在正在尝试这样做。Java温度转换摇摆
public class HandleEvent extends JFrame {
public double fCalc;
public double cCalc;
public double kCalc;
public Object kTextField = kCalc;
public Object fTextField = fCalc;
public Object cTextField = cCalc;
public Object temperature = new JTextField(12);
public HandleEvent() {
Font font1 = new Font("Arial", Font.BOLD, 12);
//Flow layout setup
setLayout(new FlowLayout(FlowLayout.LEADING,10,20));
//Temperature label and text field
JTextField temperature = new JTextField(12);
add(new JLabel("Temperature: "));
temperature.setBackground(Color.WHITE);
add(temperature);
//Fahrenheit text field and label
JTextField fTextField = new JTextField(12);
add(fTextField);
add(new JLabel("Fahrenheit"));
fTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
fTextField.setEditable(false);
//Centrigrade text field and label
JTextField cTextField = new JTextField(12);
add(cTextField);
add(new JLabel("Centigrade"));
cTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
cTextField.setEditable(false);
//Kelvin text field and label
JTextField kTextField = new JTextField(12);
add(kTextField);
add(new JLabel("Kelvin"));
kTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
kTextField.setEditable(false);
//Creating Fahrenheit, Centigrade, Kelvin buttons
JButton centigrade = new JButton("Centigrade");
JButton fahrenheit = new JButton("Fahrenheit");
JButton kelvin = new JButton("Kelvin");
//Fahrenheit properties
fahrenheit.setForeground(Color.RED);
fahrenheit.setFont(font1);
//Centigrade properties
centigrade.setForeground(Color.BLUE);
centigrade.setFont(font1);
//Kelvin properties
kelvin.setForeground(Color.MAGENTA);
kelvin.setFont(font1);
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(fahrenheit);
panel.add(centigrade);
panel.add(kelvin);
add(panel); // Add panel to the frame
// Register listeners
fahrenheitListener listener1 = new fahrenheitListener();
centigradeListener listener2 = new centigradeListener();
kelvinListener listener3 = new kelvinListener();
fahrenheit.addActionListener(listener1);
centigrade.addActionListener(listener2);
kelvin.addActionListener(listener3);
}
public static void main(String[] args) {
JFrame frame = new HandleEvent();
frame.setTitle("Temperature Converter");
frame.setSize(309, 259);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class fahrenheitListener implements ActionListener {
public double fCalc;
public double cCalc;
public double kCalc;
public Object kTextField = kCalc;
public Object fTextField = fCalc;
public Object cTextField = cCalc;
public Object temperature = new JTextField(12);
public void actionPerformed(ActionEvent e) {
//returns the object on which the Event initially occurred
// FAHRENHEIT CALC
if (e.getSource() == temperature) {
try {
fCalc = Double.parseDouble(((AbstractButton) temperature).getText());
} //ends try
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Invalid Input Try Again","Non-numeric",JOptionPane.ERROR_MESSAGE);
} //ends catch
cCalc = (fCalc - 32)/1.8;
kCalc = cCalc + 273.16;
DecimalFormat df = new DecimalFormat("#,##0");
((AbstractButton) fTextField).setText(df.format(fTextField));
((AbstractButton) cTextField).setText(df.format(cTextField));
((AbstractButton) kTextField).setText(df.format(kTextField));
} //ends if
}
}
class centigradeListener implements ActionListener {
public double fCalc;
public double cCalc;
public double kCalc;
public Object kTextField = kCalc;
public Object fTextField = fCalc;
public Object cTextField = cCalc;
public Object temperature = new JTextField(12);
public void actionPerformed(ActionEvent e) {
//returns the object on which the Event initially occurred
// CENTIGRADE CALC
if (e.getSource() == temperature) {
try {
cCalc = Double.parseDouble(((JTextComponent) temperature).getText());
} //ends try
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Invalid Input Try Again","Non-numeric",JOptionPane.ERROR_MESSAGE);
} //ends catch
fCalc = (cCalc * 9)/5 + 32;
kCalc = cCalc + 273.16;
DecimalFormat df = new DecimalFormat("#,##0");
((JTextComponent) fTextField).setText(df.format(fTextField));
((JTextComponent) cTextField).setText(df.format(cTextField));
((JTextComponent) kTextField).setText(df.format(kTextField));
} //ends if
}
}
class kelvinListener implements ActionListener {
public double fCalc;
public double cCalc;
public double kCalc;
public Object kTextField = kCalc;
public Object fTextField = fCalc;
public Object cTextField = cCalc;
public Object temperature = new JTextField(12);
public void actionPerformed(ActionEvent e) {
//returns the object on which the Event initially occurred
// KELVIN CALC
if (e.getSource() == temperature) {
try {
kCalc = Double.parseDouble(((JLabel) temperature).getText());
} //ends try
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,"Invalid Input Try Again","Non-numeric",JOptionPane.ERROR_MESSAGE);
} //ends catch
cCalc = kCalc - 273.16;
fCalc = 1.8*(kCalc - 273) + 32;
DecimalFormat df = new DecimalFormat("#,##0");
((JLabel) fTextField).setText(df.format(fTextField));
((JLabel) cTextField).setText(df.format(cTextField));
((JLabel) kTextField).setText(df.format(kTextField));
} //ends if
} //ends method
}
}
答
有一些你可能做到这一点的方式,但让工作与外部事件处理程序的概念。
在这种情况下,我们只需要公开其处理程序真正需要的功能,来实现这一点,我们可以用一个简单的界面
public interface Tempatures {
public double getValue();
public void setFahrenheit(double value);
public void setCentigrade(double value);
public void setKelvin(double value);
}
这样一来,处理程序并不需要关心价值是如何产生的或者结果如何,只有他们能够得到价值并设定结果。
虽然你仍然可以使用ActionListener
,许多功能是每个处理程序,它只是尖叫了一个更好的设计是相同的。相反,我将使用Action
API并创建一个abstract
Action
执行的基本功能,这是共同的所有处理器
public abstract class TempatureAction extends AbstractAction {
private Tempatures tempatures;
public TempatureAction(Tempatures tempatures) {
this.tempatures = tempatures;
}
public void actionPerformed(ActionEvent e) {
double value = tempatures.getValue();
tempatures.setFahrenheit(toFahrenheit(value));
tempatures.setCentigrade(toCentigrade(value));
tempatures.setKelvin(toKelvin(value));
}
public abstract double toFahrenheit(double value);
public abstract double toCentigrade(double value);
public abstract double toKelvin(double value);
}
现在,这基本上说,这一类的实现必须提供的功能转换根据其内部要求给予不同值的给定值。
对于每一个处理程序,他们可以简单地返回value
为他们翻译的价值和使用其他方法来执行实际的转换,例如...
public class FahrenheitListener extends TempatureAction {
public FahrenheitListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Fahrenheit");
}
public double toFahrenheit(double value) {
return value;
}
public double toCentigrade(double value) {
return (value - 32)/1.8;
}
public double toKelvin(double value) {
return toCentigrade(value) + 273.16;
}
}
public class CentigradeListener extends TempatureAction {
public CentigradeListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Centigrade");
}
public double toFahrenheit(double value) {
return (value * 9)/5 + 32;
}
public double toCentigrade(double value) {
return value;
}
public double toKelvin(double value) {
return toCentigrade(value) + 273.16;
}
}
public class KelvinListener extends TempatureAction {
public KelvinListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Kelvin");
}
public double toFahrenheit(double value) {
return 1.8 * (value - 273) + 32;
}
public double toCentigrade(double value) {
return value - 273.16;
}
public double toKelvin(double value) {
return value;
}
}
最后,我们可以把它一起...
public class TestPane extends JPanel implements Tempatures {
public JButton fahrenheit;
public JButton centigrade;
public JButton kelvin;
private final JSpinner temperature;
private final JFormattedTextField fahrenheitTextField;
private final JFormattedTextField centigradeField;
private final JFormattedTextField kelvinField;
public TestPane() {
Font font1 = new Font("Arial", Font.BOLD, 12);
//Flow layout setup
setLayout(new FlowLayout(FlowLayout.LEADING, 10, 20));
//Temperature label and text field
temperature = new JSpinner(new SpinnerNumberModel(0d, null, null, 0.5d));
add(new JLabel("Temperature: "));
temperature.setBackground(Color.WHITE);
add(temperature);
//Fahrenheit text field and label
fahrenheitTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(fahrenheitTextField);
add(new JLabel("Fahrenheit"));
fahrenheitTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
fahrenheitTextField.setEditable(false);
fahrenheitTextField.setColumns(8);
//Centrigrade text field and label
centigradeField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(centigradeField);
add(new JLabel("Centigrade"));
centigradeField.setCursor(new Cursor(Cursor.HAND_CURSOR));
centigradeField.setEditable(false);
centigradeField.setColumns(8);
//Kelvin text field and label
kelvinField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(kelvinField);
add(new JLabel("Kelvin"));
kelvinField.setCursor(new Cursor(Cursor.HAND_CURSOR));
kelvinField.setEditable(false);
kelvinField.setColumns(8);
//Creating Fahrenheit, Centigrade, Kelvin buttons
centigrade = new JButton("Centigrade");
fahrenheit = new JButton("Fahrenheit");
kelvin = new JButton("Kelvin");
//Fahrenheit properties
fahrenheit.setForeground(Color.RED);
fahrenheit.setFont(font1);
//Centigrade properties
centigrade.setForeground(Color.BLUE);
centigrade.setFont(font1);
//Kelvin properties
kelvin.setForeground(Color.MAGENTA);
kelvin.setFont(font1);
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(fahrenheit);
panel.add(centigrade);
panel.add(kelvin);
add(panel); // Add panel to the frame
fahrenheit.setAction(new FahrenheitListener(this));
centigrade.setAction(new CentigradeListener(this));
kelvin.setAction(new KelvinListener(this));
}
@Override
public double getValue() {
return (Double) temperature.getValue();
}
@Override
public void setFahrenheit(double value) {
fahrenheitTextField.setValue(value);
}
@Override
public void setCentigrade(double value) {
centigradeField.setValue(value);
}
@Override
public void setKelvin(double value) {
kelvinField.setValue(value);
}
}
更多细节
见How to Use Actions也看看How to Use Spinners一个第二How to Use Formatted Text Fields其处理多用户输入进行验证,并且为你
的Runnable例如自动格式化...
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TempatureCalculator {
public static void main(String[] args) {
new TempatureCalculator();
}
public TempatureCalculator() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements Tempatures {
public JButton fahrenheit;
public JButton centigrade;
public JButton kelvin;
private final JSpinner temperature;
private final JFormattedTextField fahrenheitTextField;
private final JFormattedTextField centigradeField;
private final JFormattedTextField kelvinField;
public TestPane() {
Font font1 = new Font("Arial", Font.BOLD, 12);
//Flow layout setup
setLayout(new FlowLayout(FlowLayout.LEADING, 10, 20));
//Temperature label and text field
temperature = new JSpinner(new SpinnerNumberModel(0d, null, null, 0.5d));
add(new JLabel("Temperature: "));
temperature.setBackground(Color.WHITE);
add(temperature);
//Fahrenheit text field and label
fahrenheitTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(fahrenheitTextField);
add(new JLabel("Fahrenheit"));
fahrenheitTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
fahrenheitTextField.setEditable(false);
fahrenheitTextField.setColumns(8);
//Centrigrade text field and label
centigradeField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(centigradeField);
add(new JLabel("Centigrade"));
centigradeField.setCursor(new Cursor(Cursor.HAND_CURSOR));
centigradeField.setEditable(false);
centigradeField.setColumns(8);
//Kelvin text field and label
kelvinField = new JFormattedTextField(NumberFormat.getNumberInstance());
add(kelvinField);
add(new JLabel("Kelvin"));
kelvinField.setCursor(new Cursor(Cursor.HAND_CURSOR));
kelvinField.setEditable(false);
kelvinField.setColumns(8);
//Creating Fahrenheit, Centigrade, Kelvin buttons
centigrade = new JButton("Centigrade");
fahrenheit = new JButton("Fahrenheit");
kelvin = new JButton("Kelvin");
//Fahrenheit properties
fahrenheit.setForeground(Color.RED);
fahrenheit.setFont(font1);
//Centigrade properties
centigrade.setForeground(Color.BLUE);
centigrade.setFont(font1);
//Kelvin properties
kelvin.setForeground(Color.MAGENTA);
kelvin.setFont(font1);
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(fahrenheit);
panel.add(centigrade);
panel.add(kelvin);
add(panel); // Add panel to the frame
fahrenheit.setAction(new FahrenheitListener(this));
centigrade.setAction(new CentigradeListener(this));
kelvin.setAction(new KelvinListener(this));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
@Override
public double getValue() {
return (Double) temperature.getValue();
}
@Override
public void setFahrenheit(double value) {
fahrenheitTextField.setValue(value);
}
@Override
public void setCentigrade(double value) {
centigradeField.setValue(value);
}
@Override
public void setKelvin(double value) {
kelvinField.setValue(value);
}
}
public interface Tempatures {
public double getValue();
public void setFahrenheit(double value);
public void setCentigrade(double value);
public void setKelvin(double value);
}
public abstract class TempatureAction extends AbstractAction {
private Tempatures tempatures;
public TempatureAction(Tempatures tempatures) {
this.tempatures = tempatures;
}
public void actionPerformed(ActionEvent e) {
double value = tempatures.getValue();
tempatures.setFahrenheit(toFahrenheit(value));
tempatures.setCentigrade(toCentigrade(value));
tempatures.setKelvin(toKelvin(value));
}
public abstract double toFahrenheit(double value);
public abstract double toCentigrade(double value);
public abstract double toKelvin(double value);
}
public class FahrenheitListener extends TempatureAction {
public FahrenheitListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Fahrenheit");
}
public double toFahrenheit(double value) {
return value;
}
public double toCentigrade(double value) {
return (value - 32)/1.8;
}
public double toKelvin(double value) {
return toCentigrade(value) + 273.16;
}
}
public class CentigradeListener extends TempatureAction {
public CentigradeListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Centigrade");
}
public double toFahrenheit(double value) {
return (value * 9)/5 + 32;
}
public double toCentigrade(double value) {
return value;
}
public double toKelvin(double value) {
return toCentigrade(value) + 273.16;
}
}
public class KelvinListener extends TempatureAction {
public KelvinListener(Tempatures tempatures) {
super(tempatures);
putValue(NAME, "Kelvin");
}
public double toFahrenheit(double value) {
return 1.8 * (value - 273) + 32;
}
public double toCentigrade(double value) {
return value - 273.16;
}
public double toKelvin(double value) {
return value;
}
}
}
的'JTextField's有当地背景的'HandleEvent'构造,这意味着他们将无法访问程序/课程的任何其他部分。尝试使它们成为实例级别字段。你也阴影按钮变量(重新声明为局部变量) – MadProgrammer
做'的handleEvent EVENT3 =新的handleEvent();'你内心监听器是毫无意义的作为'event3'无关与作为实例的实例在屏幕上,所产生的原始事件 – MadProgrammer
MadProgrammer:谢谢!我甚至没有意识到我已经做到了。修正并删除了事件3的事情。还删除了按钮变量,如果这些是实例级别? – CodedMe