欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java观感示例分享

程序员文章站 2024-02-26 08:30:28
复制代码 代码如下:package com.hongyuan.gui; import java.awt.eventqueue;import java.awt.event....

复制代码 代码如下:

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();
    }
   }
  });
 }
}