当我点击一个按钮时,有一个我想在面板中显示的表格。我可以让表格显示,但我无法显示标题。也尝试过使用 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();
}
}
}