0
我有這段代碼(是的,我是新手)但JTable沒有顯示。 在創建面板之前,我可以看到它(一個品紅色的盒子,因爲我還沒有製作DataModel)。爲什麼JTable沒有顯示?
但添加面板後,消失。誰能告訴我爲什麼?提前致謝。
public Ost() {
super();
JTable table;
OstBridge bridge;
JButton btn;
JPanel p1=new JPanel(new FlowLayout());
JPanel p2=new JPanel(new FlowLayout());
Container cp=getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
p1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
p2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(320, 240);
this.setTitle("OST - Downloader");
//this.setLayout(new FlowLayout());
cp.add(p1);
cp.add(p2);
table=new JTable();
table.setBackground(new Color(0xFF00FF));
table.setSize(100, 100);
table.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
p2.add(table);
bridge=new OstBridge();
btn=new JButton("Prueba");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Action event");
System.out.println(e);
System.out.println(bridge);
new Ost();
}
});
p1.add(btn);
p1.setVisible(true);
this.pack();
this.setVisible(true);
}