如何使用Java布局管理器以我想要的方式显示面板?

时间:2014-03-06 23:41:41

标签: java swing layout jpanel layout-manager

我目前正在尝试使用四个面板制作程序。使用GridLayout将三个面板组合在JFrame的左侧,最后一个面板占据屏幕的右侧。理想情况下,左面板将占据JFrame左侧的2/3,最后三个面板将占用第四个面板。我试图在很多方面做到这一点,无法弄明白。当前显示屏将屏幕分成两半,并在彼此的顶部显示黄色,红色,蓝色,但只有540像素长,而绿色则覆盖左侧面板的其余部分。

这个绿色矩形代表最后一个面板,它与黄色红色和蓝色矩形重叠,长度应为720像素。通过重新调整JFrame的大小,我已经完成了正确显示我想要的方式,直到我使它工作。通过重新调整大小直到矩形排列,我设法使它看起来应该如此。我移动了JFrame的右侧,直到两个相应的面板排成一列没有空白或重叠,不幸的是,为了做到这一点,侧面有一大堆多余的空白区域。

这是我用来制作这个显示的代码。我是一个完整的摇摆新手,可能完全做错了。我没有开始使用GridLayout,我只想让它正常工作。

    import java.awt.*;
import javax.swing.*;
class testJPanel1 extends JPanel        //topLeft
{
    int x,y;
    //JPanel Tjp1;
    public testJPanel1()
    {
        x=720;
        y=250;
        setSize(x,y);
        setVisible(true);
    }
    @Override
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        //g.fillOval(x,y,50,50);
        g.setColor(Color.yellow);
        g.fillRect(0, 0, x, y);
        g.setColor(Color.black);
        g.drawString("1",x,y);
        System.out.println("Hey 1 works");
    }
}
class testJPanel2 extends JPanel        //mid left
{
    int x,y;
    public testJPanel2()
    {
        x=720;
        y=260;
        setVisible(true);
        setSize(x,y);
    }
    @Override
    public void paintComponent(Graphics g)
    {super.paintComponent(g);
        //g.fillOval(x,y,50,50);
        g.setColor(Color.red);
        g.fillRect(0, 0, x, y);
        g.setColor(Color.black);
        g.drawString("2",x,y);
        System.out.println("Hey 2 works");
    }
}
class testJPanel3 extends JPanel        //bot left
{
    int x,y;
    int boundsx;
    int boundsy;
    public testJPanel3()
    {
        x=720;
        y=250;
        boundsx=200;
        boundsy=200;
        setVisible(true);
        setSize(x,y);
    }
    @Override
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        //g.fillOval(x,y,50,50);
        g.setColor(Color.blue);
        g.fillRect(0, 0, x, y);
        g.setColor(Color.black);
        g.drawString("3",x,y);
        System.out.println("Hey 3 works");
       // g.drawRect(0,0,boundsx,boundsy);
    }
}   
class testJPanel4 extends JPanel        //BIG one on the right
{
    int x,y;
    int boundsx;
    int boundsy;
    public testJPanel4()
    {
        x=360;
        y=760;
        boundsx=200;
        boundsy=200;
        setVisible(true);
        setSize(x,y);
    }
    @Override
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        g.setColor(Color.green);
        //g.fillOval(x,y,50,50);

        g.fillRect(0,0,x,y);
        g.setColor(Color.black);
        g.drawString("4",x,y);
        System.out.println("Hey 4 works");
        //g.drawRect(0,0,boundsx,boundsy);
    }
}
class Left_Panel extends JPanel     //Combines 1 and 2 and 3 
{
    testJPanel1 tjp1;
    testJPanel2 tjp2;
    testJPanel3 tjp3;
    public Left_Panel()
    {
        setLayout(new GridLayout(3,1));
        tjp1= new testJPanel1();
        tjp2= new testJPanel2();
        tjp3= new testJPanel3();
        add(tjp1);
        add(tjp2);
        add(tjp3);

    }
}

class combo_Panel extends JPanel        //combines left and right panels together
{
    Left_Panel lp;
    testJPanel4 tjp4;
    //GOAL IS TO MAKE THIS PANEL lp is 720,760 pixels and tjp4 360,760...still doesnt work
    public combo_Panel()
    {
        setLayout(new GridLayout(1,2));     //HOW TO make this work the way i want it or use something else i want
        lp= new Left_Panel();
        lp.setSize(720,760);
        tjp4= new testJPanel4();
        tjp4.setSize(360,760);

        add(lp);
        add(tjp4);
    }
}
class Paint_Window extends JFrame
{
    combo_Panel combo;
    Paint_Window(String title)
    {
        super(title);
                //setLayout( new BoxLayout(combo,2));
                combo = new combo_Panel();
        setBounds(new Rectangle(1080,760));
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
                //setLayout(new FlowLayout());
                add(combo);
                //add(tjp5);
               //tjp1.repaint();
    }
}
public class testJPanel
{
    public static void main(String args[])
    {
            Paint_Window window = new Paint_Window("Make your choice");
    }

1 个答案:

答案 0 :(得分:2)

您有几个选项,您可以尝试使用GridLayout在一个容器中布置所有三个面板,但这可能是不可能的,另一个选择可能是使用GridBagLayout和调整weightx属性以满足您的要求,例如......

Layout

请注意蓝色和绿色面板周围的红色“边框”,这显示了这两个组件的父容器

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.PopupMenu;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPanel leftSide;
        private JPanel rightSide;

        public TestPane() {
            setLayout(new GridBagLayout());

            leftSide = new JPanel(new GridLayout(1, 2));
            leftSide.setBackground(Color.RED);
            leftSide.setBorder(new EmptyBorder(1, 1, 1, 1));

            leftSide.add(createPanel(Color.BLUE));
            leftSide.add(createPanel(Color.GREEN));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.67;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(leftSide, gbc);

            gbc.gridx = 1;
            gbc.weightx = 0.33;
            rightSide = createPanel(Color.MAGENTA);
            add(rightSide, gbc);

        }

        protected JPanel createPanel(Color color) {
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 100);
                }
            };
            panel.setBackground(color);
            return panel;
        }
    }

}