一个JFrame中有两个JPanel [英] Two JPanels in one JFrame

查看:405
本文介绍了一个JFrame中有两个JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use two JPanels in one JFrame, with an invisible horizontal line between them. I played a little bit and got this:

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


    }
}

现在,第一个问题是如何关闭面板之间线的可调整大小"?以及如何使它不可见"?也许除了拆分窗格外还可以使用其他东西吗?

Now, the first problem is how can I turn off the "resizability" of the Line between the panels? And how do I make it "invisible"? Maybe use something else than split pane?

第二,如何仅使用JPanel的一侧? (我正在开发一个可让您在左侧绘制圆的应用程序.)

Second of all, how do can I work with only one side of the JPanel? (I am working on an application that lets you draw a circle on the left hand side).

这似乎是一个简单的问题,但是我对Java还是陌生的.

This seems like an easy question but I am relatively new to Java.

推荐答案

如之前在 BorderLayout GridBagLayout ,但您却在两个面板的中间重新放置"split"行,您可以使用 GridLayout ,无论窗口是否调整大小,这将使两个面板的尺寸相同.

As said before in a comment by @MadProgrammer you can use BorderLayout or GridBagLayout but as you're placing the "split" line right in the middle of both panels you could use GridLayout which will make both panels be of the same size no matter if the window is resized.

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

I didn't tried with GridBagLayout but I did an example on how you could achieve this pane separation without using a JSplitPane.

对于GridLayout,您所需要做的就是将元素添加到左窗格中(在我的示例中,我使用了JLabel来区分它们),而在BorderLayout中,您需要指定要在其中显示的面板像我一样画一个要向左对齐的圆(WEST常量).

With GridLayout all you need to do is add the elements to the left pane (in my example I used a JLabel to differentiate them) while in BorderLayout you need to specify that the panel where you'll be painting the circle to be aligned to the left (WEST constant) as I did.

但是,如果您使用BorderLayout方法并将文本或元素添加到右侧窗格中,则它们将在右侧对齐,您可以通过使用另一种

However if you use BorderLayout approach and add text or elements to the right pane, they will be aligned to the right, you can fix it by "boxing" the elements in another pane with a different Layout.

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的大小都不足以显示出差异:

I didn't call pack() in this example because the size of both panels (or JLabels in this case was not tall enough to show the difference:

使用pack():

致电setSize():

  1. 别忘了将程序放在事件调度线程(EDT),我是通过在main方法上编写以下几行来做到这一点的:

  1. Don't forget to place your program on the Event Dispatch Thread (EDT), I did it by writing these lines on the main method:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        new Application().createAndShowGui();
    }
});

  • 不要将所有代码放在构造函数上,否则将很难维护

  • Don't place all your code on the constructor, otherwise it will be hard to maintain

    这篇关于一个JFrame中有两个JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆