一个JFrame中有两个JPanel

时间:2017-01-16 21:59:38

标签: java swing jpanel

我想在一个JFrame中使用两个JPanel,它们之间有一条不可见的水平线。我玩了一点,得到了这个:

public class Application { 

    public static void main(String[] args)
    {   

         JFrame jframe = new JFrame();
         jframe.setSize(500,700);
         jframe.setVisible(true);
         jframe.setTitle("Title");
         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jframe.setResizable(false);


         JSplitPane splitPane = new JSplitPane();
         JPanel leftPanel = new JPanel();
         JPanel rightPanel = new JPanel();
         splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);  
         splitPane.setDividerLocation(250);                    
         splitPane.setLeftComponent(leftPanel);                  
         splitPane.setRightComponent(rightPanel); 
         jframe.add(splitPane);


    }
}

现在,第一个问题是如何关闭"可恢复性"面板之间的线?我怎么做它"看不见"?也许使用除分裂窗格之外的其他东西?

其次,我怎样才能只使用JPanel的一面? (我正在开发一个可以让你在左侧画一个圆圈的应用程序。)

这似乎是一个简单的问题,但我对Java相对较新。

2 个答案:

答案 0 :(得分:3)

如前所述{@ 3}} @MadProgrammer您可以使用commentBorderLayout,但是当您将“分割”线放在两个面板的中间时,您可以使用GridBagLayout无论窗口是否调整大小,都会使两个面板的大小相同。

我没有尝试使用GridBagLayout,但我做了一个示例,说明如何在不使用JSplitPane的情况下实现此窗格分离。

使用GridLayout所有您需要做的就是将元素添加到左侧窗格(在我的示例中,我使用JLabel来区分它们),而在BorderLayout中您需要指定您将要绘制圆圈以与左对齐(WEST常量)的面板,就像我一样。

但是,如果您使用BorderLayout方法并向右侧窗格添加文本或元素,它们将与右侧对齐,您可以通过“装箱”另一个窗格中的元素使用不同的{{3来修复它}}

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Application {

    private JFrame frame;
    private JPanel containerPane;
    private JPanel topPane;
    private JPanel bottomPane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Application().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("Example of 2 panels");
        containerPane = new JPanel();
        topPane = new JPanel();
        bottomPane = new JPanel();

        containerPane.setLayout(new GridLayout(2, 1));
        topPane.setLayout(new GridLayout(1, 2));
        bottomPane.setLayout(new BorderLayout());

        topPane.add(new JLabel("Left side"));
        topPane.add(new JLabel("Right side"));

        bottomPane.add(new JLabel("Left side"), BorderLayout.WEST);
        bottomPane.add(new JLabel("Right side"), BorderLayout.EAST);

        topPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using GridLayout"));
        bottomPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using BorderLayout"));

        containerPane.add(topPane);
        containerPane.add(bottomPane);

        frame.add(containerPane);

//      frame.pack();
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

我没有在此示例中调用pack(),因为在这种情况下,两个面板(或JLabel)的大小不足以显示差异:

使用pack()

GridLayout

致电setSize()

Layout

其他提示

  1. 不要忘记将程序放在enter image description here上,我是通过在main方法上写下这些行来完成的:

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Application().createAndShowGui();
        }
    });
    
  2. 不要将所有代码放在构造函数上,否则将很难维护

答案 1 :(得分:1)

看起来您可以使用GridLayout来执行此操作。这就是我的想法,

public class Application {
    public static void main(String[] args) {

        JFrame jframe = new JFrame();
        jframe.setTitle("Title");
        jframe.setResizable(false);

        //This creates one row and two equally divided columns
        GridLayout gridLayout = new GridLayout(0, 2);
        jframe.setLayout(gridLayout);
        gridLayout.layoutContainer(jframe);

        JPanel leftPanel = new JPanel();
        leftPanel.add(new Label("Left side"));
        jframe.add(leftPanel);

        JPanel rightPanel = new JPanel();
        rightPanel.add(new Label("Right side"));
        jframe.add(rightPanel);

        jframe.setSize(800, 500);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

以下是它的外观: enter image description here

面板不会调整大小,因为没有可见的分隔线。

相关问题