2012-11-17 41 views
1

可能重複:
Panel losing color如何在java中重置面板的外觀和感覺?

當我點擊激活文件選擇的按鈕,並添加生成的文件使用UIManager的顯示文件選擇作爲窗戶面板顏色消失病程選擇器。

import javax.swing.JPanel; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.filechooser.FileSystemView; 
import javax.swing.JFileChooser; 
import javax.swing.plaf.FileChooserUI; 

@SuppressWarnings("serial") 
public class pan extends JPanel implements DropTargetListener { 

    private DefaultListModel listModel = new DefaultListModel(); 
    private JButton addbutton; 
    private JButton removebutton; 
    private JButton selectbutton; 
    private JButton lockbutton; 
    private JButton unlockbutton; 

    /** 
    * Create the panel. 
    */ 
    public pan() { 
     setLayout(null); 
     addbutton = new JButton("New button"); 
     addbutton.setBounds(10, 10, 90, 100); 
     addbutton.addActionListener(new Action()); 
     add(addbutton); 

     removebutton = new JButton("New button"); 
     removebutton.setBounds(110, 10, 90, 100); 
     add(removebutton); 

     selectbutton = new JButton("New button"); 
     selectbutton.setBounds(210, 10, 90, 100); 
     add(selectbutton); 

     lockbutton = new JButton("New button"); 
     lockbutton.setBounds(310, 10, 90, 100); 
     add(lockbutton); 

     unlockbutton = new JButton("New button"); 
     unlockbutton.setBounds(410, 10, 90, 100); 
     add(unlockbutton); 

     JLabel headerLabel = new JLabel("New label"); 
     headerLabel.setBorder(new BevelBorder(BevelBorder.RAISED, 
      Color.LIGHT_GRAY, Color.GRAY, null, null)); 
     headerLabel.setUI(new ModifLabelUI()); 
     headerLabel.setBounds(10, 120, 635, 30); 
     add(headerLabel); 
    } 


    class Action implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      if(e.getSource()==addbutton){ 
       JFileChooser filechooser=new JFileChooser(); 
       filechooser.setMultiSelectionEnabled(true);    
       try { 
    UIManager.setLookAndFeel(UIManager 
      .getSystemLookAndFeelClassName()); 
      } catch (ClassNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (InstantiationException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IllegalAccessException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (UnsupportedLookAndFeelException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

       filechooser.updateUI(); 
       filechooser.showOpenDialog(new pan()); 
       File files=filechooser.getSelectedFile(); 
       listModel.addElement(files); 
     }  
    } 
} 

如果刪除了UImanger

回答

2

The documentation問題......您可能希望確保用戶界面管理器設置成首先使用的外表和感覺你想使用...

編輯對於一個具體的例子@see This Post

public static void main(String[] args) { 
    try { 
      // Set System L&F 
     UIManager.setLookAndFeel(
      UIManager.getSystemLookAndFeelClassName()); 
    } 
    catch (UnsupportedLookAndFeelException e) { 
     // handle exception 
    } 
    catch (ClassNotFoundException e) { 
     // handle exception 
    } 
    catch (InstantiationException e) { 
     // handle exception 
    } 
    catch (IllegalAccessException e) { 
     // handle exception 
    } 

    new SwingApplication(); //Create and show the GUI. 
} 
+0

你做其他事情的答案你測試代碼和見p roblem! –

+0

@YaserHar對不起?這是一個問題嗎? –

+0

不要擔心測試使框架和測試代碼,然後調用按添加按鈕,並添加任何文件,然後你會看到面板顏色不見 –