如何在不影响 5px 的组件之间的间隙的情况下将组件与表格对齐?我有一个想法将 FlowLayout 的 hgap 的值设置为零,但如果我这样做,它也会改变组件之间的间隙......如果我不清楚,这是截图和代码:
我想要的:
它是怎样的:
代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Test3 {
private JButton addNewColumnButton;
private JButton calculateColumnButton;
private JButton resultButton;
private JLabel textLabel;
private JTextField columnField;
private JTextField resultField;
private JComboBox columnListCB;
private JTable table;
private String[] tableCols = {
"First Column", "Second Column", "Third Column", "", "", "", ""
};
private Object[][] tableRows = {
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null},
{true, null, null, null, null, null, null, null}
};
public Test3() {
JFrame f = new JFrame("Test2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(getPanelComponents());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JPanel getPanelComponents() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel center = new JPanel(new GridLayout());
table = new JTable(tableRows, tableCols);
table.setPreferredScrollableViewportSize(new Dimension(450, 128));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
center.add(new JScrollPane(table));
JPanel eastPanel = new JPanel();
Box eastPanelBox = Box.createVerticalBox();
addNewColumnButton = new JButton("Add New Column");
addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(addNewColumnButton);
eastPanelBox.add(Box.createVerticalStrut(35));
columnField = new JTextField();
columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(columnField);
eastPanelBox.add(Box.createVerticalStrut(5));
columnListCB = new JComboBox(tableCols);
columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(columnListCB);
eastPanelBox.add(Box.createVerticalStrut(5));
calculateColumnButton = new JButton("Calculate Column");
calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(calculateColumnButton);
eastPanel.add(eastPanelBox);
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
resultButton = new JButton("Calculate");
southPanel.add(resultButton);
textLabel = new JLabel("Result:");
southPanel.add(textLabel);
resultField = new JTextField(10);
southPanel.add(resultField);
panel.add(center, BorderLayout.WEST);
panel.add(southPanel, BorderLayout.SOUTH);
panel.add(eastPanel, BorderLayout.EAST);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test3();
}
});
}
}