gpt4 book ai didi

java - JFrame : cannot display two panels in jframe

转载 作者:行者123 更新时间:2023-12-02 02:41:48 25 4
gpt4 key购买 nike

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

感谢任何帮助。

最佳答案

删除:

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

这只是一种更加一致和首选的机制

关于java - JFrame : cannot display two panels in jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45340651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com