关闭

设置边框

200人阅读 评论(0) 收藏 举报

package swing;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;

public class BorderTest {
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   @Override
   public void run() {
    BorderFrame frame = new BorderFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
 }
}

class BorderFrame extends JFrame {
 public BorderFrame() {
  this.setTitle("Border Test");
  this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
  buttonPanel = new JPanel();
  demoPanel = new JPanel();
  group = new ButtonGroup();
  
  addRadioButton("Lowered bevel", BorderFactory.createLoweredBevelBorder());
  addRadioButton("Raised bevel", BorderFactory.createRaisedBevelBorder());
  addRadioButton("Etched", BorderFactory.createEtchedBorder());
  addRadioButton("Line", BorderFactory.createLineBorder(Color.BLUE));
  addRadioButton("Matte", BorderFactory.createMatteBorder(10, 10, 10, 10, Color.BLUE));
  addRadioButton("Empty", BorderFactory.createEmptyBorder());
  
  Border etched = BorderFactory.createEtchedBorder();
  Border titled = BorderFactory.createTitledBorder(etched, "Border types");
  buttonPanel.setBorder(titled);
  
  this.setLayout(new GridLayout(2, 1));
  this.add(buttonPanel);
  this.add(demoPanel);
 }
 
 private void addRadioButton(String buttonName, final Border b) {
  JRadioButton button = new JRadioButton(buttonName);
  button.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent event) {
    demoPanel.setBorder(b);
   }
  });
  group.add(button);
  buttonPanel.add(button);
 }

 public static final int DEFAULT_WIDTH = 600;
 public static final int DEFAULT_HEIGHT = 200;
 
 private JPanel buttonPanel;
 private JPanel demoPanel;
 private ButtonGroup group;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:101401次
    • 积分:1753
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:0篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论