Java Swing | MatteBorder

MatteBorder是一个类,用于制作纯色或平铺图标的类似matte的边框。它是javax的一部分。摆动边框包,并包含不同样式的组件边框。此类帮助我们将图标或纯色设置为边框,也可以使用此类应用边框的插图。 该类的构造函数是:

null
  1. MatteBorder(图标tileIcon) :使用指定的平铺图标创建无光边框。
  2. MatteBorder(插图边框插图,颜色matteColor) :使用指定的插图和颜色创建无光边框。
  3. MatteBorder(插图Borders插图,图标tileIcon) :使用指定的插图和平铺图标创建无光边框。
  4. MatteBorder(int-top、int-left、int-bottom、int-right、Color matteColor) :使用指定的插图和颜色创建无光边框。
  5. MatteBorder(int-top、int-left、int-bottom、int-right、Icon-tileIcon) :使用指定的插图和平铺图标创建无光边框。

常用的方法有:

方法 解释
getBorderInsets() 返回边框的插图。
getBorderInsets(组件c,插图) 使用此边框的当前插图重新初始化insets参数。
getMatteColor() 返回用于平铺边框的颜色,如果正在使用平铺图标,则返回null。
getTileIcon() 返回边框的标题图标
IsBorder不透明() 返回边框是否不透明

下面的程序演示了MatteBorder类:

  1. 使用纯色应用无光边框的程序 :我们将创建一个框架 F 标题为“框架”,将创建一个面板,用作容器。我们将创建两个标签l1和l。我们将使用setborder()函数将两个框架的边框设置为无光边框,一个标签将有红色边框,另一个将有蓝色边框。我们将把标签添加到面板上,把面板添加到框架上。我们将使用setSize(400400)将框架的大小设置为400400,并使用show()显示框架。

JAVA

// java Program to apply matte border using solid colors
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte1 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
matte1 s = new matte1();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel( "  this is a matte border 2" );
// create a label
JLabel l1 = new JLabel( " this is a matte border 1" );
// set border for panel
l.setBorder( new MatteBorder( 4 , 4 , 4 , 4 , Color.red));
// set border for label
l1.setBorder( new MatteBorder( 4 , 4 , 4 , 4 , Color.blue));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize( 400 , 400 );
f.show();
}
}


  1. 输出 :

图片[1]-Java Swing | MatteBorder-yiteyi-C++库

  1. 使用图标应用蒙版边框的程序 :我们将创建一个名为“frame”的框架f,并将创建一个用作容器的面板。我们将创建两个标签l1和l。我们将使用setborder()函数将两个框架的边框设置为无光边框。一个标签将有一个图像图标作为边框,另一个标签将有另一个图像图标。我们将使用new ImageIcon()函数导入图像。我们将把标签添加到面板上,把面板添加到框架上。我们将使用setSize(400400)将框架的大小设置为400400,并使用show()显示框架。

JAVA

// java Program to apply matte border using  icons
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte 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
matte s = new matte();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel( "  this is a matte border 2" );
// create a label
JLabel l1 = new JLabel( " this is a matte border 1" );
// set border for panel
l.setBorder( new MatteBorder( new ImageIcon( "f:\gfg.png" )));
// set border for label
l1.setBorder( new MatteBorder( new ImageIcon( "f:\gfg.jpg" )));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize( 400 , 400 );
f.show();
}
}


  1. 输出 :

图片[2]-Java Swing | MatteBorder-yiteyi-C++库

  1. 通过指定插入,使用纯色和图像应用无光边框的程序 :我们将创建一个名为“frame”的框架f,并将创建一个用作容器的面板。我们将创建两个标签l1和l。我们将使用setborder()函数将两个框架的边框设置为无光边框。一个标签将有一个图像图标作为边框,另一个标签将有另一个图像图标。我们将使用new ImageIcon()函数导入图像。我们将使用new insets()函数指定边框的插入或宽度。我们将把标签添加到面板上,把面板添加到框架上。我们将使用setSize(400400)将框架的大小设置为400400,并使用show()显示框架。

JAVA

// java Program to apply matte border using
// solid color and image by specifying insets
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class matte3 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
matte3 s = new matte3();
// create a panel
JPanel p = new JPanel();
// create a label
JLabel l = new JLabel( "  this is a matte border 2" );
// create a label
JLabel l1 = new JLabel( " this is a matte border 1" );
// set border for panel
l.setBorder( new MatteBorder( new Insets( 4 , 7 , 4 , 10 ), Color.red));
// set border for label
l1.setBorder( new MatteBorder( new Insets( 10 , 4 , 10 , 4 ), new ImageIcon( "f:\gfg.png" )));
// add button to panel
p.add(l1);
p.add(l);
f.add(p);
// set the size of frame
f.setSize( 400 , 400 );
f.show();
}
}


  1. 输出 :

图片[3]-Java Swing | MatteBorder-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享