我写了一个小程序,可以在L&F的数量之间切换
只需从列表中选择L&F,按钮的外观就会有所不同。

但第二次机会不变

我是java的初学者:)

这是我的代码

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

int selectedIndices[] = jList1.getSelectedIndices();
try {
for (int j = 0; j < selectedIndices.length; j++){
if(j == 0){
  UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
   SwingUtilities.updateComponentTreeUI(this);
             this.pack();
}
if(j == 1){
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
 SwingUtilities.updateComponentTreeUI(this);
              this.pack();
 }
 if(j == 2){
 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
 SwingUtilities.updateComponentTreeUI(this);
             // this.pack();
}
if(j == 3){
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  SwingUtilities.updateComponentTreeUI(this);
             this.pack();
}
}
}
catch (Exception e) {
               }
     }

最佳答案

如果只选择一项(我想是这样),那么您的代码将始终选择MotifLookAndFeel:


selectedIndices.length为1
因此,在您的for循环中,j仅取0作为值
选择MotifLookAndFeel。


您可能想改为执行以下操作:

switch (jList1.getSelectedIndex()) {
    case 0:
       //select 1st L&F
       return;
    case 1:
       //select 2nd L&F
       return;
    case 2:
       //select 3rd L&F
       return;
}

07-24 21:06