package swingtraining;
import static java.awt.Color.BLACK;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.NORTH;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameTest extends JFrame {
public JFrameTest() {
setSize(500, 500);
setTitle("Hello :D");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static class JPanelTest extends JPanel {
public JPanelTest() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 100;
gbc.ipady = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
setBackground(BLACK);
setOpaque(true);
add(new JButton("Hello"), gbc);
}
}
public static class JButtonTest extends JButton {
JButtonTest() {
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrameTest T = new JFrameTest();
JPanelTest Jp1 = new JPanelTest();
T.add(Jp1);
}
});
}
}
This code works fine, but I don't like how my Button
is just placed exactly to the LEFT, with only the ability to change the size (width, height) of the bButton. So I was wondering if there was a more precise way to place things, like maybe just telling Java to place it on the Panel
using X Y coordinates or something, like;
JButton.setLocation(200,200);
Any suggestions?