我有一个JPanel,我想添加一个EtchedBorder.Lowered只到左边框。我知道MatteBorders可以做到这一点,但我想知道是否有办法用EtchedBorder做到这一点?
答案 0 :(得分:5)
使用JSeparator代替EtchedBorder.Lowered,
但JSeparator默认只有行,但你可以构建有趣的边框
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
public class NestedLayout {
private JFrame frame = new JFrame();
private JPanel leftPanel = new JPanel();
public NestedLayout() {
leftPanel.setLayout(new BorderLayout());
leftPanel.setBorder(BorderFactory.createEmptyBorder(
10, //top
10, //left
10, //bottom
10)); //right
leftPanel.add(new JSeparator(JSeparator.VERTICAL), BorderLayout.CENTER);
leftPanel.setPreferredSize(new Dimension(40, 40));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(leftPanel, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new NestedLayout();
}
});
}
}
答案 1 :(得分:4)
您可以连接两个面板并在每个面板上添加“相对”的乱蓬蓬边框以重新创建蚀刻效果:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame {
public MainFrame() {
setLayout(new BorderLayout());
JPanel left = new JPanel();
left.setPreferredSize(new Dimension(200, 400));
left.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GRAY));
add(left, BorderLayout.WEST);
JPanel center = new JPanel();
center.setPreferredSize(new Dimension(200, 400));
center.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.WHITE));
add(center, BorderLayout.CENTER);
setVisible(true);
pack();
}
public static void main(String[] args) {
new MainFrame();
}
}
答案 2 :(得分:2)
您可以通过对EtchedBorder或AbstractBorder进行子类化来轻松创建自己的边框类。
此代码将完全按照您的要求执行:仅在一侧绘制的EtchedBorder。但它看起来并不好。 EtchedBorder每侧只绘制两条线(一条暗线,一条灯线)。它从线条在角落处的方式获得三维外观。如果没有绘制完整的框,它看起来不是三维的。
setBorder(new EtchedBorder() {
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.setColor(getShadowColor(c));
g.drawLine(x, y + height - 2, x, y);
g.setColor(getHighlightColor(c));
g.drawLine(x+1, y + height - 3, x+1, y + 1);
}
});
答案 3 :(得分:2)
延伸Yanflea给出的答案,你可以从两个不同的面板创建一个“对立面”来复制蚀刻边框的外观。
您可以使用CompoundBorder
在一个面板上创建它,如下所示:
CompoundBorder border = BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, Color.WHITE),
BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY)
); // This would create A line on the bottom
这样做的好处是你也可以做两面,然后看起来像这样:(注意,这只是一个有九个面板的网格,只有中间有边框,也比一个厚<1 >请参阅下面的代码,我在面板上使用setBackground来显示每个面板的位置)
!
import javax.swing.*;
import java.awt.*;
public class Test
{
public static void main(String[] args)
{
JFrame f = new JFrame("A Demo");
f.setLayout(new GridLayout(3,3));
f.setSize(400,400);
JPanel j1 = new JPanel(); //j1.setBackground(new Color(255,0,0));
JPanel j2 = new JPanel(); //j2.setBackground(new Color(200,0,0));
JPanel j3 = new JPanel(); //j3.setBackground(new Color(150,0,0));
JPanel j4 = new JPanel(); //j4.setBackground(new Color(100,0,0));
JPanel j5 = new JPanel(); //j5.setBackground(new Color(0,255,0));
JPanel j6 = new JPanel(); //j6.setBackground(new Color(0,200,0));
JPanel j7 = new JPanel(); //j7.setBackground(new Color(50,0,0));
JPanel j8 = new JPanel(); //j8.setBackground(new Color(0,150,0));
JPanel j9 = new JPanel(); //j9.setBackground(new Color(0,100,0));
j5.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 2, 2, Color.WHITE),
BorderFactory.createMatteBorder(0, 0, 2, 2, Color.GRAY)
));
f.add(j1);
f.add(j2);
f.add(j3);
f.add(j4);
f.add(j5);
f.add(j6);
f.add(j7);
f.add(j8);
f.add(j9);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}