有一張表我想在面板中顯示,當我點擊一個按鈕。我能夠讓桌子顯示,但我無法顯示標題。也嘗試過使用JscrollPane,但是這裏沒有任何顯示。Java JTable頭沒有顯示
的代碼主體:
公共類GUI延伸的JFrame { JButton的按鈕; 靜態JFrame框架; JTable tableA; 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();
}
}
}
請學習java命名約定並堅持使用它們。 – kleopatra