How to place a JButton at a desired location in a JFrame using Java?
I want to put a JButton
at a particular coordinate in a JFrame
. I used setBounds()
for the 开发者_JAVA百科JPanel
(which I placed on the JFrame
) and also setBounds()
for the JButton
. However, they don't seem to function as expected.
My Output:
This is my code:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Control extends JFrame {
// JPanel
JPanel pnlButton = new JPanel();
// Buttons
JButton btnAddFlight = new JButton("Add Flight");
public Control() {
// FlightInfo setbounds
btnAddFlight.setBounds(60, 400, 220, 30);
// JPanel bounds
pnlButton.setBounds(800, 800, 200, 100);
// Adding to JFrame
pnlButton.add(btnAddFlight);
add(pnlButton);
// JFrame properties
setSize(400, 400);
setBackground(Color.BLACK);
setTitle("Air Traffic Control");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Control();
}
}
How do I place the JButton
at coordinate (0, 0)?
Following line should be called before you add your component
pnlButton.setLayout(null);
Above will set your content panel to use absolute layout. This means you'd always have to set your component's bounds explicitly by using setBounds
method.
In general I wouldn't recommend using absolute layout.
Use child.setLocation(0, 0)
on the button, and parent.setLayout(null)
. Instead of using setBounds(...) on the JFrame to size it, consider using just setSize(...)
and letting the OS position the frame.
//JPanel
JPanel pnlButton = new JPanel();
//Buttons
JButton btnAddFlight = new JButton("Add Flight");
public Control() {
//JFrame layout
this.setLayout(null);
//JPanel layout
pnlButton.setLayout(null);
//Adding to JFrame
pnlButton.add(btnAddFlight);
add(pnlButton);
// postioning
pnlButton.setLocation(0,0);
Define somewhere the consts :
private static final int BUTTON_LOCATION_X = 300; // location x
private static final int BUTTON_LOCATION_Y = 50; // location y
private static final int BUTTON_SIZE_X = 140; // size height
private static final int BUTTON_SIZE_Y = 50; // size width
and then below :
JButton startButton = new JButton("Click Me To Start!");
// startButton.setBounds(300, 50,140, 50 );
startButton.setBounds(BUTTON_LOCATION_X
, BUTTON_LOCATION_Y,
BUTTON_SIZE_X,
BUTTON_SIZE_Y );
contentPane.add(startButton);
where contentPane
is the Container
object that holds the entire frame :
JFrame frame = new JFrame("Some name goes here");
Container contentPane = frame.getContentPane();
I hope this helps , works great for me ...
You should set layout first by syntax pnlButton.setLayout()
, and then choose the most suitable layout which u want. Ex: pnlButton.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
. And then, take that JButton into JPanel.
I have figured it out. For the button do .setBounds(0, 0, 220, 30)
The .setBounds
layout has following parameters - (int x, int y, int width, int height)
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.
I created the following GUI.
All Swing applications must start with a call to the SwingUtilities
invokeLater
method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I used a JFrame
. I did not extend a JFrame
. This is referred to as composition over inheritance. The only reason you should extend a Swing component, or any Java class, is if you intend to override one or more of the class methods.
A JFrame
has a default BorderLayout
. I placed the JPanel
in the center of the JFrame
.
I created a JPanel
to place in the JFrame
. I used a FlowLayout
to place the JButton
in the center of the top of the JPanel
. If you want the JButton
to be somewhere else, you can adjust the values of the empty border to place the JButton
where you want.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class AirTrafficControl implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new AirTrafficControl());
}
@Override
public void run() {
JFrame frame = new JFrame("Air Traffic Control");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
panel.setPreferredSize(new Dimension(400, 400));
JButton btnAddFlight = new JButton("Add Flight");
panel.add(btnAddFlight);
return panel;
}
}
First, remember your JPanel size height and size width, then observe: JButton coordinates is (xo, yo, x length , y length). If your window is 800x600, you just need to write:
JButton.setBounds(0, 500, 100, 100);
You just need to use a coordinate gap to represent the button, and know where the window ends and where the window begins.
精彩评论