-Java- Swing GUI - Moving around components specifically with layouts

Posted by Xemiru Scarlet Sanzenin on Stack Overflow See other posts from Stack Overflow or by Xemiru Scarlet Sanzenin
Published on 2012-09-30T03:09:27Z Indexed on 2012/09/30 3:37 UTC
Read the original article Hit count: 167

Filed under:
|
|
|

I'm making a little test GUI for something I'm making. However, problems occur with the positioning of the panels.

public winInit() {
super("Chatterbox - Login");

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (UnsupportedLookAndFeelException e) {
}

setSize(300,135);

pn1 = new JPanel();
pn2 = new JPanel();
pn3 = new JPanel();

l1 = new JLabel("Username");
l2 = new JLabel("Password");
l3 = new JLabel("Random text here");
l4 = new JLabel("Server Address");
l5 = new JLabel("No address set.");

i1 = new JTextField(10);

p1 = new JPasswordField(10);

b1 = new JButton("Connect");
b2 = new JButton("Register");
b3 = new JButton("Set IP");

l4.setBounds(10, 12, getDim(l4).width, getDim(l4).height);
l1.setBounds(10, 35, getDim(l1).width, getDim(l1).height);
l2.setBounds(10, 60, getDim(l2).width, getDim(l2).height);
l3.setBounds(10, 85, getDim(l3).width, getDim(l3).height);
l5.setBounds(l4.getBounds().width + 14, 12, l5.getPreferredSize().width, l5.getPreferredSize().height);

l5.setForeground(Color.gray);

i1.setBounds(getDim(l1).width + 15, 35, getDim(i1).width, getDim(i1).height);
p1.setBounds(getDim(l1).width + 15, 60, getDim(p1).width, getDim(p1).height);

b1.setBounds(getDim(l1).width + getDim(i1).width + 23, 34, getDim(b2).width, getDim(b1).height - 5);
b2.setBounds(getDim(l1).width + getDim(i1).width + 23, 60, getDim(b2).width, getDim(b2).height - 5);
b3.setBounds(getDim(l1).width + getDim(i1).width + 23, 10, etDim(b2).width, getDim(b3).height - 5);

b1.addActionListener(clickButton);
b2.addActionListener(clickButton);
b3.addActionListener(clickButton);

pn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
pn2.setLayout(new FlowLayout(FlowLayout.RIGHT));

pn1.add(l1);
pn1.add(i1);
pn1.add(b1);

pn2.add(l2);
pn2.add(p1);
pn2.add(b2);

add(pn1);
add(pn2);

}

I am attempting to use FlowLayout to position the panels in the way desired. I'd use BorderLayout while adding, but the vertical spacing is too far away when I just use directions closest to one another.

The output of this code is to create a window, 300,150, place whatever's in the two panels in the exact same spaces.

Yes, I realize there's useless code there with setBounds(), but that was just me screwing around with Absolute Positioning, which wasn't working out for me either.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing