我的错误是没有显示两个表的表头。现在我用new JTable(data, columnNames)
设置标题。
下面是一个例子,显示了我的问题:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import net.miginfocom.swing.MigLayout;
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JPanel {
public JScrollPane createLayout() {
JPanel panel = new JPanel(new MigLayout(""));
JScrollPane sp = new JScrollPane(panel);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(table1(), "growx, wrap");
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(table2());
// panel.add(Box.createRigidArea(new Dimension(0,10)));
return sp;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"},
{"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"},
{"Joe", "Brown"}};
final JTable table = new JTable(data, columnNames);
tableProperties(table);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"},
{"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"},
{"Joe", "Brown"}};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
tableProperties(table);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public void tableProperties(JTable table) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.repaint();
table.revalidate();
}
}
}
有什么建议我做错了吗?
-
JTableHeader
要求JScrollPane
作为集装箱 -
你必须从
JTable
中获得JTableHeader
,并把它单独放置,例如panel.add(table1.getTableHeader(), "constant, constant, constant");
,在那里使用BorderLayout作为更好,更简单的LayoutManager
,JPanel
比BoxLayout
,例如panel.add(table1.getTableHeader(), BorderLayout.NORTH);
,然后把JTable
放在中心区域 -
更好的应该是-不要在
JScrollPane
中使用JPanel
,将JTable
直接放到JScrollPane
中,然后JTableHeader
是可见的,否则你必须为JPanel
实现Scrollable
用于自然滚动
-
使用
MigLayout
为整个容器(指定为),使用此自定义LayoutManager
不需要混合不同的LayoutManagers
-
使用
DefaultTableModel
来存储JTable
s视图的值 -
参见
Oracle tutorial Initial Thread
-
setPreferredScrollableViewportSize
被JScrollPane
使用 -
您的
PreferredSize
设置创建**** Swing GUI
- void
tableProperties
包含两个无用的代码行table.repaint();
和table.revalidate();
,并且顺序错误