如何显示JButton并赋予其功能? [英] How to display a JButton and give it functionality?

查看:84
本文介绍了如何显示JButton并赋予其功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题.
首先,我试图在JFrame上显示JButton,到目前为止,我已经设法在JFrame上不显示任何内容.
其次,如何为按钮添加功能?您会通过一种方法吗? 任何反馈表示赞赏.

Two questions.
Firstly I'm trying to display a JButton on a JFrame, so far I've managed to display the JFrame with nothing on it.
Secondly how would one add functionality to a button? Would you pass it a method? Any feedback is appreciated.

           <code>
           //imports SWING etc...
           //global variables...

            public class FahrenheitGUI {
            public static void main(String args[]){
            prepareGUI();
            }

            private static void prepareGUI(){
            JFrame frame = new JFrame("Temp");
            JPanel panel = new JPanel();
            JLabel temperatureLabel;
            int h = 300; int w = 300;
            frame.setSize(h,w);

            JButton one = new JButton( "0" );
            JButton two = new JButton( "1" );
            JButton three = new JButton( "2" );
            JButton four = new JButton( "3" );
            JButton five = new JButton( "4" );
            JButton six = new JButton( "5" );
            JButton seven = new JButton( "6" );
            JButton eight = new JButton( "7" );
            JButton nine = new JButton( "8" );
            JButton ten = new JButton( "9" );
            JButton negative = new JButton( "-" );
            JButton dot = new JButton( "." );
            JButton reset = new JButton( "reset" );

            one.setBounds(10,10,20,20);

            //one.addActionListener(onButtonPress);
            //creates an error

            frame.setVisible(true);
            }

            }



            class Keypad implements ActionListener{

            public void actionPerformed( ActionEvent one){
            // guessing 
            }
            public void actionPerformed( ActionEvent two){
            // guessing 
            }    
            }

推荐答案

您可以创建JPanel,向面板中添加按钮,然后向JFrame中添加整个面板,如下所示:

You could create JPanel, add buttons to your panel and then and the whole panel to your JFrame like this:

JPanel panel = new JPanel(); //by default it will has FlowLayout
panel.add(yourButton);
frame.add(yourJPanel);
frame.setVisible(true);

我个人创建扩展JPanel的类,并在其中设置面板的大小(而不是框架的大小),然后在将面板添加到我的框架后,我调用pack()方法,该方法将参考以下内容调整框架的大小面板的大小. 如果要更改默认布局管理器,只需调用setLayout(LayoutManager) 如果要向按钮添加功能,请使用:

Personally I create class that extends JPanel, and inside of it I set size for panel (not for frame) and then, after adding panel to my frame I call pack() method which will resize your frame in reference of the size of your panel. If you want to change default layout manager just call setLayout(LayoutManager) If you want to add functionality to your button just use:

 yourButton.addActionListener(new ActionListener() 
 {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //button logic
        }
 });

这篇关于如何显示JButton并赋予其功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆