Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

java - Two JPanels in one JFrame

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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():

enter image description here

Calling setSize():

enter image description here


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...