首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >开始java swing,给出按钮功能

开始java swing,给出按钮功能
EN

Stack Overflow用户
提问于 2015-03-29 15:20:40
回答 2查看 452关注 0票数 -1

我现在在一个做swing入门的班上。我们的任务是创建一个计算器GUI。我制作了一个结合了边框布局和网格布局的GUI。我不确定如何赋予按钮功能,我看了一遍课堂讲稿,却找不到它。

我在网上看到过一些例子,但看起来都一样,我不认为它们有帮助,因为我看到的那些只使用一个按钮,我需要一种方法来区分我的按钮,因为每个按钮都有自己的操作。

要注意的是,我不需要这个计算器的任何功能,我只需要它来记录按钮点击,一旦我弄清楚了,我将把它添加到一个字符串“输入”,并保持对用户的视觉。例如,如果用户(老师)点击5*5,这需要显示在屏幕顶部,但我不需要25的答案。

这是我的代码,它运行并具有我想要的gui布局。

代码语言:javascript
复制
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;


public class GUI extends JFrame implements ItemListener{ //ActionListener
    //JTextArea info = new JTextArea("this will calculate numbers");
    JTextField f1 = new JTextField(10); //param = width
    protected String input;

    //*****************************************************************************************************************




    //constructor
    //@ args String, Int, Int
    public GUI(String title, int width, int height ){
        //gui.pack() *try this later*

        setBounds(500, 170, width, height);// sets location x,y on screen at start of program also sets size of GUI x,y

        setTitle(title); //sets title of

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //needed else x wont close properly



        createGUI();

    }

    //separate gui logic from other logic
    public void createGUI(){

        //set layout you plan to use
        setLayout(new BorderLayout());
        //add(info, BorderLayout.NORTH);
        add(f1, BorderLayout.NORTH);

        //CENTER********************************************************************************************************
        JPanel center = new JPanel();
        center.setLayout(new GridLayout(4,3));
        center.add(new JButton("1"));
        center.add(new JButton("2"));
        center.add(new JButton("3"));
        center.add(new JButton("4"));
        center.add(new JButton("5"));
        center.add(new JButton("6"));
        center.add(new JButton("7"));
        center.add(new JButton("8"));
        center.add(new JButton("9"));
        center.add(new JButton("0"));
        center.add(new JButton("."));
        center.add(new JButton("C"));
        add(center, BorderLayout.CENTER);
        //EAST**********************************************************************************************************
        JPanel east = new JPanel();
        east.setLayout(new GridLayout(6,1));
        east.add(new JButton("+"));
        east.add(new JButton("-"));
        east.add(new JButton("x"));
        east.add(new JButton("/"));
        east.add(new JButton("%"));
        east.add(new JButton("="));
        add(east, BorderLayout.EAST);
    }


    //main
    public static void main(String[] args){
        GUI calculator = new GUI("GUI Calculator",300,250);

        calculator.setVisible(true);//makes calculator gui visible *make this line last or close to last*

    }

    //Must do this method or else class must be made abstract
    public void itemStateChanged(ItemEvent event){

    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-03-29 15:26:36

实际上,对于每个按钮,您需要为其分配一个方法,当用户激活该按钮时,该方法将调用它的actionPerformed方法。

就我个人而言,我会使用Action,这是一个自我延续的工作单元,但您可以使用内部类或匿名类,我会避免使用一个大型actionPerformed方法,因为随着时间的推移,它会变得难以管理

有关更多详细信息,请参阅How to Write an Action ListenersHow to Use ActionsHow to Use Buttons, Check Boxes, and Radio Buttons

票数 2
EN

Stack Overflow用户

发布于 2015-03-29 15:31:08

需要给按钮添加ActionListener;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29326654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档