我正在尝试编写一个程序,根据用户选择(使用AWT和Swing的所有GUI)将Celsius转换为Fahrenheit或Fahrenheit Celsius。
如何允许一种方法更改其他方法的可见性,以便更改程序的哪些部分可见?
以下是我的代码,因为我的确切问题很难解释:
package javapp1;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
/**
*
* @author logan
*/
public class FahrenheitToCelsius extends JFrame
{
private JPanel startPanel;
private JPanel fahrenheitToCelsiusPanel;
private JPanel celsiusToFahrenheitPanel;
private JLabel decideLabel;
private JLabel FahrenheitToCelsiusLabel;
private JLabel CelsiusToFahrenheitLabel;
private JTextField FahrenheitToCelsiusText;
private JTextField CelsiusToFahrenheitText;
private JButton fahrenheitButton;
private JButton celsiusButton;
private JButton calcButton;
private JButton exitButton;
private JButton backButton;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 100;
public FahrenheitToCelsius()
{
setTitle("Fahrenheit/Celsius Conversion");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildStartPanel();
add(startPanel);
}
private void buildStartPanel()
{
decideLabel = new JLabel("Would you like to convert from Celsius or Fahrenheit?");
fahrenheitButton = new JButton("Fahrenheit");
fahrenheitButton.addActionListener(new FahrenheitToCelsiusListener());
celsiusButton = new JButton("Celsius");
celsiusButton.addActionListener(new CelsiusToFahrenheitListener());
exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitButtonListener());
startPanel = new JPanel();
startPanel.add(decideLabel);
startPanel.add(fahrenheitButton);
startPanel.add(celsiusButton);
setVisible(true);
}
private void buildFahrenheitToCelsiusPanel()
{
FahrenheitToCelsiusLabel = new JLabel("Enter the degrees Fahrenheit");
FahrenheitToCelsiusText = new JTextField(10);
calcButton = new JButton("Calculate");
calcButton.addActionListener(new CalculateFtoC());
exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitButtonListener());
backButton = new JButton("Back");
backButton.addActionListener(new BackButtonListener());
fahrenheitToCelsiusPanel = new JPanel();
fahrenheitToCelsiusPanel.add(FahrenheitToCelsiusLabel);
fahrenheitToCelsiusPanel.add(FahrenheitToCelsiusText);
fahrenheitToCelsiusPanel.add(calcButton);
fahrenheitToCelsiusPanel.add(exitButton);
}
private void buildCelsiusToFahrenheitPanel()
{
CelsiusToFahrenheitLabel = new JLabel("Enter the degree Celsius");
CelsiusToFahrenheitText = new JTextField(10);
calcButton = new JButton("Calculate");
calcButton.addActionListener(new CalculateCtoF());
exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitButtonListener());
backButton = new JButton("Back");
backButton.addActionListener(new BackButtonListener());
celsiusToFahrenheitPanel = new JPanel();
celsiusToFahrenheitPanel.add(CelsiusToFahrenheitLabel);
celsiusToFahrenheitPanel.add(CelsiusToFahrenheitText);
celsiusToFahrenheitPanel.add(calcButton);
celsiusToFahrenheitPanel.add(exitButton);
}
private class FahrenheitToCelsiusListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
startPanel.setVisible(false);
celsiusToFahrenheitPanel.setVisible(false);
buildFahrenheitToCelsiusPanel();
}
}
private class CelsiusToFahrenheitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
startPanel.setVisible(false);
fahrenheitToCelsiusPanel.setVisible(false);
buildCelsiusToFahrenheitPanel();
}
}
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
private class BackButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fahrenheitToCelsiusPanel.setVisible(false);
celsiusToFahrenheitPanel.setVisible(false);
buildStartPanel();
}
}
private class CalculateFtoC implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
int celsius;
int fahrenheit;
input = FahrenheitToCelsiusText.getText();
fahrenheit = Integer.parseInt(input);
celsius = ((fahrenheit-32)*(5/9));
JOptionPane.showMessageDialog(null, fahrenheit+" degrees fahrenheit is "+celsius+" degrees celsius.");
}
}
private class CalculateCtoF implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input;
int celsius;
int fahrenheit;
input = CelsiusToFahrenheitText.getText();
celsius = Integer.parseInt(input);
fahrenheit = ((celsius*(9/32))+32);
JOptionPane.showMessageDialog(null, celsius+" degrees celsius is "+fahrenheit+" degrees fahrenheit.");
}
}
public static void main(String[] args)
{
new FahrenheitToCelsius();
}
}