服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java观感示例分享

java观感示例分享

2019-11-12 14:26java技术网 JAVA教程

这篇文章主要介绍了java观感示例,该实例查询并生成了系统中存在观感对应的按钮并在用户点击相应按钮时将窗口的观感切换到指定的观感上

代码如下:

package com.hongyuan.gui;

 

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class PlafTest {

 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {

   @Override
   public void run() {
    PlafFrame frame=new PlafFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
 }

}

class PlafFrame extends JFrame
{
 private JPanel buttonPanel;
 public PlafFrame(){
  this.setTitle("PlafTest");
  this.setSize(400, 300);

  buttonPanel=new JPanel();
  //查询观感并生成按钮
  UIManager.LookAndFeelInfo[] infos=UIManager.getInstalledLookAndFeels();
  for(UIManager.LookAndFeelInfo info:infos){
   makeButton(info.getName(),info.getClassName());
  }

  this.add(buttonPanel);
 }
 void makeButton(String name,final String plafName){

  JButton button=new JButton(name);
  buttonPanel.add(button);

  button.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    try {
     //设置观感并更新组件
     UIManager.setLookAndFeel(plafName);
     SwingUtilities.updateComponentTreeUI(PlafFrame.this);
    } catch (ClassNotFoundException | InstantiationException
      | IllegalAccessException
      | UnsupportedLookAndFeelException e1) {
     e1.printStackTrace();
    }
   }
  });
 }
}

 

延伸 · 阅读

精彩推荐