单击jButton按钮无法显示jTable内容

时间:2023-01-28 22:30:48
代码如下
package untitled2;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.table.JTableHeader;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame1 extends JFrame {
    JPanel contentPane;
    Object[][] o1 = new Object[50][6];
    String[] s1 = {"机器号", "状态", "上机时间", "卡号", "所用金额", "备注"};
    JTable jTable1 = new JTable(o1, s1);
    JTableHeader jTableHeader1 = jTable1.getTableHeader();
    JButton jButton1 = new JButton();
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(400, 300));
        setTitle("Frame Title");
        jTable1.setBounds(new Rectangle(12, 59, 369, 234));
        jTableHeader1.setBounds(new Rectangle(14, 43, 450, 16));
        jButton1.setBounds(new Rectangle(15, 7, 54, 18));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        contentPane.add(jTable1);
        contentPane.add(jTableHeader1);
        contentPane.add(jButton1);
    }

    public void jButton1_actionPerformed(ActionEvent e) {
        o1[1][0] = "A001";
        o1[1][1] = "上机";
        o1[1][2] = "12:00:00";
        o1[1][3] = "3521";
        o1[1][4] = "45";
        o1[1][5] = "无";
        jTable1=new JTable(o1,s1);
     
      }
}


class Frame1_jButton1_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
谢谢大家的关注

5 个解决方案

#1


帮顶

不熟悉GUI

#2


谢谢...

#3


    public void jButton1_actionPerformed(ActionEvent e)
    {
        o1[1][0] = "A001";
        o1[1][1] = "上机";
        o1[1][2] = "12:00:00";
        o1[1][3] = "3521";
        o1[1][4] = "45";
        o1[1][5] = "无";
        jTable1 = new JTable(o1, s1);
        this.repaint();

    }

#4


ls说的对 只要刷新一下就可以显示出来了

#5


怎么不可以呢?我试了下,是可以的。

#1


帮顶

不熟悉GUI

#2


谢谢...

#3


    public void jButton1_actionPerformed(ActionEvent e)
    {
        o1[1][0] = "A001";
        o1[1][1] = "上机";
        o1[1][2] = "12:00:00";
        o1[1][3] = "3521";
        o1[1][4] = "45";
        o1[1][5] = "无";
        jTable1 = new JTable(o1, s1);
        this.repaint();

    }

#4


ls说的对 只要刷新一下就可以显示出来了

#5


怎么不可以呢?我试了下,是可以的。