我已经设置了约束,以便我的组件与窗口一起调整大小。我的问题是订单被破坏,因为他们的位置下降。 GroupLayout中是否有与GridBagConstraints.anchor等效的内容?
调整大小之前:
调整大小后:
[编辑] 这是我的代码: [编辑#2] 这是我可以运行的代码:
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
@SuppressWarnings("serial")
public class ConnectUI extends JPanel {
private static JTextField textField;
private static JTable table;
public static JButton submit;
/**
* Create the panel.
*/
public static void main(String[] args){
JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new ConnectUI());
f.setVisible(true);
}
public ConnectUI() {
textField = new JTextField();
textField.setColumns(10);
JLabel lblAvailable = new JLabel("Available : ");
lblAvailable.setFont(new Font("Tahoma", Font.PLAIN, 12));
JLabel lblUsername = new JLabel("Username : ");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
JScrollPane scrollPane = new JScrollPane();
submit = new JButton("Submit");
JLabel lblClient = new JLabel("CLIENT");
lblClient.setFont(new Font("Tahoma", Font.PLAIN, 12));
JLabel lblWarning = new JLabel("Username must not exceed 15 characters and a server must be selected");
lblWarning.setFont(new Font("Tahoma", Font.PLAIN, 12));
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblUsername, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
.addComponent(lblAvailable, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(452))))
.addGroup(groupLayout.createSequentialGroup()
.addGap(196)
.addComponent(lblClient, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
.addGap(134)))
.addGap(38))
.addGroup(groupLayout.createSequentialGroup()
.addGap(200)
.addComponent(submit, GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
.addGap(198))
.addGroup(groupLayout.createSequentialGroup()
.addGap(68)
.addComponent(lblWarning, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.addGap(75))
.addGroup(groupLayout.createSequentialGroup()
.addGap(120)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
.addGap(230))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblClient, GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(31)
.addComponent(lblUsername, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblAvailable, GroupLayout.DEFAULT_SIZE, 4, Short.MAX_VALUE)
.addGap(18)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
.addGap(37)
.addComponent(lblWarning, GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(submit, GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
.addGap(69))
);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null},
{null, null, null},
{null, null, null},
},
new String[] {
"Room", "Host", "IP"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(table);
setLayout(groupLayout);
}
}