My goal is to have my components resize according to my GridBagConstraints, but for some reason, when my applet is run, the components appear, but do not fill the entire applet like I expected. here is my code:
ServerPanel:
public class ServerPanel extends JPanel{
public ServerPanel(){
setLayout(new GridBagLayout()); //use gridbag layout
GridBagConstraints gbc = new GridBagConstraints();
JButton reverse = new JButton("Reverse text");
JButton send = new JButton("Send text");
JButton clear = new JButton("Clear text");
JTextField text = new JTextField("Test");
JScrollPane scrollPane = new JScrollPane(text, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//Reverse button
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = gbc.HORIZONTAL;
add(reverse, gbc);
//send button
gbc.gridy = 1;
add(send, gbc);
//clear button
gbc.gridy = 2;
add(clear,gbc);
//text field
gbc.gridy = 3;
gbc.ipadx = 20;
gbc.ipady = 20;
gbc.fill = gbc.BOTH;
add(scrollPane, gbc);
}
}
the relevant code from ServerApplet which extends JApplet:
public void init(){
//dim = getSize();
//create the GUI in the EDT
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
//System.out.println(dim);
setLayout(new BorderLayout());
add(new ServerPanel(), BorderLayout.CENTER);
}
});
}
What happens is that the right components get created, and the panel is centered in the applet, but it does not expand to fill the entire applet. Any help is appreciated. Thanks!
When I see this:
but for some reason, when my applet is run, the components appear, but do not fill the entire applet like I expected....
used in conjunction with GridBagLayout, I look to see if the weightx and weighty fields are being set on the GridBagConstraints, because a symptom of them being left at their default values of 0 is that the components all bunch up in the center.
Solution: set these fields where and when needed. If a component should expand in the x direction, then give it a + weightx, perhaps 1.0, and likewise for weighty.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.