有一个表格,我想在单击按钮时显示在面板中。我能够显示表格,但我无法显示标题。也尝试使用JscrollPane,但这里根本没有显示任何内容。
代码的主要部分:
公共类 GUI 扩展了 JFrame { JButton按钮; 静态 JFrame 框架; JTable 表 A; JPanel TablePanel;
public static void main(String[] args) {
frame = new GUI();
}
public GUI() {
frame = new JFrame();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
JPanel MainPanel = new JPanel();
MainPanel.setBackground(new Color(0, 0, 0));
MainPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
null, null, null));
MainPanel.setBounds(0, 0, screen.width, screen.height - 60);
MainPanel.setPreferredSize(screen);
frame.getContentPane().add(MainPanel);
MainPanel.setLayout(null);
button = new JButton("Show Assignments");
button.setFont(new Font("Tahoma", Font.PLAIN, 10));
button.setBounds(MainPanel.getWidth() / 2 - 65, 40, 130, 20);
MainPanel.add(button);
TablePanel = new JPanel();
TablePanel.setBackground(new Color(245, 222, 179));
TablePanel
.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
TablePanel.setBounds(160, 85, MainPanel.getWidth() - 170,
MainPanel.getHeight() - 95);
MainPanel.add(TablePanel);
TablePanel.setLayout(null);
button.addActionListener(new MyActionListener());
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
单击按钮时会发生什么。
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String[] columnNames = { "AssignmentID", "Assignmenttype",
" AssignmentDesricption", "ClientID", "Status" };
Object[][] data = {
{
"028079",
"Software Support",
"Danzafe is in need software support on their mail program.",
"25890", "Waiting" },
{
"028080",
"Software Installing",
"Danzafe is in need of installation of new software on thier server.",
"25890", "In Progress" },
{ "028081", "Hardware Setup",
"Expert Beslag needs to get a new local server setup.",
"25891", "In Progress" },
{
"028082",
"Hardware Support",
"Expert Beslag have a computer that is making troubles with the wireless internet card",
"25891", "Waiting" },
{
"028083",
"Balacing Question",
"Blizzard needs help balacing out the class of World of Warcraft",
"00001", "Never Ends" } };
tableA = new JTable(data, columnNames);
tableA.setBounds(5, 35, TablePanel.getWidth() - 15,
TablePanel.getHeight() - 15);
TablePanel.add(tableA.getTableHeader());
tableA.setFillsViewportHeight(true);
tableA.setVisible(true);
TablePanel.add(tableA);
TablePanel.repaint();
}
}
}
-
必须将
的JTable
放入JScrollPane
,那么JTableHeader
应该是可见 -
或将
JPanel
的Layout Manager
从默认FlowLayout
更改为BorderLayout
-
从
JTable
获得JTableHeader
并放入NORTH
区域 -
正确的方法是仅使用
JScrollPane
-
不要使用
setBounds()
,使用built_inBorderLayout
进行JFrame
,并将JScrollPane
(只是可能没有理由使用JPanel
,只有在您将计划使用CardLayout
的情况下)放在CENTER area
-
其余的
JComponent(s)
放到单独的JPanel
,这个JPanel
放到NOTHR
或SOUTH
区域来自JFrame
您必须将表格放入滚动窗格中,然后将滚动窗格添加到标签中。它应该以这种方式工作,您可能不必做更多的事情。