当使用BorderLayout布局多个JPanel时,当重新调整包含对话框时,是什么决定哪个面板应该按照其他面板的成本保持其大小?
在下面的示例代码中,我希望包含JComboBox和JCheckBoxes的南面板在重新调整对话框大小时保持其大小。
原始尺寸 重新调整大小,南方小组搞砸了
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JList;
import javax.swing.border.TitledBorder;
import javax.swing.BoxLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class GrantPrivilegesDialog extends JDialog {
private JTable queueTable;
private JComboBox groupComboBox;
private JCheckBox receiveCheckBox;
private JCheckBox sendCheckBox;
private JCheckBox browseCheckBox;
private JCheckBox viewCheckBox;
private JCheckBox createCheckBox;
private JCheckBox deleteCheckBox;
private JCheckBox modifyCheckBox;
private JCheckBox purgeCheckBox;
private TableRowSorter<DefaultTableModel> queueSorter;
private DefaultTableModel queueModel;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
GrantPrivilegesDialog dialog = new GrantPrivilegesDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public GrantPrivilegesDialog() {
setTitle("Grant Privileges To Group(s)");
setBounds(100, 100, 866, 461);
getContentPane().setLayout(new BorderLayout());
{
JPanel northPanel = new JPanel();
northPanel.setLayout(new BorderLayout());
getContentPane().add(northPanel, BorderLayout.NORTH);
{
queueTable = new JTable();
//Object[][] rows = null;
Object[][] rows ={ {"*", "localhost1", "someQueue1"},
{"", "localhost2", "someQueue2"},
{"", "localhost3", "someQueue3"},
{"", "localhost4", "someQueue4"},
{"", "localhost5", "someQueue5"},
{"", "localhost6", "someQueue6.sdfsdfssdf.sfsdfsdfpweerpowerpoiwer.werjsdgf.sdföw.slksdfsdf"}
};
String[] queueColumnNames = {"","Server","QueueName"};
queueModel = new DefaultTableModel(rows, queueColumnNames){
@Override
public Class getColumnClass(int colNum) {
switch (colNum) {
case 0:
return String.class; //Asterisk (*) for dynamic
case 1:
return String.class; //Server
case 2:
return String.class; //Queue Name
default:
return String.class;
}
}
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
queueTable.setModel(queueModel);
queueSorter = new TableRowSorter<DefaultTableModel>(queueModel);
queueTable.setRowSorter(queueSorter);
queueTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
queueTable.getColumnModel().getColumn(0).setPreferredWidth(2);
queueTable.getColumnModel().getColumn(0).setMaxWidth(2);
queueTable.getColumnModel().getColumn(1).setPreferredWidth(225);
queueTable.getColumnModel().getColumn(2).setPreferredWidth(600);
queueTable.setCellSelectionEnabled(true);
JScrollPane queueScrollPane = new JScrollPane(queueTable);
northPanel.add(queueScrollPane, BorderLayout.CENTER);
queueScrollPane.setViewportBorder(new TitledBorder(null, "Queues", TitledBorder.LEADING, TitledBorder.TOP, null, null));
{
queueScrollPane.setViewportView(queueTable);
}
}
}
{
JPanel southPanel = new JPanel();
getContentPane().add(southPanel, BorderLayout.SOUTH);
southPanel.setLayout(new BorderLayout());
{
{
//JList<? extends E> list = new JList();
//panel_1.add(list, BorderLayout.NORTH);
}
}
{
JPanel privilegesPanel = new JPanel();
privilegesPanel.setBorder(BorderFactory.createTitledBorder("Grant"));
southPanel.add(privilegesPanel, BorderLayout.CENTER);
privilegesPanel.setLayout(new BorderLayout());
{
JPanel groupListPanel = new JPanel();
groupListPanel.setBorder(BorderFactory.createTitledBorder("Select Group"));
privilegesPanel.add(groupListPanel, BorderLayout.WEST);
groupListPanel.setLayout(new BorderLayout());
{
String[] choices = {"GroupA", "GroupB", "GroupC", "GroupD", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE", "GroupE"};
groupComboBox = new JComboBox(choices);
groupComboBox.setEditable(true);
groupComboBox.setPreferredSize(new Dimension(300,10));
groupComboBox.setSelectedIndex(4);
groupListPanel.add(groupComboBox, BorderLayout.EAST);
}
}
{
JPanel privilegesCheckBoxPanel = new JPanel();
privilegesCheckBoxPanel.setBorder(BorderFactory.createTitledBorder("Privileges"));
privilegesPanel.add(privilegesCheckBoxPanel, BorderLayout.EAST);
privilegesCheckBoxPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
{
receiveCheckBox = new JCheckBox("receive");
receiveCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(receiveCheckBox);
}
{
sendCheckBox = new JCheckBox("send");
sendCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(sendCheckBox);
}
{
browseCheckBox = new JCheckBox("browse");
browseCheckBox.setSelected(true);
privilegesCheckBoxPanel.add(browseCheckBox);
}
{
viewCheckBox = new JCheckBox("view");
privilegesCheckBoxPanel.add(viewCheckBox);
}
{
createCheckBox = new JCheckBox("create");
privilegesCheckBoxPanel.add(createCheckBox);
}
{
deleteCheckBox = new JCheckBox("delete");
privilegesCheckBoxPanel.add(deleteCheckBox);
}
{
modifyCheckBox = new JCheckBox("modify");
privilegesCheckBoxPanel.add(modifyCheckBox);
}
{
purgeCheckBox = new JCheckBox("purge");
privilegesCheckBoxPanel.add(purgeCheckBox);
}
}
}
{
JPanel confirmPane = new JPanel();
southPanel.add(confirmPane, BorderLayout.SOUTH);
confirmPane.setLayout(new BorderLayout(0, 0));
{
JPanel panel = new JPanel();
confirmPane.add(panel, BorderLayout.EAST);
{
JButton grantButton = new JButton("GRANT");
panel.add(grantButton);
grantButton.setActionCommand("GRANT");
getRootPane().setDefaultButton(grantButton);
grantButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(receiveCheckBox.isSelected());
System.out.println(sendCheckBox.isSelected());
System.out.println(browseCheckBox.isSelected());
System.out.println(viewCheckBox.isSelected());
System.out.println(createCheckBox.isSelected());
System.out.println(deleteCheckBox.isSelected());
System.out.println(modifyCheckBox.isSelected());
System.out.println(purgeCheckBox.isSelected());
}
} );
}
{
JButton cancelButton = new JButton("CLOSE");
panel.add(cancelButton);
cancelButton.setActionCommand("CLOSE");
}
}
{
JLabel errorMessageLabel = new JLabel("");
confirmPane.add(errorMessageLabel, BorderLayout.WEST);
}
}
}
setMinimumSize(new Dimension(870, 300));
pack();
}
}
答案 0 :(得分:2)
将getContentPane().add(northPanel, BorderLayout.NORTH);
更改为getContentPane().add(northPanel, BorderLayout.CENTER);
,带表格的面板可调整大小,第二个面板将固定大小:
另请阅读BorderLayout
。我建议您使用另一个布局管理器,例如GridBagLayout
,因为它更适合复杂的表单。