【问题标题】:Add a lateral Jpanel添加横向Jpanel
【发布时间】:2014-02-27 04:35:31
【问题描述】:

您好,我的框架中有一个网格布局,另外两个 Jpanel 内部也有网格布局。现在我想在右侧添加一个从上到下垂直占用空间的 Jpanel。 我该怎么办?

container = frame.getContentPane();
matricepc = new JPanel();
matrice = new JPanel();
matrice.setLayout(new GridLayout(griglia.getRow(), griglia.getColumn()));
matricepc.setLayout(new GridLayout(griglia.getRow(), griglia.getColumn()));
container.setLayout(new GridLayout(0,2));
container.add(matricepc);
container.add(matrice);

【问题讨论】:

    标签: java swing user-interface awt layout-manager


    【解决方案1】:

    matricepcmatrice 放在带有GridLayoutJPanel 中,而不是JFrame。将JPanel 放在JFrameBorderLayout.CENTER 上,然后将您的另一个权利JPanel 添加到BorderLayout.EAST

    container = frame.getContentPane();
    matricepc = new JPanel();
    matrice = new JPanel();
    matrice.setLayout(new GridLayout(griglia.getRow(), griglia.getColumn()));
    matricepc.setLayout(new GridLayout(griglia.getRow(), griglia.getColumn()));
    
    //container.setLayout(new GridLayout(0,2));             <--+
    //container.add(matricepc);                                |-- Remove this code
    //container.add(matrice);                               <--+
    
    JPanel centerPanel = new JPanel(new GridLayout(0,2));   <--+
    centerPanel.add(matricepc);                                |
    centerPanel.add(matrice);                                  |
    container.add(centerPanel, BorderLayout.CENTER);           |-- Add this code
    JPanel rightPanel = new JPanel();                          |
    container.add(rightPanel, BorderLayout.EAST);           <--+
    

    【讨论】:

    • 是的,我已经这样做了,但是我在 view.PartitaView.(PartitaView.java:49) 处有一个 NullExecptionPointer,即行:JPanel centerPanel = new JPanel(new GridLayout(0, 2));
    • 如需尽快获得更好的帮助,请发帖 Minimal Complete Tested and Readable Example (MCTRE)。
    • 你的代码最后三行有没有拿出来?
    • 那条线不应该抛出NullPinterException任何地方你在整个银河系都使用它。
    • 顺便说一句 - 代码行不可能产生NullPointerException,您显然是在读取错误的堆栈跟踪,或者将其等同于错误的代码行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2011-07-10
    相关资源
    最近更新 更多