簡體   English   中英

如何使Java中的按鈕與textField相對應?

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

剛剛完成我的大學一年級,想提高我的Java編碼技能。 我目前正在嘗試開發一個簡單的GUI計算器應用程序。 但是,我仍然堅持如何按一下我的jButton,例如在我的文本字段中出現第一個數字。 我了解我可能必須為此功能使用動作偵聽器,但目前仍處於停滯狀態。 任何幫助都將是驚人的。 非常感謝你。

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);
        }
    }
});

祝您學習順利!

您可以在數字按鈕上添加按下的動作,並將textFields文本設置為所需的文本。(或在必要時附加)。

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

如果您要使用Java Gui,我真的建議您使用Eclipse Window Builder。 使用一些Gui代碼可以節省大量時間,而這些代碼永遠不會增加您的Java知識。 如果您真的想增加您的Java知識,可以查看一下本書:

Java 6e中的數據結構和算法Michael T. Goodrich,Roberto Tamassia,Michael H. Goldwasser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM