0
你好每一個我是新的Java編程我做了一個溫度轉換計算器它工作正常如果我保留我的代碼在這一個adittion方法,但如果我劃分它在不同的類空的JFrame顯示了我運行此程序後...破壞我的代碼在幾個類現在空Jframe顯示後運行
這裏是我的數據文件:
public class DataClass extends JFrame {
JFrame myFrame = new JFrame();
JPanel cp = new JPanel();
JLabel lbl_1 = new JLabel("Celcius");
JLabel lbl_2 = new JLabel("Fahrienheit");
JTextField tf_1 = new JTextField();
JTextField tf_2 = new JTextField();
JButton convert = new JButton("Convert");
JButton reset = new JButton("Reset");
JButton exitButton = new JButton("Exit");
JRadioButton c1toc2 = new JRadioButton("Celcius to Fahreinheit");
JRadioButton c2toc1 = new JRadioButton("Fahreiheit to Celcius");
ButtonGroup group = new ButtonGroup();
String celcius = "";
String farheinheit = "";
double celc = 0, farh = 0;
public void Addition() {
cp.add(lbl_1);
cp.add(lbl_2);
cp.add(tf_1);
cp.add(tf_2);
cp.add(convert);
cp.add(reset);
cp.add(exitButton);
cp.add(c1toc2);
cp.add(c2toc1);
group.add(c1toc2);
group.add(c2toc1);
myFrame.add(cp);
lbl_1.setBounds(20, 15, 60, 50);
tf_1.setBounds(100, 25, 200, 25);
lbl_2.setBounds(20, 100, 80, 50);
tf_2.setBounds(100, 110, 200, 25);
convert.setBounds(20, 180, 100, 30);
reset.setBounds(140, 180, 100, 30);
exitButton.setBounds(260, 180, 100, 30);
c1toc2.setBounds(20, 220, 150, 25);
c2toc1.setBounds(20, 250, 150, 25);
tf_2.setEditable(false);
tf_1.setEditable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationRelativeTo(null);
myFrame.setLayout(null);
myFrame.setSize(400, 400);
myFrame.setVisible(true);
}
}
這裏是我的動作監聽文件:
public class Temperature extends DataClass {
public Temperature() {
convert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
convertActionPerformed(evt);
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
resetActionPerformed(evt);
}
});
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
c1toc2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ctofActionPerformed(evt);
}
});
c2toc1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ftocActionPerformed(evt);
}
});
}
private void convertActionPerformed(ActionEvent evt) {
// TODO add your handling code here:
if (c1toc2.isSelected()) {
String celcius = tf_1.getText();
double celc = Double.parseDouble(celcius);
double farh = (celc * 1.8) + 32;
String farheinheit = String.valueOf(farh);
tf_2.setText(farheinheit);
tf_1.setEditable(false);
tf_2.setEditable(false);
}
else if (c2toc1.isSelected()) {
String farheinheit = tf_2.getText();
double farh = Double.parseDouble(farheinheit);
double celc = ((farh * 5)/9) - 32;
String celcius = String.valueOf(celc);
tf_1.setText(celcius);
tf_1.setEditable(false);
tf_2.setEditable(false);
}
}
private void resetActionPerformed(ActionEvent evt) {
tf_1.setText("");
tf_2.setText("");
tf_1.setEditable(false);
tf_2.setEditable(false);
group.clearSelection();
}
private void ctofActionPerformed(ActionEvent evt) {
tf_1.setEditable(true);
tf_2.setEditable(false);
}
private void ftocActionPerformed(ActionEvent evt) {
tf_2.setEditable(true);
tf_1.setEditable(false);
}
private void exitButtonActionPerformed(ActionEvent evt) {
System.exit(0);
}
}
這裏是我的DriverClass:
public class DriverClass {
public static void main(String args) {
DataClass obj1 = new DataClass();
obj1.Addition();
}
}
你有一些非常混亂的代碼。 'DataClass'從'JFrame'擴展,但你永遠不會使用,你可以在'DataClass'中創建另一個'JFrame'並使用它。作爲一般性建議,您不應該從'JFrame'或其他頂級容器擴展,而應該從'JPanel'開始,然後將其添加到您想要使用的任何容器 – MadProgrammer