在运行期间在面板中添加面板时,我无法设置位置

时间:2012-06-01 08:13:21

标签: java swing jpanel draw layout-manager

我在使用java及其swing组件时非常困难,我必须说当我使用C#进行编程时这会更容易,而且我对这段代码感到有些困惑。

我要做的是在another panel上添加一些面板。 这向用户显示他加入的项目需要完成的任务。

On the left you see the panel, and i want to add more panels to it.

我在Gui的右侧创建了一个面板,我希望在运行时添加更多面板。 我设法添加了一个面板,但它有一些奇怪的行为。

enter image description here

蓝色面板是我在运行时使用此代码添加的newly created面板。

  JPanel pnl = new JPanel();
  lpane.setBackground(Color.red);
  lpane.setLayout(new BorderLayout());
  pnl.setBounds(0, 0, 20, 100);
  pnl.setOpaque(true);
  pnl.setBackground(Color.BLUE);
  lpane.add(pnl);
  lpane.validate();

这只是一个测试,这就是为什么代码不包含其他面板的for-loop等的原因。 你可以看到我正在使用BorderLayout因为我在互联网上找到了这个,并且因为没有BorderLayout它不会绘制任何东西。 此外,当我尝试将BorderLayout设置为.NORTH或.STARTPAGE时,它会在面板中开始绘制,但我仍然无法设置面板的任何位置?

有人知道为什么我不能设置任何职位或宽度和高度?

3 个答案:

答案 0 :(得分:3)

  • BorderLayout已实施了5. JComponents区域,

  • 使用JComponent

  • 可以看到最后添加BorderLayout到具体区域
  • 使用(在JPanel中预先实现)FlowLayout,接受来自JComponents的PreferredSize

  • GridLayout适用于屏幕尺寸相同的儿童

  • 一些规则How Swing LayoutManager works

答案 1 :(得分:3)

好的,所以这里有一些代码可以帮助你,但我真的建议你花些时间在http://docs.oracle.com/javase/tutorial/uiswing/

上完成Swing教程。

布局管理器的存在使布局变得灵活和动态,但需要一些时间和练习来真正理解它们。下面的代码是执行所需操作的一种方法,但它可能不是所有用例的正确代码。

注意:这不是生产级代码,而是更多地说明一点。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Simple app to demonstrate how to use basic layout managers.
 * @author ewald
 */
public class LayoutPanels {

    private JFrame frame = new JFrame("The Top Frame");

    public LayoutPanels() {
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }


    /**
     * Sample code - this is not best practice, but it will help
     * you to make some progress.
     */
    public void startUp() {
        JPanel topPanel = new JPanel(new BorderLayout());

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.BLUE);
        eastPanel.setLayout(new FlowLayout());
        eastPanel.setPreferredSize(new Dimension(150,200));

        JPanel onePanel = new JPanel(new FlowLayout());
        JPanel twoPanel = new JPanel(new FlowLayout());
        JPanel threePanel = new JPanel(new FlowLayout());

        onePanel.setPreferredSize(new Dimension(100,50));
        twoPanel.setPreferredSize(new Dimension(100,50));
        threePanel.setPreferredSize(new Dimension(100,50));

        onePanel.setBackground(Color.RED);
        twoPanel.setBackground(Color.GREEN);
        threePanel.setBackground(Color.YELLOW);

        eastPanel.add(onePanel);
        eastPanel.add(twoPanel);
        eastPanel.add(threePanel);

        topPanel.add(eastPanel, BorderLayout.EAST);

        frame.getContentPane().add(topPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        LayoutPanels app = new LayoutPanels();
        app.startUp();
    }

}

答案 2 :(得分:2)

对此有几种方法。一种方法是在父面板上使用BoxLayout.Y_AXIS。这将导致您的子面板在添加时彼此相加。与BorderLayout相关,它有5个区域,并且不适合执行此任务。

JPanel pnl = new JPanel();
pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS)); // This will cause your new panels to be added below eachother
pnl.setBackground(Color.BLUE);
//and set sizes etc

JPanel newPanel = new JPanel();
newPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // set layout on your child panel. FlowLayout is default but you might want to read up on the different layoutmanagers to pick the right one.
newPanel.setSize(50, 50); // you can use pnl.getWidth to make your panels to have the same width as it's parent pane, but you probably want the height to be locked
//and add the contents to this panel
pnl.add(newPanel);
相关问题