1

当我点击一个按钮时,有一个我想在面板中显示的表格。我可以让表格显示,但我无法显示标题。也尝试过使用 JscrollPane,但这里没有任何显示。

代码的主要部分:

公共类 GUI 扩展 JFrame { JButton 按钮;静态 JFrame 框架;JTable表A;JPanel 表面板;

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();
        }
    }
}
4

2 回答 2

3
  • 必须放入JTableJScrollPane然后JTableHeader应该可见

  • 或将Layout ManagerforJPanel从默认更改FlowLayoutBorderLayout

  • 从该地区JTableHeader出发JTable和前往该NORTH地区

  • 正确的方法是JScrollPane只使用

  • 不要使用setBounds(),使用 built_in BorderLayoutforJFrame并放入JScrollPane(可能没有理由使用JPanel,仅在您打算使用的情况下CardLayout)到CENTER area

  • 其余的JComponent(s)放在单独的JPanel,这个JPanel放在NOTHRSOUTH区域的来自JFrame

于 2012-11-08T08:32:14.573 回答
0

您必须将表格放入滚动窗格,然后将滚动窗格添加到标签中。它应该以这种方式工作,您可能不需要做更多的事情。

于 2012-11-08T11:12:31.723 回答