编程语言
首页 > 编程语言> > java – 显示边框线的BorderLayout

java – 显示边框线的BorderLayout

作者:互联网

我不是很擅长Java GUI,需要寻求帮助.

我打算在BorderLayout的西边添加图像,中心是我的内容和底部的按钮.

我创建了一个空边框,在我的南面板和我的西面板和中心面板之间做了一些填充.现在我只想在南边界的顶部添加一条线.

如下面的屏幕截图所示,西面板和中心面板之间也有一条线,我该如何移除该线并将线保持在南面板的顶部?

附上我的代码:

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

解决方法:

要删除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.

请记住,您可以切换边框的顺序和样式.有关详细信息,请参阅tutorial.

标签:java,swing,border,border-layout
来源: https://codeday.me/bug/20190717/1489525.html