我正在为我的大学做一个项目,我需要两个 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);
}
}
我将此 JTable 添加到我的主 JFrame 和另一个 JComponent 中:
public class MainGUI extends JFrame{
private JTextField field1;
private JTextField field2;
private JButton button1;
private JButton button2;
private Memory mem;
public MainGUI()
{
this.GraphicComponents();
}
public final void GraphicComponents()
{
this.setTitle("Machine");
this.setSize(800, 620);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
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)
{
MainGUI.this.mem.setAddrValue(MainGUI.this.field1.getText().toString(),MainGUI.this.field2.getText().toString() );
}
});
this.add(button1);
this.button2 = new JButton("Run");
this.button2.setBounds(500,550,100,30);
this.button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
ExecutionGUI exe = new ExecutionGUI(mem);
exe.setVisible(true);
}
});
this.add(button2);
this.mem = new Memory();
this.mem.setBounds(450, 30, 315, 490);
this.add(mem);
} }
在这段代码中,setValueAt() 方法工作完美,没有任何问题。但是当我在第二个 JFrame 中使用相同的 JTabel 代码时,当我按下运行 JButton 但在 Memory 类中创建新的 JTable 而不是使用 setValueAt() 向 JTable 插入值时,JTabel 不会更新。
第二个JFrame的代码:
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);
}
所以我只有第二个 JFrame 有问题,我不能用新数据刷新 JTable。我已经尝试了不止一种方法来获得正确的结果,但什么都没有。
谢谢你的帮助。