我在Jframe中显示两个面板有问题。请帮我修改下面的代码
public class quotingtable extends javax.swing.JFrame {
DefaultTableModel model;
JTable table;
JButton SetButton = new JButton("Set Symbol");
JButton VNStock = new JButton("VNStockChart");
JButton Global = new JButton("GlobalChart");
JPanel quotingpanel = new JPanel(new BorderLayout());
JPanel functionpanel = new JPanel(new BorderLayout());
public void run(){
model = new DefaultTableModel(col,row);
quotingpanel.add(table);
functionpanel.add(BorderLayout.CENTER,SetButton);
functionpanel.add(BorderLayout.WEST,VNStock);
functionpanel.add(BorderLayout.EAST,Global);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
quotingpanel.add(pane);
getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);
setSize(800,800);
setLayout( new FlowLayout());
setLayout ( new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
感谢任何帮助。
答案 0 :(得分:2)
删除:
setLayout( new FlowLayout());
setLayout ( new BorderLayout());
以这种方式使用BorderLayout
将无法获取预先存在的组件,因此会忽略它们并且不会将它们布局
并考虑将setSize(800,800);
替换为pack();
您可能还想更改
getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);
到
getContentPane().add(functionpanel, BorderLayout.CENTER);
getContentPane().add(quotingpanel, BorderLayout.SOUTH);
它只是一个更加一致和首选的机制