BorderLayout shows border lines

I am not very good at Java GUI and need help.

I intend to add images to the west of mine BorderLayout

, in the center, to be my content and buttons at the bottom.

I created an empty border to make some spacers between my south panel and my west and center panels. Now I just want to add a line on top of the south border.

As shown in the screenshot below, is there a line between the west panel and the center panel, how can I remove this line and keep the line on top of the south panel?

Attached is my code:

enter image description here

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

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

        JPanel panel1 = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new FlowLayout());
        JPanel panel3 = new JPanel(new FlowLayout());
        JPanel panel4 = new JPanel(new FlowLayout());

        JFrame frame = new JFrame();

        panel2.add( new JLabel( "WEST <will be adding image here>" ));
        panel3.add( new JLabel( "CENTER <contents>"));  
        panel4.add( new JLabel( "SOUTH <will be adding buttons>" ));

        panel1.add(panel2, BorderLayout.WEST);
        panel1.add(panel3, BorderLayout.CENTER);
        panel1.add(panel4, BorderLayout.SOUTH);

        panel2.setBorder(BorderFactory.createRaisedBevelBorder());
        panel3.setBorder(BorderFactory.createRaisedBevelBorder());      
        panel4.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        frame.add(panel1); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(510,390);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}

      

+3


source to share


3 answers


To remove the border between WEST and CENTER, just remove their borders

panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createRaisedBevelBorder());

      

If you want to keep the border with the edge of the frame, add a border instead panel1

.

As for SOUTH, if you want to "add a line over the south border" and keep an empty border, use:



panel4.setBorder(BorderFactory.createCompoundBorder(
       BorderFactory.createEmptyBorder(10, 10, 10, 10),
       BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK)));

      

or createRaisedBevelBorder()

instead of createMatteBorder

.

Remember that you can switch the border order and style. See the tutorial for details .

Inner: matte, outer: emptyInner: empty, outer: matte

+6


source


Try the following:



import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

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

        JPanel panel1 = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new BorderLayout());
        JPanel panel3 = new JPanel(new FlowLayout());
        JPanel panel4 = new JPanel(new FlowLayout());
        JPanel panel5 = new JPanel(new FlowLayout());

        JFrame frame = new JFrame();

        panel4.add( new JLabel( "WEST <will be adding image here>" ));
        panel5.add( new JLabel( "CENTER <contents>"));  
        panel3.add( new JLabel( "SOUTH <will be adding buttons>" ));

        panel1.add(panel2, BorderLayout.CENTER);
        panel1.add(panel3, BorderLayout.SOUTH);
        panel2.add(panel4, BorderLayout.WEST);
        panel2.add(panel5, BorderLayout.CENTER);

        panel2.setBorder(BorderFactory.createRaisedBevelBorder());     
        panel3.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        frame.add(panel1); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(510,390);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}

      

+3


source


enter image description here
  `The following code adds components to the frame's content area. Because content panels use the default BorderLayout class, your code doesn't need to install a layout manager. The complete program is in the BorderLayoutDemo.java file.

...//Container pane = aFrame.getContentPane()...
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);

//Make the center component big, since that the 
//typical usage of BorderLayout.
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);

button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);

button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);

button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);

Specify the component location (for example, BorderLayout.LINE_END) as one of the arguments to the add method. If this component is missing from a container controlled by a BorderLayout object, make sure that the component location was specified and no another component was placed in the same location.

All tutorial examples that use the BorderLayout class specify the component as the first argument to the add method. For example:

add(component, BorderLayout.CENTER)  //preferred
However, the code in other programs specifies the component as the second  argument. For example, here are alternate ways of writing the preceding code:

add(BorderLayout.CENTER, component)  //valid but old fashioned
     or
add("Center", component)             //valid but error prone

      

+1


source







All Articles