Java摇摆 | BevelBorder 和 SoftBevelBorder
BevelBorder 和 SoftBevelBorder 是 javax.swing.Border 包的一部分。这个包包含不同的组件边框。 BevelBorder 是一个简单的两行斜角边框的实现。斜角边框和软斜角边框几乎相同,但软斜角边框软化了边角。
BevelBorder 类的构造函数:
- BevelBorder(int Type) :创建具有指定类型的斜角边框,其颜色将从传递给 paintBorder 方法的组件的背景颜色派生。
- BevelBorder(int Type, Color h, Color s) :创建具有指定类型、高光和阴影颜色的斜角边框。
- BevelBorder(int Type, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor) :创建具有指定类型、高亮和阴影颜色的斜角边框。
SoftBevelBorder 类的构造函数:
- SoftBevelBorder(int Type) : 创建一个具有指定类型的斜角边框,其颜色将从传递给 paintBorder 方法的组件的背景颜色派生。
- SoftBevelBorder(int Type, Color h, Color s) :创建具有指定类型、高光和阴影颜色的斜角边框。
- SoftBevelBorder(int Type, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor) : 创建具有指定类型、高光和阴影颜色的斜角边框。
常用的方法有:
method explanation getBevelType() returns the type of bevel border getBorderInsets(Component c, Insets insets) Reinitialize the insets parameter with this Border’s current Insets. getHighlightInnerColor() Returns the inner highlight color of the bevel border. getHighlightInnerColor(Component c) Returns the inner highlight color of the bevel border when rendered on the specified component. getHighlightOuterColor() Returns the outer highlight color of the bevel border. getHighlightOuterColor(Component c) Returns the outer highlight color of the bevel border when rendered on the specified component. getShadowInnerColor() Returns the inner shadow color of the bevel border. getShadowInnerColor(Component c) Returns the inner shadow color of the bevel border when rendered on the specified component. getShadowOuterColor() Returns the outer shadow color of the bevel border. getShadowOuterColor(Component c) Returns the outer shadow color of the bevel border when rendered on the specified component. isBorderOpaque() returns whether border is opaque or not
下面的程序说明了 Bevel Border 类:
- 创建具有指定类型的简单斜角边框的程序:要创建斜角边框,我们首先创建一个 JPanel 对象p ,所有边框都将应用于该对象。 JPanel 将托管在 JFrame f 内,这是该程序中最外层的容器。为了设置斜角边框,我们创建了 2 个 JLabel 对象,“l”和“l1”,一个用于凸起的类型边框,另一个用于降低的类型边框。边框由函数l.setBorder() 和 l1.setBorder() 应用。最后,通过 p.add()函数将边框添加到 JPanel,并通过 f.show() 显示结果。
Java
// Java Program to create a simple bevel
// border with specified type
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel s = new bevel();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new BevelBorder(BevelBorder.RAISED));
// set border for label
l1.setBorder(new BevelBorder(BevelBorder.LOWERED));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
Java
// java Program to apply bevel border with
// specified colors to highlight and shadow
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel1 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel1 s = new bevel1();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new BevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
// set border for label
l1.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
Java
// java Program to create a simple Soft bevel border
// with specified type
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel2 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel2 s = new bevel2();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
// set border for label
l1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
Java
// Java Program to apply soft bevel border with
// specified colors to highlight and shadow
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel3 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel3 s = new bevel3();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new SoftBevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
// set border for label
l1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
- 输出:
- 将指定颜色的斜角边框应用于高亮和阴影的程序:要创建具有高亮颜色的斜角边框,我们首先创建一个 JPanel 对象p ,所有边框都将应用于此对象。 JPanel 将托管在 JFrame f 内,这是该程序中最外层的容器。为了设置斜角边框,我们创建了 2 个 JLabel 对象,“l”和“l1”,一个用于凸起的类型边框,另一个用于降低的类型边框。边框由函数l.setBorder() 和 l1.setBorder() 应用。颜色作为参数传递给这些构造函数,例如:Color.red 等。最后,边框通过 p.add()函数添加到 JPanel,结果由 f.show() 显示。
Java
// java Program to apply bevel border with
// specified colors to highlight and shadow
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel1 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel1 s = new bevel1();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new BevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
// set border for label
l1.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
- 输出:
下面的程序说明了 SoftBevelBorder 类:
- 创建具有指定类型的简单软斜角边框的程序:要创建软斜角边框,我们首先创建一个 JPanel 对象p ,所有边框都将应用于此对象。 JPanel 将托管在 JFrame f 内。为了设置斜角边框,我们创建了 2 个 JLabel 对象,“l”和“l1”。边框由函数l.setBorder() 和 l1.setBorder() 应用。为了使边框变软,我们在 setBorder() 方法的参数中调用构造函数,如“new SoftBevelBorder()”行所示。最后,通过 p.add()函数将边框添加到 JPanel,并通过 f.show() 显示结果。
Java
// java Program to create a simple Soft bevel border
// with specified type
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel2 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel2 s = new bevel2();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
// set border for label
l1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
- 输出:
- 将指定颜色的软斜角边框应用于高亮和阴影的程序:要创建软斜角边框,我们首先创建一个 JPanel 对象p ,所有边框都将应用于此对象。 JPanel 将托管在 JFrame f 内。为了设置斜角边框,我们创建了 2 个 JLabel 对象,“l”和“l1”。边框由函数l.setBorder() 和 l1.setBorder() 应用。为了使边框变软,我们在 setBorder() 方法的参数中调用构造函数,如“new SoftBevelBorder()”行所示。颜色作为参数传递给这些构造函数,例如:Color.red 等)。最后,通过 p.add()函数将边框添加到 JPanel,并通过 f.show() 显示结果。
Java
// Java Program to apply soft bevel border with
// specified colors to highlight and shadow
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class bevel3 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// create a object
bevel3 s = new bevel3();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel(" this is bevel border of raised type");
// create a label
JLabel l1 = new JLabel(" this is bevel border of lowered type");
// set border for panel
l.setBorder(new SoftBevelBorder(BevelBorder.RAISED, Color.red,
Color.blue));
// set border for label
l1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, Color.black,
Color.red, Color.pink, Color.yellow));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize(400, 400);
f.show();
}
}
- 输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译。
参考:
- https://docs.oracle.com/javase/7/docs/api/javax/swing/border/BevelBorder.html
- https://docs.oracle.com/javase/7/docs/api/javax/swing/border/SoftBevelBorder.html