學習,善待...第一次搖擺桂。我正在嘗試使用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
}
}
的'JTextField's有當地背景的'HandleEvent'構造,這意味着他們將無法訪問程序/課程的任何其他部分。嘗試使它們成爲實例級別字段。你也陰影按鈕變量(重新聲明爲局部變量) – MadProgrammer
做'的handleEvent EVENT3 =新的handleEvent();'你內心監聽器是毫無意義的作爲'event3'無關與作爲實例的實例在屏幕上,所產生的原始事件 – MadProgrammer
MadProgrammer:謝謝!我甚至沒有意識到我已經做到了。修正並刪除了事件3的事情。還刪除了按鈕變量,如果這些是實例級別? – CodedMe