边框布局显示边框线

3

我在Java GUI方面不是很擅长,需要寻求帮助。

我想要将图片添加到我的BorderLayout的西侧,中心是我的内容,底部是按钮。

我创建了一个空边框,在我的南侧面板和西侧以及中心面板之间留出一些填充。现在我只想在南侧边框的顶部添加一条线。

如下面的截图所示,西侧面板和中心面板之间有一条分隔线,我该如何移除它并保留南侧面板顶部的分隔线?

这是我的代码:

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个回答

6

要删除WEST和CENTER之间的边框,只需要将它们的边框删除即可。

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

如果你想保留他们与框架边缘的边框,则应将边框添加到panel1而不是边框。

至于SOUTH,在您想要“在南侧边框上方添加一条线”并保留空白边框时,请使用:

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

使用createRaisedBevelBorder()代替createMatteBorder,记得可以更改边框的顺序和样式。详见教程
图片中展示了内部使用matte边框,外部为空的效果,以及内部为空,外部使用matte边框的效果。

3
尝试这个:

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

在我看来,你的答案是最好的 :) - The Kanguru

1
以下代码向框架的内容面板添加组件。由于内容面板默认使用边界布局类(BorderLayout),因此代码不需要设置布局管理器。完整程序位于BorderLayoutDemo.java文件中。
...//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's 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's 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's 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接