JFrame:无法在jframe中显示两个面板

时间:2017-07-27 03:06:04

标签: java swing jframe jpanel

我在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);

感谢任何帮助。

1 个答案:

答案 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);

它只是一个更加一致和首选的机制