如何使 java 中的按钮与文本字段相对应?

How do I make a button in java correspond with a textField?

刚读完大学一年级,想在 Java 提高我的编码技能。我目前正在尝试开发一个简单的 GUI 计算器应用程序。但是,我仍然坚持如何使用我的 jButtons,例如一旦按下,我的文本字段上就会出现数字 1。我知道我可能必须为此功能使用动作侦听器,但我目前被卡住了。 任何帮助都会很棒。非常感谢。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    frame.pack();
    frame.setIconImage(new ImageIcon("F:\Java Summer Project\Sumer_Project\images\farme_image.jpg").getImage());
    frame.setSize(500, 480);
    frame.setResizable(false);
    frame.setVisible(true);

    ////// panel\\
    JPanel panel = new JPanel();
    panel.setLayout(null);
    frame.add(panel);

    TextField textField = new TextField();
    textField.setBounds(0, 0, 500, 200);
    textField.setEnabled(true);
    panel.add(textField);

    JButton openbracketButton = new JButton("(");
    openbracketButton.setBounds(0, 200, 50, 50);
    panel.add(openbracketButton);

    JButton closebracketButton = new JButton(")");
    closebracketButton.setBounds(50, 200, 50, 50);
    panel.add(closebracketButton);

    JButton percButton = new JButton("%");
    percButton.setBounds(100, 200, 50, 50);
    panel.add(percButton);

    JButton clearButton = new JButton("ac");
    clearButton.setBounds(150, 200, 50, 50);
    panel.add(clearButton);

    JButton divideButton = new JButton("÷");
    divideButton.setBounds(150, 250, 50, 50);
    panel.add(divideButton);

    JButton timesButton = new JButton("X");
    timesButton.setBounds(150, 300, 50, 50);
    panel.add(timesButton);

    JButton minusButton = new JButton("+");
    minusButton.setBounds(150, 350, 50, 50);
    panel.add(minusButton);

    JButton plusButton = new JButton("-");
    plusButton.setBounds(150, 400, 50, 50);
    panel.add(plusButton);

    JButton oneButton = new JButton("1");
    oneButton.setBounds(0, 250, 50, 50);
    oneButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent oneButton) {
            System.out.println("Do Something Clicked");
        }
    });

    panel.add(oneButton);

我猜你正在寻找这样的东西:

oneButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent oneButton) {
        if(StringUtils.isEmpty(textField)) {
            // If its empty so just put 1 in the display
            textField.setText("1");
        } else {
            // Get value on display and convert to a integer
            // Plus one because its the button one
            int result = 1 + Integer.parseInt(textField.getText());
            // Set the result to the display
            textField.setText((String) result);
        }
    }
});

祝你学习顺利!

您可以向数字按钮添加按下操作并将文本字段文本设置为所需的文本。(或在必要时追加)。

// Like this
textField.setText("1");

如果您要使用 java Gui,我真的向您推荐 Eclipse Window Builder。使用一些永远不会增加您的 Java 知识的 Gui 代码,它会为您节省大量时间。如果你真的想增加你的Java知识,你可以看看这本书:

Java6e 中的数据结构和算法 迈克尔·古德里奇、罗伯托·塔马西亚、迈克尔·H·戈德瓦瑟