zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java观感示例分享

JAVA 示例 分享
2023-06-13 09:15:18 时间

复制代码代码如下:


packagecom.hongyuan.gui;

importjava.awt.EventQueue;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;

importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.SwingUtilities;
importjavax.swing.UIManager;
importjavax.swing.UnsupportedLookAndFeelException;

publicclassPlafTest{

 publicstaticvoidmain(String[]args){
  EventQueue.invokeLater(newRunnable(){

   @Override
   publicvoidrun(){
    PlafFrameframe=newPlafFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
 }

}

classPlafFrameextendsJFrame
{
 privateJPanelbuttonPanel;
 publicPlafFrame(){
  this.setTitle("PlafTest");
  this.setSize(400,300);

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

  this.add(buttonPanel);
 }
 voidmakeButton(Stringname,finalStringplafName){

  JButtonbutton=newJButton(name);
  buttonPanel.add(button);

  button.addActionListener(newActionListener(){

   @Override
   publicvoidactionPerformed(ActionEvente){
    try{
     //设置观感并更新组件
     UIManager.setLookAndFeel(plafName);
     SwingUtilities.updateComponentTreeUI(PlafFrame.this);
    }catch(ClassNotFoundException|InstantiationException
      |IllegalAccessException
      |UnsupportedLookAndFeelExceptione1){
     e1.printStackTrace();
    }
   }
  });
 }
}