2014-05-09 1 views
0

JFileChooser보다 더 좋은 옵션이 있습니까? 내 응용 프로그램과 Java JFileChooser에서 창을 표시하고 싶습니다. JFileChooser를 사용자 정의하거나 모양을 변경하는 방법이 있습니까? 나는 모양과 느낌에 대해 알고 있지만 그것은 내 목적을 위해 일할 것입니다. JFileChooser는 작동하지만 이전 80 년대의 모습을 보여 주며 원하는 것은 아니며 프로그램 사용자도 마찬가지입니다.JFileChooser를 Microsoft 모양과 느낌으로 변경 하시겠습니까?

질문 : JFileChooser를 Microsoft Look and Feel으로 변환 할 수 있습니까?

코드 : 당신이 마이크로 소프트 플랫폼 이서에있는 경우

원인의
package TestMenu; 

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class FileChooserDemo extends JPanel implements ActionListener { 
    private static final long serialVersionUID = 1L; 
    static private final String newline = "\n"; 
    JButton openButton; 
    JButton saveButton; 
    JTextArea log; 
    JFileChooser fc; 

    public FileChooserDemo() { 
     super(new BorderLayout()); 

     log = new JTextArea(5,20); 
     log.setMargin(new Insets(5,5,5,5)); 
     log.setEditable(false); 
     JScrollPane logScrollPane = new JScrollPane(log); 

     fc = new JFileChooser(); 

     openButton = new JButton("Open a File..."); 
     openButton.addActionListener(this); 

     saveButton = new JButton("Save a File..."); 
     saveButton.addActionListener(this); 

     JPanel buttonPanel = new JPanel(); //use FlowLayout 
     buttonPanel.add(openButton); 
     buttonPanel.add(saveButton); 

     add(buttonPanel, BorderLayout.PAGE_START); 
     add(logScrollPane, BorderLayout.CENTER); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == openButton) { 
      int returnVal = fc.showOpenDialog(FileChooserDemo.this); 

      if (returnVal == JFileChooser.APPROVE_OPTION) { 
       File file = fc.getSelectedFile(); 
       log.append("Opening: " + file.getName() + "." + newline); 
      } else { 
       log.append("Open command cancelled by user." + newline); 
      } 
      log.setCaretPosition(log.getDocument().getLength()); 
     } else if (e.getSource() == saveButton) { 
      int returnVal = fc.showSaveDialog(FileChooserDemo.this); 
      if (returnVal == JFileChooser.APPROVE_OPTION) { 
       File file = fc.getSelectedFile(); 
       log.append("Saving: " + file.getName() + "." + newline); 
      } else { 
       log.append("Save command cancelled by user." + newline); 
      } 
      log.setCaretPosition(log.getDocument().getLength()); 
     } 
    } 

    protected static ImageIcon createImageIcon(String path) { 
     java.net.URL imgURL = FileChooserDemo.class.getResource(path); 
     if (imgURL != null) { 
      return new ImageIcon(imgURL); 
     } else { 
      System.err.println("Couldn't find file: " + path); 
      return null; 
     } 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("FileChooserDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new FileChooserDemo()); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     createAndShowGUI(); 
    } 
} 

답변

2

, 다음

try { 
     UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (UnsupportedLookAndFeelException ex) { 
     Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 
+0

...을 MainFrame.class.getName 당신의 JFrame의 클래스에 시도의 이름이다 이 코드를 넣은 클래스의 클래스입니다. 작동하고 그것이 내가 한 일이기 때문에 궁금합니다. –

+0

JFrame을 확장하고 contentPane에 add (FileChooserDemo())를 호출하는 클래스가 있어야하며 위 코드를 추가해야합니다. – Patrick

+0

나는 그렇게한다. 그러나 MainFrame.class.getName() .log ... 등은 MainFrame 클래스로 시도했기 때문에 내 클래스의 이름이고 MainFrame이라는 클래스를 생성하라고했습니다. 그래서 나는 그것이 틀렸다라고 알았다. 그런 다음 우연히 같은 패키지에있는 클래스의 이름으로 변경 했으므로 오류가 발생하지 않았습니다. 그렇다면 아마도 내가 작업하고있는 클래스라고 생각했기 때문에 그것을 그대로두고 오류가 없었습니다. –

관련 문제