Java GUI:修改计算器程序

发布于 2024-10-24 19:45:53 字数 2222 浏览 2 评论 0原文

我正在尝试修改老师给我的计算器程序。它应该由主程序和 GUI 程序组成。我唯一的问题是如何处理事件。如你看到的 除了主程序之外,我还创建了 Numerics 类。我想要发生的是,当我单击一个数字时,它应该将数字程序中的值扔到主程序中 NorthPanel 类中的文本字段。但我不知道怎么办。谁能给我任何关于如何做到这一点的想法?

这是主程序的一部分

public class NorthPanel extends JPanel {
    private JTextField display;
    private JLabel filler;

    public NorthPanel() {
    //receive the thrown value from Numerics program to be displayed
        setLayout(new BorderLayout());
        String calcTF="0.";
        display = new JTextField(calcTF);
        display.setEditable(false);
        display.setFont(new Font("Century Gothic",Font.BOLD,19));
        display.setHorizontalAlignment(JTextField.RIGHT);                                   
        add(display,BorderLayout.CENTER);                   
    }
}

public class CenterPanel extends JPanel {

    private Numerics numeric;
    private Operations operator;
    private Functions function;

    public CenterPanel() {
        setLayout(null);

        numeric = new Numerics();
        numeric.setBounds(5,5,150,150);

        operator = new Operations();
        operator.setBounds(158,5,45,150);

        function = new Functions();
        function.setBounds(204,5,55,150);

        add(numeric);               
        add(operator);
        add(function,0);
    }
}

,这是数值程序的一部分

public class Numerics extends JPanel implements ActionListener
{
    private JButton c7;
    String value="";
    public Numerics() 
    {
        UIManager.put("Button.background", Color.gray);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc=new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0,3,5,3);

        c7=new JButton("7");
        c7.setFont(new Font("Arial",Font.BOLD,20));
        c7.setBorder(BorderFactory.createRaisedBevelBorder());
        gbc.gridx=0;
        gbc.gridy=0;
        add(c7,gbc);
        c7.addActionListener(this);
public void actionPerformed(ActionEvent ae1)
    {
        if(ae1.getSource()==c7)
        {
            value+="7";
            //throw the value to display in the NorthPanel...
        }
    }

I'm trying to modify the calculator program our teacher gave to me. It's supposed to be composed of the main and GUI programs. My only problem is how to handle events. As you can see
I created the Numerics class apart from the main program. What I want to happen is that when I click a number it's supposed to throw the value from the numerics program to the textfield in the NorthPanel class in the main program. But I don't know how. Can anyone give me any ideas on how to do this?

Here is part of the main program

public class NorthPanel extends JPanel {
    private JTextField display;
    private JLabel filler;

    public NorthPanel() {
    //receive the thrown value from Numerics program to be displayed
        setLayout(new BorderLayout());
        String calcTF="0.";
        display = new JTextField(calcTF);
        display.setEditable(false);
        display.setFont(new Font("Century Gothic",Font.BOLD,19));
        display.setHorizontalAlignment(JTextField.RIGHT);                                   
        add(display,BorderLayout.CENTER);                   
    }
}

public class CenterPanel extends JPanel {

    private Numerics numeric;
    private Operations operator;
    private Functions function;

    public CenterPanel() {
        setLayout(null);

        numeric = new Numerics();
        numeric.setBounds(5,5,150,150);

        operator = new Operations();
        operator.setBounds(158,5,45,150);

        function = new Functions();
        function.setBounds(204,5,55,150);

        add(numeric);               
        add(operator);
        add(function,0);
    }
}

and here is part of the Numerics program

public class Numerics extends JPanel implements ActionListener
{
    private JButton c7;
    String value="";
    public Numerics() 
    {
        UIManager.put("Button.background", Color.gray);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc=new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0,3,5,3);

        c7=new JButton("7");
        c7.setFont(new Font("Arial",Font.BOLD,20));
        c7.setBorder(BorderFactory.createRaisedBevelBorder());
        gbc.gridx=0;
        gbc.gridy=0;
        add(c7,gbc);
        c7.addActionListener(this);
public void actionPerformed(ActionEvent ae1)
    {
        if(ae1.getSource()==c7)
        {
            value+="7";
            //throw the value to display in the NorthPanel...
        }
    }

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

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

发布评论

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

评论(1

肤浅与狂妄 2024-10-31 19:45:53

查看事件监听器简介 &特别是 Java 教程的 如何编写操作侦听器 部分。

Look into Introduction to Event Listeners & specifically the How to Write an Action Listener sections of the Java Tutorial.

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