程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java觀感示例分享

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved