I am struggling controlling my components. I hava a JFrame which contains a JPanel. My components such as JLabel and JTextArea are added to this Panel. So my question is:
How can I control these compontens? I’ve tried using
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 1;
But it doesn’t seems to work..
Here is my function the initialize my GUI:
public void initGUI()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final JFrame frame = new JFrame("instantINFO!");
frame.setSize(screenSize.width, screenSize.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
final JPanel panel = new JPanel();
weather = new JTextArea(vær, 6, 20);
weather.setFont(new Font("Arial", Font.BOLD, 16));
weather.setLineWrap(true);
weather.setWrapStyleWord(true);
weather.setOpaque(false);
weather.setEditable(false);
stedLabel = new JLabel(sted);
dagLabel = new JLabel(dag);
panel.add(weather);
panel.add(stedLabel);
panel.add(dagLabel);
frame.add(panel);
frame.setVisible(true);
}
For instance I want the weather label to be in the left upper corner, with a few pixels of margin.
A JPanel uses a FlowLayout by default. And by default components are centered in a row on the panel. If you want the components left aligned then you need to change the flow layout to left align the components. Read the FlowLayout API to see how to do this.
I also suggest you look at the Swing tutorial on Using Layout Managers for working examples.