【问题标题】:I Want The Integer Of My Calculator To Change When I Press One Of My JButtons当我按下我的一个 JButton 时,我希望我的计算器的整数发生变化
【发布时间】:2013-06-21 00:44:46
【问题描述】:

我有一个名为 input 的整数,它被转换为 String 字符串,我设置了 oneButton.addActionListener 和 public void actionPerformed(ActionEvent e){

if (e.getSource() == oneButton){

输入 = 1;

} }

但我的计算器上的数字一直为 0,请帮助

代码:

//everything is imported

public class Ken implements ActionListener{


static JButton oneButton = new JButton("1");
static JButton twoButton = new JButton("2");
static JButton threeButton = new JButton("3");
static JButton fourButton = new JButton("4");
static JButton fiveButton = new JButton("5");
static JButton sixButton = new JButton("6");
static JButton sevenButton = new JButton("7");
static JButton eightButton = new JButton("8");
static JButton nineButton = new JButton("9");
static JButton zeroButton = new JButton("0");
static JButton minusButton = new JButton("-");
static JButton timesButton = new JButton("*");
static JButton enterButton = new JButton("=");
static JButton dividButton = new JButton("/");
static JButton plusButton = new JButton("+");
public static int input = 0;    


public Ken(){

    JFrame frame = new JFrame("MyCalc");


    String string = ""+input;

    JTextField text = new JTextField(string, 9);
    frame.setSize(476, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    FlowLayout fl = new FlowLayout(0, 30, 20);
    setLayout(fl);
    Border empty;
    empty = BorderFactory.createEmptyBorder();

    frame.getContentPane().setLayout(fl);

    Container con = frame.getContentPane();
    con.setBackground(Color.LIGHT_GRAY);
    Font f = new Font("MS UI Gothic", Font.BOLD, 40);
    Font f2 = new Font("Engravers MT", Font.PLAIN, 40);
    int bHeight = 80;
    int bWidth = 70;
    text.setBackground(Color.GREEN);
    text.setFont(f2);
    text.setPreferredSize(new Dimension(bHeight, bWidth));
    text.setEditable(false);


    frame.add(text, BorderLayout.NORTH);

    oneButton.setPreferredSize(new Dimension(bHeight, bWidth));
    oneButton.setBackground(Color.DARK_GRAY);
    oneButton.setForeground(Color.GREEN);
    oneButton.setFont(f);
    oneButton.addActionListener(new Evt());
    frame.add(oneButton);

    twoButton.setPreferredSize(new Dimension(bHeight, bWidth));
    twoButton.setBackground(Color.DARK_GRAY);
    twoButton.setForeground(Color.GREEN);
    twoButton.setFont(f);
    frame.add(twoButton);

    threeButton.setPreferredSize(new Dimension(bHeight, bWidth));
    threeButton.setBackground(Color.DARK_GRAY);
    threeButton.setForeground(Color.GREEN);
    threeButton.setFont(f);
    frame.add(threeButton);

    plusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    plusButton.setBackground(Color.DARK_GRAY);
    plusButton.setForeground(Color.GREEN);
    plusButton.setFont(f);
    frame.add(plusButton);

    fourButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fourButton.setBackground(Color.DARK_GRAY);
    fourButton.setForeground(Color.GREEN);
    fourButton.setFont(f);
    frame.add(fourButton);

    fiveButton.setPreferredSize(new Dimension(bHeight, bWidth));
    fiveButton.setBackground(Color.DARK_GRAY);
    fiveButton.setForeground(Color.GREEN);
    fiveButton.setFont(f);
    frame.add(fiveButton);

    sixButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sixButton.setBackground(Color.DARK_GRAY);
    sixButton.setForeground(Color.GREEN);
    sixButton.setFont(f);
    frame.add(sixButton);

    minusButton.setPreferredSize(new Dimension(bHeight, bWidth));
    minusButton.setBackground(Color.DARK_GRAY);
    minusButton.setForeground(Color.GREEN);
    minusButton.setFont(f);
    frame.add(minusButton);

    sevenButton.setPreferredSize(new Dimension(bHeight, bWidth));
    sevenButton.setBackground(Color.DARK_GRAY);
    sevenButton.setForeground(Color.GREEN);
    sevenButton.setFont(f);
    frame.add(sevenButton);

    eightButton.setPreferredSize(new Dimension(bHeight, bWidth));
    eightButton.setBackground(Color.DARK_GRAY);
    eightButton.setForeground(Color.GREEN);
    eightButton.setFont(f);
    frame.add(eightButton);

    nineButton.setPreferredSize(new Dimension(bHeight,bWidth));
    nineButton.setBackground(Color.DARK_GRAY);
    nineButton.setForeground(Color.GREEN);
    nineButton.setFont(f);
    frame.add(nineButton);

    timesButton.setPreferredSize(new Dimension(bHeight, bWidth));
    timesButton.setBackground(Color.DARK_GRAY);
    timesButton.setForeground(Color.GREEN);
    timesButton.setFont(f);
    frame.add(timesButton);


    zeroButton.setPreferredSize(new Dimension(bHeight, bWidth));
    zeroButton.setBackground(Color.DARK_GRAY);
    zeroButton.setForeground(Color.GREEN);
    zeroButton.setFont(f);
    frame.add(zeroButton);


    enterButton.setPreferredSize(new Dimension(190, bWidth));
    enterButton.setBackground(Color.DARK_GRAY);
    enterButton.setForeground(Color.GREEN);
    enterButton.setFont(f);
    frame.add(enterButton);

    dividButton.setPreferredSize(new Dimension(bHeight, bWidth));
    dividButton.setBackground(Color.DARK_GRAY);
    dividButton.setForeground(Color.GREEN);
    dividButton.setFont(f);
    frame.add(dividButton);

    frame.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);


}


private static Dimension Dimension(int bHeight, int bWidth) {
    // TODO Auto-generated method stub
    return null;
}



private static void setLayout(FlowLayout fl) {
    // TODO Auto-generated method stub

}



public static void main(String[] args){
 new Ken();
}


private class Evt implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == oneButton){

    }

    }

}


}

有什么帮助吗?

【问题讨论】:

    标签: java jbutton actionlistener event-listener actionevent


    【解决方案1】:

    您已经在主类“Ken”上实现了ActionListener 接口,因此在这种情况下您不需要Evt 类。

    如果这仍然有效,我感到很惊讶,因为你的主类使用 implements ActionListener 但没有实现所需的功能,你应该得到一个错误。

    因此,在您的主类中实现actionPerformed 而不是Evt,并且当您将actionlistener 附加到您使用this 的按钮时。

    在你的动作监听器中你应该这样做

    if(e.getSource().equals(oneButton)
    

    但这不是问题。

    并且在您的事件处理程序中,您必须更新显示数字的 gui 元素。仅将输入设置为 1 是不够的。所以你应该让JTextField text 也是一个成员变量,然后你可以用这个来更新它的内容:

    input = 1;
    text.setText(""+input);
    

    此外,您不应该将所有元素都设为静态。无论如何,它们都被声明为类成员。

    【讨论】:

    • 谢谢,在对 actionPerformed 使用 System.out.println() 进行一些调试后,我得出的结论是问题是输入变量没有改变。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 2013-09-28
    • 1970-01-01
    • 2019-05-22
    • 2021-08-12
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多