gpt4 book ai didi

java - JFileChooser 不遵循外观

转载 作者:行者123 更新时间:2023-11-29 03:19:33 25 4
gpt4 key购买 nike

我正在制作一个文本编辑器,这是我的代码的基本版本。我使用 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 使整个事情看起来像我正在使用的平台,但 JFileChooser save 始终是 java 外观和感觉。有人可以帮忙吗?我可能把它放在了错误的位置,但我不知道在哪里

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();
}
}
}

最佳答案

代码中的 JFileChooserstatic,因此在 main 中设置外观之前实例化。

在实例化之前设置外观。因此,由于您的 JFileChooserstatic,因此两者都在 static block 中。

...
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();
...
...

关于java - JFileChooser 不遵循外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com