Two JPanels in one JFrame

不羁的心 提交于 2019-12-12 01:28:25

问题


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?

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

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


回答1:


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.

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

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.

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

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:

Using pack():

Calling setSize():


Additional tips

  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();
        }
    });
    
  2. Don't place all your code on the constructor, otherwise it will be hard to maintain




回答2:


It looks like you can use GridLayout to do this. Here is what i think,

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

    }
}

Here is how it looks:

The panels will not resize as well as there is no line visible that seprates them.



来源:https://stackoverflow.com/questions/41685700/two-jpanels-in-one-jframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!