-1

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 
            }    
            }

EightSquared
  • 37
  • 2
  • 13
  • 3
    First you would check out [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) and then have a look at [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to Write an Action Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer Jul 30 '14 at 00:21
  • 2
    This question appears to be off-topic because it can easily be answered by reading the appropriate tutorials – MadProgrammer Jul 30 '14 at 00:24

2 Answers2

2

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);

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) Edit: If you want to add functionality to your button just use:

 yourButton.addActionListener(new ActionListener() 
 {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //button logic
        }
 });
mlethys
  • 436
  • 9
  • 29
0

call frame.add(Button) for each button. Afterwards frame.pack() once.

Gumbo
  • 1,716
  • 1
  • 15
  • 22
  • I think FlowLayout is the default – Gumbo Jul 31 '14 at 15:35
  • You can think that, I can tell you for a fact, JFrame uses a BorderLayout by default, JPanel uses a FlowLayout by default. From the [JavaDocs](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html): *"The default content pane will have a BorderLayout manager set on it."* – MadProgrammer Jul 31 '14 at 20:51
  • @MadProgrammer sorry i thougth it was FlowLayout for both – Gumbo Aug 01 '14 at 17:49