我在JTable
内有一个JScrollView
,但在滚动条上似乎没有更好地对齐,我猜这是因为alignment.Moreover
的原因,这是真实代码的一个片段:
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 771, 453);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
panel.setBounds(10, 32, 747, 370);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel VPanel = new JPanel();
VPanel.setBounds(297, 43, 440, 224);
panel.add(VPanel);
JTable TableV = new JTable();
TableV.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableV.getTableHeader().setReorderingAllowed(false);
DefaultTableModel Model = new DefaultTableModel(0, 0);
String header[] = new String[] { "Country", "ID", "WAN IP", "User", "OS", "Java version" };
VPanel.add(new JScrollPane(TableV, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
// just adding some data to fill the table
Model.setColumnIdentifiers(header);
//set model into the table object
TableV.setModel(Model);
//just adding some diff data to check if it scrolls down
for (int count = 1; count <= 50; count++)
{
Model.addRow(new Object[] { "data1", "data2", "data3", "data4", "data5", "data6" });
}
for (int count = 1; count <= 70; count++)
{
Model.addRow(new Object[] { "data100", "data200", "data300", "data400", "data500", "data600" });
}
frame.setVisible(true);
最佳答案
了解VPanel(应将其重命名为vPanel以符合Java命名规则)使用FlowLayout,因此滚动窗格可能不太适合其中。如果希望滚动窗格填充它,请为其提供BorderLayout并添加JScrollPane BorderLayout.CENTER。
例如。,
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class TableFoo extends JPanel {
private static final String[] HEADER = new String[] { "Country", "ID", "WAN IP", "User", "OS", "Java version" };
private static final int PREF_W = 500;
private static final int PREF_H = 400;
private DefaultTableModel model = new DefaultTableModel(HEADER, 0);
private JTable table = new JTable(model);
public TableFoo() {
for (int count = 0; count < 50; count++) {
model.addRow(new Object[] { "data1", "data2", "data3", "data4", "data5", "data6" });
}
for (int count = 0; count < 70; count++) {
model.addRow(new Object[] { "data100", "data200", "data300", "data400", "data500", "data600" });
}
JScrollPane scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("TableFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TableFoo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}