我有一个带有 GridBagLayout 的 JPanel
panelOpslog
。
该面板上有 2 个面板。 1 个在顶部,1 个在底部。
panelSearchOpsLog
---------------------
panelShiftTeamLogging
panelSearchOpsLog
设置为向底部和右侧生长
panelOpslog = new JPanel();
tabbedPane.addTab("Opslog", null, panelOpslog, null);
GridBagLayout gbl_panelOpslog = new GridBagLayout();
gbl_panelOpslog.columnWidths = new int[]{1155, 0};
gbl_panelOpslog.rowHeights = new int[]{212, 401, 0};
gbl_panelOpslog.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panelOpslog.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
panelOpslog.setLayout(gbl_panelOpslog);
....
panelShiftTeamLogging = new JPanel();
GridBagConstraints gbc_panelShiftTeamLogging = new GridBagConstraints();
gbc_panelShiftTeamLogging.fill = GridBagConstraints.BOTH;
gbc_panelShiftTeamLogging.gridx = 0;
gbc_panelShiftTeamLogging.gridy = 1;
panelOpslog.add(panelShiftTeamLogging, gbc_panelShiftTeamLogging);
panelShiftTeamLogging.setLayout(null);
....
panelSearchOpsLog = new JPanel();
panelSearchOpsLog.setFont(new Font("Tahoma", Font.PLAIN, 10));
panelSearchOpsLog.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Search Opslog", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
GridBagConstraints gbc_panelSearchOpsLog = new GridBagConstraints();
gbc_panelSearchOpsLog.fill = GridBagConstraints.BOTH;
gbc_panelSearchOpsLog.insets = new Insets(0, 0, 5, 0);
gbc_panelSearchOpsLog.gridx = 0;
gbc_panelSearchOpsLog.gridy = 0;
panelOpslog.add(panelSearchOpsLog, gbc_panelSearchOpsLog);
GridBagLayout gbl_panelSearchOpsLog = new GridBagLayout();
gbl_panelSearchOpsLog.columnWidths = new int[]{129, 0, 0};
gbl_panelSearchOpsLog.rowHeights = new int[]{30, 160, 0};
gbl_panelSearchOpsLog.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_panelSearchOpsLog.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
panelSearchOpsLog.setLayout(gbl_panelSearchOpsLog);
现在我想要一个选项将
panelShiftTeamLogging
设置为不可见,以便面板 1 可以使用完整的空间。
我将这段代码放在按钮的动作侦听器中。它隐藏了 panelShiftTeamLogging
但 panelSearchOpsLog
未调整大小
btnHideShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean isVisible = panelShiftTeamLogging.isVisible();
panelShiftTeamLogging.setVisible(!isVisible);
panelOpslog.revalidate();
panelOpslog.repaint();
panelOpslog.getParent().revalidate();
panelOpslog.getParent().repaint();
}
});
PS:我想继续使用 GridBagLayout
编辑:一个工作示例
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JTextArea;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
public class test {
private JFrame frame;
private JTabbedPane tabbedPane;
private JPanel panel;
private JPanel panelTop;
private JButton btnNewButton;
private JPanel panelBottom;
private JTextArea textArea;
private JTable table;
private JScrollPane scrollPane;
private JScrollPane scrollPane_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1002, 533);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{988, 0};
gridBagLayout.rowHeights = new int[]{465, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
frame.getContentPane().add(tabbedPane, gbc_tabbedPane);
panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{983, 0};
gbl_panel.rowHeights = new int[]{189, 206, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
panelTop = new JPanel();
GridBagConstraints gbc_panelTop = new GridBagConstraints();
gbc_panelTop.fill = GridBagConstraints.BOTH;
gbc_panelTop.insets = new Insets(0, 0, 5, 0);
gbc_panelTop.gridx = 0;
gbc_panelTop.gridy = 0;
panel.add(panelTop, gbc_panelTop);
GridBagLayout gbl_panelTop = new GridBagLayout();
gbl_panelTop.columnWidths = new int[]{449, 0};
gbl_panelTop.rowHeights = new int[]{21, 0, 0};
gbl_panelTop.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panelTop.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
panelTop.setLayout(gbl_panelTop);
btnNewButton = new JButton("Hide / Show the lower panel. The Table should also use the space from the lower panel");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean isVisible = panelBottom.isVisible();
panelBottom.setVisible(!isVisible);
panel.revalidate();
panel.repaint();
panel.getParent().revalidate();
panel.getParent().repaint();
}
});
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.anchor = GridBagConstraints.NORTHWEST;
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 0;
panelTop.add(btnNewButton, gbc_btnNewButton);
scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
panelTop.add(scrollPane, gbc_scrollPane);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
},
new String[] {
"New column", "New column"
}
));
scrollPane.setViewportView(table);
panelBottom = new JPanel();
panelBottom.setLayout(null);
GridBagConstraints gbc_panelBottom = new GridBagConstraints();
gbc_panelBottom.fill = GridBagConstraints.BOTH;
gbc_panelBottom.gridx = 0;
gbc_panelBottom.gridy = 1;
panel.add(panelBottom, gbc_panelBottom);
scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 0, 973, 233);
panelBottom.add(scrollPane_1);
textArea = new JTextArea();
scrollPane_1.setViewportView(textArea);
}
}
您忘记将 GridBagLayout 的权重设置为合理的值,按钮为 0,其他按钮为 1.0。另外,避免 setSize 等。一个有效的例子可能是这样的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Test2 extends JPanel {
public static final int PREF_W = 1000;
public static final int PREF_H = 650;
public Test2() {
JButton hidePanelBtn = new JButton("Hide/Show Panel");
String[] columns = {"Column A", "Column B"};
DefaultTableModel tableModel = new DefaultTableModel(columns, 60);
JTable table = new JTable(tableModel);
JScrollPane scrollPane11 = new JScrollPane(table);
JTextArea textArea = new JTextArea("Hello World", 12, 50);
final JScrollPane scrollPane12 = new JScrollPane(textArea);
hidePanelBtn.addActionListener(e -> {
scrollPane12.setVisible(!scrollPane12.isVisible());
Test2.this.revalidate();
Test2.this.repaint();
});
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
mainPanel.add(hidePanelBtn, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridy++;
mainPanel.add(scrollPane11, gbc);
gbc.gridy++;
mainPanel.add(scrollPane12, gbc);
setPreferredSize(new Dimension(PREF_W, PREF_H));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("New Tab", mainPanel);
setLayout(new BorderLayout());
add(tabbedPane);
}
public static void main(String[] args) {
EventQueue.invokeLater(Test2::createAndDisplay);
Test.main(args);
}
private static void createAndDisplay() {
Test2 test2 = new Test2();
JFrame frame = new JFrame("Test");
frame.add(test2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}