Java 组件赢得';在小程序中不调整大小
我的目标是让我的组件根据我的GridBagConstraints调整大小,但由于某种原因,当我的小程序运行时,组件会出现,但不会像我预期的那样填充整个小程序。这是我的密码: 服务器面板:Java 组件赢得';在小程序中不调整大小,java,swing,applet,resize,gridbaglayout,Java,Swing,Applet,Resize,Gridbaglayout,我的目标是让我的组件根据我的GridBagConstraints调整大小,但由于某种原因,当我的小程序运行时,组件会出现,但不会像我预期的那样填充整个小程序。这是我的密码: 服务器面板: public class ServerPanel extends JPanel{ public ServerPanel(){ setLayout(new GridBagLayout()); //use gridbag layout GridBagConstraints gbc = new
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);
}
}
扩展JApplet的ServerApplet的相关代码:
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);
}
});
}
所发生的情况是创建了正确的组件,并且面板位于小程序的中心,但它不会展开以填充整个小程序。感谢您的帮助。谢谢 当我看到这个:
但由于某些原因,当我的小程序运行时,组件会出现,但不会像我预期的那样填充整个小程序
与GridBagLayout一起使用时,我查看是否在GridBagConstraints上设置了weightx和weighty字段,因为它们保留默认值0的一个症状是组件都聚集在中心
解决方案:在需要的位置和时间设置这些字段。如果一个组件应该在x方向上展开,那么给它一个+weightx,也许是1.0,同样地给weighty