在GUI上创建边框

时间:2014-02-20 02:40:35

标签: java swing user-interface

GUI

我创建了这个图形界面,缺少的是“PERSONAL INFORMATION”边框。我知道setBorder(BorderFactory.createTitledBorder("PERSONAL INFORMATION))创建了一个边框,但我如何将标题定位到左上角,并输入正确的尺寸,以便在不使用布局管理器的情况下,该线不会在按钮下面。非常感谢。

1 个答案:

答案 0 :(得分:4)

      -

   import java.awt.*;
   import javax.swing.*;
   import javax.swing.border.*;

class BorderTest extends JFrame
 {
 public BorderTest()
 {
setTitle("Border Test");
setSize(450, 450);

JPanel content = (JPanel) getContentPane(); 
content.setLayout(new GridLayout(6,2));

JPanel p = new JPanel();
p.setBorder(new BevelBorder (BevelBorder.RAISED));
p.add(new JLabel("RAISED BevelBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new BevelBorder (BevelBorder.LOWERED));
p.add(new JLabel("LOWERED BevelBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new LineBorder (Color.black, 5));
p.add(new JLabel("Black LineBorder, thickness = 5"));
content.add(p);

p = new JPanel();
p.setBorder(new EmptyBorder (10,10,10,10));
p.add(new JLabel("EmptyBorder with thickness of 10"));
content.add(p);

p = new JPanel();
p.setBorder(new EtchedBorder (EtchedBorder.RAISED));
p.add(new JLabel("RAISED EtchedBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new EtchedBorder (EtchedBorder.LOWERED));
p.add(new JLabel("LOWERED EtchedBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new SoftBevelBorder (SoftBevelBorder.RAISED));
p.add(new JLabel("RAISED SoftBevelBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new SoftBevelBorder (SoftBevelBorder.LOWERED));
p.add(new JLabel("LOWERED SoftBevelBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new MatteBorder (new ImageIcon("spiral.gif")));
p.add(new JLabel("MatteBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new TitledBorder ( new MatteBorder (new ImageIcon("spiral.gif")), "Title String"));
p.add(new JLabel("TitledBorder using MatteBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new TitledBorder (new LineBorder (Color.black, 5), "Title String"));
p.add(new JLabel("TitledBorder using LineBorder"));
content.add(p);

p = new JPanel();
p.setBorder(new TitledBorder (
  new EmptyBorder (10,10,10,10),
  "Title String"));
p.add(new JLabel("TitledBorder using EmptyBorder"));
content.add(p);

setVisible(true);
 }

public static void main(String args[])
{
new BorderTest();
}
}
  1. 这个演示示例显示了所有类型的border.i认为是这样 对你有所帮助。

  2. enter image description here