Reputation: 123
Ok so im new to programming GUIs in Java and I need some help on how to add buttons and Labels. I went around looking at an example and I figured it this was how it worked to basically add a button:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI_Tests extends JFrame{
public static void main(String[] args){
private JButton button;
button = new JButton("pls work");
add(button);
}
}
Well that didnt work at all... Can someone show me how its done and give me some pointers?
Upvotes: 0
Views: 2065
Reputation: 5663
You need to add your button to a JFrame
The basic flow should look something like this:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GUITests extends JFrame{
public GUITests() {
setTitle("Simple example");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton("pls work");
add(button);
pack();
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUITests ex = new GUITests();
ex.setVisible(true);
}
});
}
}
The GUITests class extends the JFrame
class so you can call all the methods that are visible on the JFrame
class. In the main method It's just some boiler plate code that calls your constructor for you so that you can see your basic example.
Check out the tutorials here: http://zetcode.com/tutorials/javaswingtutorial/firstprograms/
Upvotes: 1
Reputation: 209002
BorderLayout.CENTER
position. When adding components to JFrame
, if a different Layout is not specified, then you want to set a component's position, like add(button, BorderLayout.SOUTH);
Also you should use a constructor. Something like this
public class GUI_Tests extends JFrame {
public GUI_Tests(){
JButton button = new JButton("Pls work");
JLabel label = new JLabel("Pls work");
add(button, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
}
}
Also you need to remember to set the frame visible.
A simple running program would be something like this
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class GUI_Tests extends JFrame {
public GUI_Tests() {
JButton button = new JButton("Pls work");
JLabel label = new JLabel("Pls work");
add(button, BorderLayout.CENTER);
add(label, BorderLayout.WEST);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new GUI_Tests();
}
});
}
}
main
method may look somewhat crazy to you, but all it is doing is causing the program to run on the Event Dispatch Thread (EDT). I won't go into this too much as you are still a beginner.run
is that all I do is create an instance of me program, which extends JFrame
. If I didn't extend JFrame
, I would have to explicitly create a JFrame
to run the program. All GUI programs need some top-level container to run.Also you can see in the constructor, that when I added the button
and label
I set a layout position
add(button, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
as I stated earlier, when using a JFrame
without specifying a different layout, you should use position with the default `BorderLayout. Other possible positions are
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
BorderLayout.NORTH
BorderLayout.SOUTH
Take a look at [Laying Out Components Within a Container]Laying Out Components Within a Container)
pack()
. What this does is make the frame the perfect with for the preferrsed sizes of the components. It is preferred to pack()
rather then setSize()
of a frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
makes it so that when x out the frame, the program shuts down.setLocationRelativeTo(null);
set the frame's location in the center of the screensetVisible(true);
makes the frame visible. This should always be called to make the frame visible on start of the program.Please have a look at the Swing tutorials
Upvotes: 3
Reputation: 647
public class GUI_Tests extends JFrame {
private JButton button;
public GUI_Tests() {
setTitle("Title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so that the application properly closes when you click close
button = new JButton("pls work");
add(button);
pack(); // resize the frame to its contents
setLocationRelativeTo(null); // center the frame on the screen
}
public static void main(String args[]) {
// properly start a swing application
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI_Tests gui = new GUI_Tests();
gui.setVisible(true); // set the frame visible
}
});
}
}
hope this helps you understand it a bit more. i didn't use any layouts in this code as its just a simple example, however, i highly recommend you to read on built-in layouts in swing and how to use them. Here's one good start
Upvotes: 1
Reputation: 2497
type
setVisible( true );
setSize( 500 , 500 );
and I think you'll see something. Also get rid of private in the statement private JButton button
But you should really put your code in the constructor of the GUI_Tests:
import javax.swing.*;
public class GUI_Tests extends JFrame {
public GUI_Tests() {
JButton button = new JButton( "Hello World" );
add( button );
setVisible( true );
setSize( 500 , 500 );
}
final public static void main( String[] args ) {
GUI_Tests tests = new GUI_Tests();
}
}
Check out the Java tutorial for more help, possibly here: http://docs.oracle.com/javase/tutorial/uiswing/start/index.html
Upvotes: 0
Reputation: 9618
You are supposed to add your JButton
to a JFrame
, but it seems to me that you haven't even created a JFrame
yet. You should first create an instance of GUI_Tests
, then use the JFrame#add
method with your JButton
. For example:
public class GUI_Tests extends JFrame {
public GUI_Tests() {
super("My first Swing frame!");
this.setPreferredSize(new Dimension(640, 480));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create your GUI here
this.add(new JButton("Click on me :)"));
this.setVisible(true);
}
}
Actually, I wouldn't be mean to you, but I think you tried to go too fast: in my opinion, you should start by following some tutorial on how to deal with Swing :)
Upvotes: 1