我正在创建一个文本编辑器,这是我的代码的基本版本。我使用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();
}
}
}
答案 0 :(得分:4)
代码中的JFileChooser
为static
,因此会在main
中设置外观之前进行实例化。
在实例化之前设置外观。因此,自static
JFileChooser
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();
...
...
块中都存在{。}}。
{{1}}
答案 1 :(得分:0)
而不是System.getProperty("user.home//documents")
,键入(System.getProperty("user.home") + "/documents")
(假设您的文档文件夹名为“文档”)。前者将返回null
,将JFileChooser
的位置设置为默认位置 - 用户的主目录。
这是因为"user.home//documents"
不是System.getProperty()
的已定义键,而"user.home"
是。{/ p>
您还应该将JFileChooser
设置为new JFileChooser(<path>)
而不是new FileChooser(<path>)
答案 2 :(得分:0)
必须在初始化Object之前进行LookAndFeel声明,否则该对象将不会使用这些LookAndFeel属性。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
THEN
save = new FileChooser(System.getProperty("user.home//documents"));