我正在创建一个计算器,我想知道如何在文本字段中添加数字或符号。例如,当您按下标有' 2'的按钮时,我想要' 2'出现在文本域中。
到目前为止我所拥有的仅仅是计算器的框架,其中按钮当前无效。如果您需要更多信息,请询问。感谢
import javax.swing.*;
import java.awt.*;
public class CalcFrame extends JFrame
{
public CalcFrame()
{
super("Calculator");
setLayout(new FlowLayout() );
JTextField calcTextField = new JTextField (19);
add (calcTextField);
calcTextField.setEditable(false);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout ( new GridLayout(4,4) );
String[] buttonNames = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"};
for (String buttonText : buttonNames)
{
JButton button = new JButton(buttonText);
buttonPanel.add(button);
}
buttonPanel.setPreferredSize(new Dimension(220, 250));
add(buttonPanel);
}
public static void main (String[]args)
{
CalcFrame myCalc = new CalcFrame();
myCalc.setSize(300, 350);
myCalc.setVisible( true );
myCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:4)
在这里,我编辑了你的代码并添加了一些好东西, = 按钮正在工作,并添加了 C 按钮,这是一个清除按钮。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;//added this line
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class CalcFrame extends JFrame implements ActionListener{
private JTextField calcTextField;
private ScriptEngineManager sem;
private ScriptEngine scriptEngine;
public CalcFrame(){
super("Calculator");
setLayout(new FlowLayout() );
calcTextField = new JTextField (19);
calcTextField.setPreferredSize(new Dimension(0,50));//added this line
add (calcTextField);
calcTextField.setEditable(false);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout ( new GridLayout(5,4,10,10) );
String[] buttonNames = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "(", ")"};
for (String buttonText : buttonNames)
{
JButton button = new JButton(buttonText);
buttonPanel.add(button);
button.addActionListener(this);//added this line
}
buttonPanel.setPreferredSize(new Dimension(220, 250));
add(buttonPanel);
sem = new ScriptEngineManager();
scriptEngine = sem.getEngineByName("JavaScript");
}
//added this method (it must be added because you implemented
//ActionListener above while declaring the class
@Override
public void actionPerformed(ActionEvent ev){
JButton b = (JButton)ev.getSource();
if(b.getText().equals("C"))
calcTextField.setText("");
else if(!b.getText().equals("="))
calcTextField.setText(calcTextField.getText()+b.getText());
else{
try{
String result = scriptEngine.eval(calcTextField.getText()).toString();
calcTextField.setText(result);
}catch(Exception ex){
JOptionPane.showMessageDialog(null,"The Expression is wrong, maybe you miss a bracket!");
}
}
}
//-----
public static void main (String[]args) {
CalcFrame myCalc = new CalcFrame();
myCalc.setSize(300, 350);
myCalc.setVisible( true );
myCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 1 :(得分:2)
String[] buttonNames = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"};
for (String buttonText : buttonNames)
{
JButton button = new JButton(buttonText);
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//Set your textfield to the previous text here as well as the new button
//which should correlate to your button name.
calcTextField.setText(calcTextField.getText() + buttonText);
}
});
}
答案 2 :(得分:2)
正如Salah所说,但是,我们不要老套;)让我们利用lambdas的优势:
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
//Avoid extending your class with JFrame. There is no need in this case.
public class CalcFrame extends JFrame {
public CalcFrame() {
super("Calculator");
setLayout(new FlowLayout());
JTextField calcTextField = new JTextField(19);
add(calcTextField);
calcTextField.setEditable(false);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
List<String> buttonNames = Arrays.asList("7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+");
buttonNames.forEach(bt -> {
JButton button = new JButton(bt);
button.addActionListener(e -> calcTextField.setText(calcTextField.getText() +
((JButton) e.getSource()).getText()));
buttonPanel.add(button);
});
//buttonPanel.setPreferredSize(new Dimension(220, 250));
add(buttonPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(CalcFrame::new);
}
}
答案 3 :(得分:1)
首先您需要在按钮中添加ActionListener
:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// set text.
}
});
然后您可以将文本设置为文本字段,如下:
textField.setText(buttonText);