Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用setValueAt&x27刷新/更新JTable;我不能正常工作_Java_Swing_Jtable - Fatal编程技术网

Java 使用setValueAt&x27刷新/更新JTable;我不能正常工作

Java 使用setValueAt&x27刷新/更新JTable;我不能正常工作,java,swing,jtable,Java,Swing,Jtable,我正在为我的学院做一个项目,我需要两个JFrame。第一个是主菜单,第二个在按下JButton(Run)时可见。 我需要在两个JFrame中都绘制一个内存,所以我使用JTable来显示内存 内存类为: public class Memory extends JPanel{ private JPanel panel; private JTable table; private String[] array; private JScrollPane scr

我正在为我的学院做一个项目,我需要两个JFrame。第一个是主菜单,第二个在按下JButton(Run)时可见。 我需要在两个JFrame中都绘制一个内存,所以我使用JTable来显示内存

内存类为:

    public class Memory extends JPanel{

    private JPanel panel;
    private JTable table;
    private String[] array; 
    private JScrollPane scrollpane;

    public Memory()
    {
       array = new String[256]
       this.addMemoryGUI();
    }

    public final JPanel addMemoryGUI()
    {
        this.panel = new JPanel();
        this.panel.setPreferredSize(new Dimension(315,490));
        this.panel.setLayout(null);
        this.add(panel);

        String info[][] = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = j;

            info[j][1] = null;
        }
        String[] title = new String[]{"Address","Value"};

        DefaultTableModel model = new DefaultTableModel(info,title);
        table = new JTable(model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   
        }
        };
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
        table.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(this.table);
        this.scrollpane.setBounds(0, 0,315, 490);
        this.panel.add(this.scrollpane);
        return panel;
    }

    public void setAddrValue(int addr,String value)
    {
        Memory.this.array[addr] = value;
        this.table.setValueAt(value,addr , 1);
    }

    public String getAddrValue(int addr)
    {
        return Memory.this.array[addr];
    }

    public String[] getMemory()
    {
        return Memory.this.array;
    }



    public void deleteValue(int i)
    {
        array[i]=null;
        this.table.setValueAt(null, i, 1);
    }
}
} }

在这段代码中,setValueAt()方法工作得很好,没有任何问题。 但当我在第二个JFrame中使用相同的JTabel代码时,当我按下run JButton,但创建新的JTable而不是内存类中的JTable,并使用setValueAt()向JTable插入值时,JTabel不会更新

第二帧的代码:

public class ExecutionGUI extends JFrame {


private JTextField field1;
        private JTextField field2;
    private JButton button1;
private JScrollPane scrollpane;
        private JButton button2;
    private Memory mem;
private JTable table;
private static DefaultTableModel model;
private String info[][];
    private String[] title;
private Memory mem;

 public ExecutionGUI(Memory mem)
    {
        this.mem = mem;

        this.GraphicComponents();
    }

 public final void GraphicComponents()
    {
       this.setTitle("Run");
        this.setBounds(200, 100, 800, 600);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setResizable(false);


        this.info = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = j;
            if(mem.getAddrValue(j)==null)
            {

            }
            else
            {
                info[j][1] = mem.getAddrValue(j);
            }
        }
        title = new String[]{"Address","Value"};

        model = new DefaultTableModel(info,title);
        table = new JTable(model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   
        }
        };
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        table1.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
        table1.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(table);
        this.scrollpane.setBounds(610, 30,170, 470);
        this.add(this.scrollpane);

        this.field1=new JTextField();
        this.field1.setBounds(10,50,70,20);
        this.add(field1);

        this.field2=new JTextField();
        this.field2.setBounds(90,50,70,20);
        this.add(field2);

        this.button1=new JButton("Write Value");
        this.button1.setBounds(170,50,130,20);
        this.button1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e)
         { 
                      table.setValueAt(MainGUI.this.field2.getText().toString(),MainGUI.this.field1.getText().toString(),1 );
         }

        });
        this.add(button1);
    }
公共类ExecutionGUI扩展了JFrame{
私有JTextField字段1;
私有JTextField字段2;
私人按钮1;
私有JScrollPane滚动窗格;
私人按钮2;
专用存储器;
专用JTable表;
私有静态模型;
私有字符串信息[];
私有字符串[]标题;
专用存储器;
公共执行GUI(内存内存内存)
{
this.mem=mem;
这是graphicscomponents();
}
公共最终组件()
{
本条。设定标题(“运行”);
这个.挫折(200100800600);
此.setLayout(null);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此参数为.setresizeable(false);
this.info=新字符串[256][2];

对于(int j=0;jDon)来说,不要使用空布局和setBounds()。Swing设计用于布局管理器以实现适当的功能。好的,我同意,但我认为我的问题不在这里。因此我稍后会在代码中使用它。
table.setValueAt(MainGUI.this.field2.getText().toString(),MainGUI.this.field1.getText().toString(),1);
jtable
setValueAt(Object value,int row,int column)
+1@camickr。另请参见类似的问题/答案请学习java命名约定并遵守它们。
public class ExecutionGUI extends JFrame {


private JTextField field1;
        private JTextField field2;
    private JButton button1;
private JScrollPane scrollpane;
        private JButton button2;
    private Memory mem;
private JTable table;
private static DefaultTableModel model;
private String info[][];
    private String[] title;
private Memory mem;

 public ExecutionGUI(Memory mem)
    {
        this.mem = mem;

        this.GraphicComponents();
    }

 public final void GraphicComponents()
    {
       this.setTitle("Run");
        this.setBounds(200, 100, 800, 600);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setResizable(false);


        this.info = new String[256][2];
        for(int j=0;j<256;j++)
        {
            info[j][0] = j;
            if(mem.getAddrValue(j)==null)
            {

            }
            else
            {
                info[j][1] = mem.getAddrValue(j);
            }
        }
        title = new String[]{"Address","Value"};

        model = new DefaultTableModel(info,title);
        table = new JTable(model){
        @Override
        public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   
        }
        };
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        table1.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
        table1.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.GRAY);
        this.scrollpane = new JScrollPane(table);
        this.scrollpane.setBounds(610, 30,170, 470);
        this.add(this.scrollpane);

        this.field1=new JTextField();
        this.field1.setBounds(10,50,70,20);
        this.add(field1);

        this.field2=new JTextField();
        this.field2.setBounds(90,50,70,20);
        this.add(field2);

        this.button1=new JButton("Write Value");
        this.button1.setBounds(170,50,130,20);
        this.button1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e)
         { 
                      table.setValueAt(MainGUI.this.field2.getText().toString(),MainGUI.this.field1.getText().toString(),1 );
         }

        });
        this.add(button1);
    }