import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Temperature extends JFrame implements ActionListener{
private JLabel text1,text2;
private JTextField userInput,output;
private JButton cvrt,cvrt1,clr;
public void BuildGUI(){
text1 = new JLabel("Key in your value");
text2 = new JLabel("Answer");
userInput = new JTextField();
output = new JTextField();
cvrt = new JButton("Convert Celcius to Fahrenheit");
cvrt.addActionListener(this);
cvrt1 = new JButton ("Convert Fahrenheit to Celcius");
cvrt1.addActionListener(this);
clr = new JButton("Clear");
clr.addActionListener(this);
Container cpane = getContentPane();
cpane.setLayout(new GridLayout(4,2));
setVisible(true);
cpane.add(text1);
cpane.add(userInput);
cpane.add(text2);
cpane.add(output);
cpane.add(cvrt);
cpane.add(cvrt1);
cpane.add(clr);
}
public void actionPerformed(ActionEvent e){
double fahrenheit,celcius;
if(e.getSource()== cvrt){
String value = userInput.getText();
celcius = Double.parseDouble(value);
fahrenheit = celcius*1.8+32;
String out = ""+fahrenheit;
output.setText(out);
}else if (e.getSource()== cvrt1){
String value = userInput.getText();
fahrenheit = Double.parseDouble(value);
celcius = (fahrenheit-32)/1.8;
String out = ""+ celcius;
output.setText(out);
}else{
userInput.setText(null);
output.setText(null);
}
}
}
抱歉,我还是 Java 新手。我被分配创建一个选项卡窗格。我只是想知道你能把上面的课程调到下面的 JTabbed 窗格中吗?因为将所有代码都扔到一个类中会很混乱。浏览了很多网站,但仍然是徒劳的。希望有人能指出来。
public class Windows extends JFrame{
public Windows(){
setTitle("Converter");
setSize(null);
JTabbedPane tabby = new JTabbedPane();
getContentPane().add(tabby);
}