2015-10-05 131 views
-2

學習,善待...第一次搖擺桂。我正在嘗試使用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 
} 

}

+0

的'JTextField's有當地背景的'HandleEvent'構造,這意味着他們將無法訪問程序/課程的任何其他部分。嘗試使它們成爲實例級別字段。你也陰影按鈕變量(重新聲明爲局部變量) – MadProgrammer

+0

做'的handleEvent EVENT3 =新的handleEvent();'你內心監聽器是毫無意義的作爲'event3'無關與作爲實例的實例在屏幕上,所產生的原始事件 – MadProgrammer

+0

MadProgrammer:謝謝!我甚至沒有意識到我已經做到了。修正並刪除了事件3的事情。還刪除了按鈕變量,如果這些是實例級別? – CodedMe

回答

1

有一些你可能做到這一點的方式,但讓工作與外部事件處理程序的概念。

在這種情況下,我們只需要公開其處理程序真正需要的功能,來實現這一點,我們可以用一個簡單的界面

public interface Tempatures { 

    public double getValue(); 
    public void setFahrenheit(double value); 
    public void setCentigrade(double value); 
    public void setKelvin(double value); 

} 

這樣一來,處理程序並不需要關心價值是如何產生的或者結果如何,只有他們能夠得到價值並設定結果。

雖然你仍然可以使用ActionListener,許多功能是每個處理程序,它只是尖叫了一個更好的設計是相同的。相反,我將使用Action API並創建一個abstractAction執行的基本功能,這是共同的所有處理器

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; 
     } 

    } 
}