我要做一個簡單的計算器,但我遇到了這個問題:
當按下按鈕時,它應該執行以下操作:
- 從兩個 JTextField 中獲取文本并將它們轉換為 Double。使用 Double.parseDouble(String input) 將 String 轉換為 Double。(不知道把這個放在哪里?)
- 從 JComboBox 獲取文本。使用 getSelectedItem() 方法獲取當前選定的字串。(不確定我是否正確地這樣做了)
- 根據來自 JComboBox 的字串計算結果 這決定了您將使用 運算子、- 運算子、* 運算子還是 / 運算子。(不知道該怎么做)
- 將結果設定為 JLabel 使用 .setText() 方法構造一個 String
這是我的4個問題。
class MyFrame extends JFrame {
public JTextField firstNumber;
public JTextField secondNumber;
public JButton calc;
public JLabel result;
public JComboBox combo;
public MyFrame() {
super();
init();
}
private void init() {
JButton calc = new JButton("Calculate");
calc.addActionListener(new MyButtonListener(this));
firstNumber = new JTextField();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(calc);
this.add(firstNumber);
this.add(secondNumber);
this.pack();
this.setVisible(true);
}
}
class MyButtonListener implements ActionListener {
MyFrame fr;
public MyButtonListener(MyFrame frame)
{
fr = frame;
fr.firstNumber.getText();
fr.secondNumber.getText();
fr.combo.getSelectedItem();
}
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton) e.getSource();
}
}
private static void constructGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JTextField firstNumber = new JTextField();
JTextField secondNumber = new JTextField();
JButton calc = new JButton("Calculate");
JLabel result = new JLabel();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Simple Calculator");
String[] arth = { "Add", "Subtract", "Multiple",
"Divide" };
JComboBox combo = new JComboBox(arth);
combo.setSelectedIndex(0);
frame.setLayout(new GridLayout(5, 2));
frame.add(new JLabel("First Number:"));
frame.add(firstNumber);
frame.add(new JLabel("Second Number:"));
frame.add(secondNumber);
frame.add(combo);
frame.add(calc);
frame.add(new JLabel("Result:"));
frame.pack();
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
constructGUI();
}
});
}
}
uj5u.com熱心網友回復:
您的 MyFrame 未與 main 中呼叫的其余代碼連接。
為您的結果欄位添加了偵聽器(也包括在 JFrame 中,因為它沒有被使用) - 可以從 lambda 中提取代碼以獲得更好的可見性
僅添加“添加”其余部分將類似
私有靜態無效構造GUI(){
JFrame.setDefaultLookAndFeelDecorated(true); JTextField firstNumber = new JTextField(); JTextField secondNumber = new JTextField(); JButton calc = new JButton("Calculate"); JLabel result = new JLabel("Result:"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Simple Calculator"); String[] arth = {"Add", "Subtract", "Multiple", "Divide"}; JComboBox combo = new JComboBox(arth); combo.setSelectedIndex(0); frame.setLayout(new GridLayout(5, 2)); frame.add(new JLabel("First Number:")); frame.add(firstNumber); frame.add(new JLabel("Second Number:")); frame.add(secondNumber); frame.add(combo); frame.add(calc); frame.add(result); frame.pack(); frame.setSize(200, 200); frame.setVisible(true); calc.addActionListener( e -> { var first = Double.parseDouble(firstNumber.getText()); var secound = Double.parseDouble(secondNumber.getText()); double res = 0; switch (Objects.requireNonNull(combo.getSelectedItem()).toString()) { case "Add": res = first secound; break; } result.setText(String.valueOf(res)); });}
簡化主線:
public static void main(String[] args) {
SwingUtilities.invokeLater(Main2::constructGUI);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511686.html
