我在这里有一个代码,我得到了这个错误.如何修复下面代码的错误?
Container container = getContentPane();
这是运行代码.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ModelJTable{
private DefaultTableModel model;
private JTable table;
public ModelJTable() {
super();
model = new DefaultTableModel();
model.addColumn("Product Name");
model.addColumn("Price");
model.addColumn("Quantity");
model.addColumn("Subtotal");
String[] socrates = { "Socrates", "", "469-399 B.C." };
model.addRow(socrates);
table = new JTable(model);
JButton addButton = new JButton("Add Philosopher");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String[] philosopher = { "", "", "" };
model.addRow(philosopher);
}
});
JButton removeButton = new JButton("Remove Selected Philosopher");
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
model.removeRow(table.getSelectedRow());
}
});
JPanel inputPanel = new JPanel();
inputPanel.add(addButton);
inputPanel.add(removeButton);
Container container = getContentPane();
container.add(new JScrollPane(table), BorderLayout.CENTER);
container.add(inputPanel, BorderLayout.NORTH);
Global.uniFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Global.uniFrame.setSize(800, 300);
Global.uniFrame.setVisible(true);
}
public static class Global{
public static JFrame uniFrame = new JFrame();
}
private static void createGUI(){
new ModelJTable();
}
public static void main(String args[]) {
createGUI();
}
}
我将代码更改为此,它不显示内容.
Container container = new Container();
container.add(new JScrollPane(table), BorderLayout.CENTER);
container.add(inputPanel, BorderLayout.NORTH);
Global.uniFrame.getContentPane();
Global.uniFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Global.uniFrame.setSize(800, 300);
Global.uniFrame.setVisible(true);