我有一个 JPanel,我想将 EtchedBorder.Lowered 添加到仅左边框。我知道 MatteBorders 可以做到这一点,但我想知道是否有办法用 EtchedBorder 做到这一点?
问问题
3809 次
4 回答
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();
}
});
}
}
于 2012-06-07T19:10:19.070 回答
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();
}
}
于 2012-06-07T20:10:45.017 回答
2
您可以通过子类化 EtchedBorder 或 AbstractBorder 轻松创建自己的边框类。
此代码将完全按照您的要求执行:仅在一侧绘制的 EtchedBorder。但是看起来不太好。EtchedBorder 只是在每一侧绘制两条线(一条深色,一条浅色)。它从线条在拐角处相遇的方式获得了 3 维外观。如果没有绘制完整的框,它看起来就不是 3 维的。
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);
}
});
于 2012-06-08T01:13:59.510 回答
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);
}
}
于 2013-10-15T23:40:07.167 回答