出于某种原因,某些Swing组件在我运行程序时不会出现,我无法弄清楚原因。仅显示乘法标签,乘法按钮,总标签和停止按钮。其他人都没有工作。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BabyCalculatorFinal extends JFrame{
private JLabel AdditionLabel;
private JTextField AdditionField;
private JButton AdditionButton;
private JPanel Multiplication;
private JLabel MultiplicationLabel;
private JTextField MultiplicationField;
private JButton MultiplicationButton;
private JPanel Total;
private JLabel TotalLabel;
private JTextField TotalField;
JButton StopButton;
public BabyCalculatorFinal(){
setDefaultCloseOperation(EXIT_ON_CLOSE);// 1st thing to do
setName("Baby Calculator Final"); // 2nd thing to do
setLayout(new GridLayout(3,0)); //sets grid layout for the entire thing with 3 rows
// Create Action Event
BabyCalculatorListener Listener = new BabyCalculatorListener();
//Addition
//Addition Set Layout
JPanel Addition = new JPanel(new BorderLayout());
//Addition Features
AdditionLabel = new JLabel("Amount to add"); //Create label
AdditionField = new JTextField(10);
AdditionButton = new JButton("Add");
//Organize Addition Panel
Addition.add(AdditionLabel, BorderLayout.WEST);//IMPORTANT FORMAT
Addition.add(AdditionLabel, BorderLayout.CENTER);
Addition.add(AdditionButton, BorderLayout.EAST);
//Add addition Panel to Frame
add(Addition);
AdditionButton.addActionListener(Listener);
//Multiplictation
//Multiplication Set Layout
Multiplication = new JPanel();
Multiplication.setLayout(new BorderLayout());//Trying a different way of setting the layout
//Multiplication Features
MultiplicationLabel = new JLabel("Amount to Multiply"); //Create label
MultiplicationField = new JTextField(10);
MultiplicationButton = new JButton("Multiply");
//Organize Multiplication Panel
Addition.add(MultiplicationLabel, BorderLayout.WEST);
Addition.add(MultiplicationLabel, BorderLayout.CENTER);
Addition.add(MultiplicationButton, BorderLayout.EAST);
//Add Multiplication Panel to Frame
add(Multiplication);
MultiplicationButton.addActionListener(Listener);
//Total
Total = new JPanel(new FlowLayout(10));
TotalLabel = new JLabel("Total");
TotalField = new JTextField();
TotalField.setText("0.0");
TotalField.setVisible(false);
StopButton = new JButton("Stop");
Total.add(TotalLabel);
Total.add(TotalField);
Total.add(StopButton);
//Add Total Panel to Frame
add(Total);
pack();
setVisible(true);
}
public static void main(String[] args){
JFrame myFrame = new BabyCalculatorFinal();
}
public class BabyCalculatorListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String totalText = TotalField.getText();
double total = Double.parseDouble(totalText);
if (e.getSource() == AdditionButton){
String additionText = AdditionField.getText();
double addition = Double.parseDouble(additionText);
total += addition;
}
else{
String multiplicationText = MultiplicationField.getText();
double multiplication = Double.parseDouble(multiplicationText);
total += multiplication;
}
TotalField.setText(total + "");
}
}
}
答案 0 :(得分:3)
您的代码中充满了拼写错误(?),例如您将AdditionLabel
两次添加到JPanel
,而不是添加AdditionLabel
和AdditionField
。并且您在创建Multiplication
面板后未使用Addition
面板,而是覆盖additionLabel = new JLabel("Amount to add"); // Create label
additionField = new JTextField(10);
additionButton = new JButton("Add");
// Organize addition Panel
addition.add(additionLabel, BorderLayout.WEST);// IMPORTANT FORMAT
addition.add(additionField, BorderLayout.CENTER); // instead of additionLabel
addition.add(AdditionButton, BorderLayout.EAST);
// Add addition Panel to Frame
add(addition);
AdditionButton.addActionListener(Listener);
// Multiplictation
// Multiplication Set Layout
multiplication = new JPanel();
multiplication.setLayout(new BorderLayout());// Trying a different way
// of setting the layout
// Multiplication Features
multiplicationLabel = new JLabel("Amount to Multiply"); // Create label
multiplicationField = new JTextField(10);
multiplicationButton = new JButton("Multiply");
// Organize Multiplication Panel
multiplication.add(multiplicationLabel, BorderLayout.WEST); // instead of Addition
multiplication.add(multiplicationField, BorderLayout.CENTER);
multiplication.add(multiplicationButton, BorderLayout.EAST);
面板的内容。添加组件的更正片段应该是(我更改了变量名以符合Java约定):
{{1}}