我做错了什么,第二个按钮起作用很奇怪?

发布于 2025-01-22 07:10:28 字数 1889 浏览 5 评论 0原文

我知道代码很糟糕且容易。我是一个入门者,需要一些帮助。我想在我的代码中添加第二个按钮。我觉得自己更改了按钮的坐标,因此不会“落在第一个”上。有什么建议吗? (同样,这对我来说有点未知,所以我只是复制 - 粘贴它,希望它不会影响第二个按钮。我觉得这与我输入的按钮的坐标有关)。

import java.awt.*;

import java.awt.event.*;

public class CalculatorGUI extends Frame implements ActionListener

{

static Operand op;
TextField display;
Button button0;
Button button1;
public CalculatorGUI(String title, Operand op)
{               
    super(title);
    CalculatorGUI.op = op;
    this.setLayout(null);
    this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    this.setBackground(Color.blue);
    button0 = new Button("0");
    button0.setBounds(64, 265, 35, 28);
    button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button0.setForeground(Color.blue);
    button0.setFocusable(false);
    button0.addActionListener(this);
    this.add(button0);
    this.setSize(283,320);
    this.setLocation(40,30);
    this.setVisible(true);
    this.setResizable(false);
    
    button1 = new Button("1");
    button1.setBounds(64, 230, 35, 28);
    button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button1.setForeground(Color.blue);
    button1.setFocusable(false);
    button1.addActionListener(this);
    this.add(button1);
    this.setSize(283,320);
    this.setLocation(40,30);
    this.setVisible(true);
    this.setResizable(false);
    
    this.addWindowListener(new CloseWindowAndExit());
    display = new TextField("");
    display.setEditable(false);
    display.setBounds(13, 55, 257, 30);
    this.add(display);
    
}

class CloseWindowAndExit extends WindowAdapter
{
    public void windowClosing(WindowEvent closeWindowAndExit)
    {
        System.exit(0);
    }
}

@Override
public void actionPerformed(ActionEvent e) 
{
    
    if(e.getSource()==button0) 
    {
      display.setText("Button0 is pressed");
      CalculatorGUI.op.addDigit('0');
    }
}

}

I know the code is bad and easy. I am a starter and need some help. I want to add a second button to my code. I feel like I changed the coordinates of the button so it doesn't "fall" on the first one. Any suggestions? (Also, .this is a bit unknown to me so I just copy - pasted it hoping it doesnt affect the second button. I feel like it has to do with .this and the coordinates of the button I entered).

import java.awt.*;

import java.awt.event.*;

public class CalculatorGUI extends Frame implements ActionListener

{

static Operand op;
TextField display;
Button button0;
Button button1;
public CalculatorGUI(String title, Operand op)
{               
    super(title);
    CalculatorGUI.op = op;
    this.setLayout(null);
    this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    this.setBackground(Color.blue);
    button0 = new Button("0");
    button0.setBounds(64, 265, 35, 28);
    button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button0.setForeground(Color.blue);
    button0.setFocusable(false);
    button0.addActionListener(this);
    this.add(button0);
    this.setSize(283,320);
    this.setLocation(40,30);
    this.setVisible(true);
    this.setResizable(false);
    
    button1 = new Button("1");
    button1.setBounds(64, 230, 35, 28);
    button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button1.setForeground(Color.blue);
    button1.setFocusable(false);
    button1.addActionListener(this);
    this.add(button1);
    this.setSize(283,320);
    this.setLocation(40,30);
    this.setVisible(true);
    this.setResizable(false);
    
    this.addWindowListener(new CloseWindowAndExit());
    display = new TextField("");
    display.setEditable(false);
    display.setBounds(13, 55, 257, 30);
    this.add(display);
    
}

class CloseWindowAndExit extends WindowAdapter
{
    public void windowClosing(WindowEvent closeWindowAndExit)
    {
        System.exit(0);
    }
}

@Override
public void actionPerformed(ActionEvent e) 
{
    
    if(e.getSource()==button0) 
    {
      display.setText("Button0 is pressed");
      CalculatorGUI.op.addDigit('0');
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-01-29 07:10:28

请勿将按钮直接添加到主帧。相反,创建另一个面板并将按钮添加到它:

    Panel newPanel = new Panel(new GridBagLayout());

    Button button0 = new Button("BUTTON_0");
    button0.setBounds(10, 265, 30, 30);
    button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button0.setForeground(Color.blue);
    button0.setFocusable(false);

    Button button1 = new Button("BUTTON_1");
    button1.setBounds(10, 265, 30, 30);
    button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button1.setForeground(Color.blue);
    button1.setFocusable(false);

    newPanel.add(button0);
    newPanel.add(button1);

    this.add(newPanel);

Do not add buttons directly to the main frame. Instead, create another panel and add the buttons to it:

    Panel newPanel = new Panel(new GridBagLayout());

    Button button0 = new Button("BUTTON_0");
    button0.setBounds(10, 265, 30, 30);
    button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button0.setForeground(Color.blue);
    button0.setFocusable(false);

    Button button1 = new Button("BUTTON_1");
    button1.setBounds(10, 265, 30, 30);
    button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
    button1.setForeground(Color.blue);
    button1.setFocusable(false);

    newPanel.add(button0);
    newPanel.add(button1);

    this.add(newPanel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文