简体   繁体   English

JFileChooser不遵循外观

[英]JFileChooser not following look and feel

i am making a text editor, and this is the basic version of my code. 我正在做一个文本编辑器,这是我代码的基本版本。 I used UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 我使用了UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); to make the whole thing look like the platform I am using, but the JFileChooser save is always the java look and feel. 使整个外观看起来像我正在使用的平台,但是JFileChooser save的始终是Java外观。 Can anybody help? 有人可以帮忙吗? I might be putting it in the wrong spot, but I don't know where 我可能把它放在错误的位置,但是我不知道在哪里

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

@SuppressWarnings("serial")
    public class TextEditor extends JPanel {
    static Container pane;
    static Container paneText;
    static BasicFrame frame;
    static JTextArea textArea;
    static JScrollPane areaScrollPane;
    static FileFilter txtFile;
    static JFileChooser save = new FileChooser(System.getProperty("user.home//documents"));

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,               UnsupportedLookAndFeelException, IOException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        frame = BasicFrame.getInstance();
        pane = frame.getContentPane();
        paneText = new JPanel();
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        areaScrollPane = new JScrollPane(textArea);
        areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        areaScrollPane.setPreferredSize(new Dimension(250, 250));
        int hGap = 10;
        int vGap = 20;
        pane.setLayout(new FlowLayout(FlowLayout.LEFT, hGap, vGap));
        Action SaveAs = new SaveAs("Save File", "Writes the text file");
        JButton one = new JButton(SaveAs);
        one.addActionListener(null);
        txtFile = new FileNameExtensionFilter("Text File (.txt)", "txt");
        save.addChoosableFileFilter(txtFile);
        save.setFileFilter(txtFile);
        save.setAcceptAllFileFilterUsed(true);
        pane.add(areaScrollPane);
        pane.add(one);
        pane.add(paneText);
        paneText.setLayout(new BoxLayout(paneText, BoxLayout.Y_AXIS));
        frame.setSize(450, 320);
        frame.setVisible(true);
    }

    static class SaveAs extends AbstractAction {
        public SaveAs(String text, String desc) {
            super(text);
            putValue(SHORT_DESCRIPTION, desc);
        }

        public void actionPerformed(ActionEvent e) {
            save.setFileHidingEnabled(false);
            save.setApproveButtonText("Save");
            save.setSelectedFile(new File("new 1"));
            int actionDialog = save.showSaveDialog(null);
            if (actionDialog != JFileChooser.APPROVE_OPTION) {
                return;
            } else {
                log("Done!", true);
            }
            String name = save.getSelectedFile().getAbsolutePath();
            if (!name.endsWith(".txt") && save.getFileFilter() == txtFile) {
                name += ".txt";
            }
            BufferedWriter outFile = null;
            try {
                outFile = new BufferedWriter(new FileWriter(name));
                textArea.write(outFile);

            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (outFile != null) {
                    try {
                        outFile.close();
                    } catch (IOException ioee) {
                    }
                }
            }
        }

        private void log(String msg, boolean remove) {

            JLabel label1;
            label1 = new JLabel(msg);
            if (remove) {
                paneText.removeAll();
            }
            paneText.add(label1);
            paneText.validate();
            pane.validate();
            new Thread() {
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                    paneText.removeAll();
                    paneText.validate();
                    pane.validate();
                }
            }.start();
        }
    }
}

The JFileChooser in your code is static and therefore is instantiated before the Look and Feel is set in main . 您代码中的JFileChooserstatic ,因此在main设置外观后会实例化。

Set the Look and Feel before instantiation. 在实例化之前设置外观。 So, both in a static block since your JFileChooser is static . 因此,由于您的JFileChooserstatic ,因此两者都处于static块中。

...
static FileFilter txtFile;
static JFileChooser save;

static {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (ClassNotFoundException | 
           InstantiationException | 
           IllegalAccessException | 
           UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
    save = new JFileChooser(System.getProperty("user.home//documents"));
}

public static void main(String[] args)  {
    frame = new JFrame();
    pane = frame.getContentPane();
    ...
    ...

Instead of System.getProperty("user.home//documents") , type (System.getProperty("user.home") + "/documents") (Assuming your documents folder is called "documents"). 代替System.getProperty("user.home//documents") ,键入(System.getProperty("user.home") + "/documents") (假设您的documents文件夹称为“ documents”)。 The former would return null , setting the JFileChooser 's location to its default -- the user's home directory. 前者将返回null ,将JFileChooser的位置设置为其默认位置-用户的主目录。

This is because "user.home//documents" is not a defined key for System.getProperty() , while "user.home" is. 这是因为"user.home//documents"不是System.getProperty()的已定义键,而"user.home"是。

You should also set your JFileChooser to a new JFileChooser(<path>) rather than a new FileChooser(<path>) 您还应该将JFileChooser设置为new JFileChooser(<path>)而不是new FileChooser(<path>)

The LookAndFeel declaration must be made before the initialization of the Object, otherwise the object won't take on those LookAndFeel properties. 必须在对象初始化之前进行LookAndFeel声明,否则该对象将不会具有那些LookAndFeel属性。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

THEN 然后

save = new FileChooser(System.getProperty("user.home//documents"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM