Jump to content

Help with multiple buttons....

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
I was learning through old tutorials from codecall. http://forum.codecal...our-window.html
But I was struck and went through my own way. If you know tell me what is wrong with this code.
package math;

import javax.swing.*;

import java.awt.event.*;


public class test extends JFrame implements ActionListener{



public test(){

    JFrame frame = new JFrame("Jesus");

    JButton buttonOne = new JButton("St.Peter's");

    JButton buttonTwo = new JButton("St.Anthony");

    JButton buttonThree = new JButton("St.Sebastian");

    frame.setVisible(true);

    frame.setSize(400, 400);

    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    buttonOne.addActionListener(this);

    buttonTwo.addActionListener(this);

    buttonThree.addActionListener(this);

    frame.add(buttonOne);

    frame.add(buttonTwo);

    frame.add(buttonThree);


}

public void actionPerformed(ActionEvent e){

    System.out.println("I love you, I don't let you down. I pray for you!");

}

public static void main(String args[]){

        new test();

    }


}


#2
keller

keller

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Well to start you need to create your test, test t = new test();
then you gotta show it t.show();
And your output is going to standard out (command line).
and package math; isn't needed really.

#3
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
You could give the frame a layout so the buttons all show on the frame better for example add the line "frame.setLayout(new FlowLayout());" to your constructor. And it always helps to have the "frame.setVisible(true);" at the bottom after you add all the buttons so everything for sure shows up when the frame starts.
twas brillig

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Any panel or frame you create has a borderLayout by default.
By doing frame.add(button1) times you actually do frame.add(button1, BorderLayout.Center);
Because you add three buttons to the center they are just placed on top of eachother. You can either change the layout of the whole frame as tate said, or add the buttons to other places (West, South,North,East, Center).
If you want them all in the, let's say, south. You must create a panel with a flow/grid/other layout, add the 3 buttons to the panel, and then add the panel to the BorderLayout.South of the frame.

The following page nicely shows what each layout does: http://java.sun.com/...out/visual.html