我是Java编程的新手。我在Windows 7中使用swing创建了一个简单的记事本。它在那里工作得很好。后来我把它移到了Windows 8,现在我遇到了一些渲染问题。帧未正确呈现,如下所示。
这是我的代码:
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SpellChecker extends JFrame {
JTextArea txtrEditorpane = new JTextArea();
Boolean fileSaved = false,fileEdited=false;
String savedFileNAme = "";
public SpellChecker() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
if(fileEdited){
int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous File First?","Unsaved Changes",JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
saveFile();
}
}
}
});
try {
javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setTitle("Untitled.txt");
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNew = new JMenuItem("New");
mntmNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(fileEdited){
int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous File First?","Unsaved Changes",JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
saveFile();
}
}
setTitle("Untitled.txt");
txtrEditorpane.setText("");
}
});
mnFile.add(mntmNew);
JMenuItem mntmOpen = new JMenuItem("Open");
mntmOpen.addActionListener(new OpenL());
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(new SaveL());
mnFile.add(mntmSave);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(fileEdited){
int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous File First?","Unsaved Changes",JOptionPane.YES_NO_OPTION);
if(dialogResult == JOptionPane.YES_OPTION){
saveFile();
}
}
SpellChecker.this.dispose();
}
});
mnFile.add(mntmExit);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmFont = new JMenuItem("Font");
mntmFont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(SpellChecker.this);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
txtrEditorpane.setFont(font);
}
}
});
mnEdit.add(mntmFont);
JMenuItem mntmForecolor = new JMenuItem("ForeColor");
mntmForecolor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color initialcolor=txtrEditorpane.getForeground();
Color color=JColorChooser.showDialog(SpellChecker.this,"Select a color",initialcolor);
txtrEditorpane.setForeground(color);
}
});
mnEdit.add(mntmForecolor);
JMenuItem mntmBackgroundColor = new JMenuItem("BackGround Color");
mntmBackgroundColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Color initialcolor=txtrEditorpane.getBackground();
Color color=JColorChooser.showDialog(SpellChecker.this,"Select a color",initialcolor);
txtrEditorpane.setBackground(color);
}
});
mnEdit.add(mntmBackgroundColor);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
JMenuItem mntmAboutUs = new JMenuItem("About Us");
mnHelp.add(mntmAboutUs);
txtrEditorpane.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
if(fileSaved){
SpellChecker.this.setTitle(savedFileNAme+"*");
fileEdited=true;
}
else {
SpellChecker.this.setTitle("Untitled.txt*");
fileEdited=true;
}
}
});
JScrollPane sp = new JScrollPane(txtrEditorpane);
getContentPane().add(sp, BorderLayout.CENTER);
setVisible(true);
setSize(500, 500);
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
public void saveFile() {
String filename, dir;
FileWriter fw = null;
if (fileSaved) {
try {
fw = new FileWriter(savedFileNAme);
fw.write(txtrEditorpane.getText());
} catch (Exception err) {
} finally {
try {
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} else {
JFileChooser c = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Text file",
new String[] { "txt" });
c.setFileFilter(filter);
c.addChoosableFileFilter(filter);
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(SpellChecker.this);
if (rVal == JFileChooser.APPROVE_OPTION) {
filename = c.getSelectedFile().getName();
dir = c.getCurrentDirectory().toString();
savedFileNAme = dir + "\\" + filename + ".txt";
try {
fw = new FileWriter(savedFileNAme);
fw.write(txtrEditorpane.getText());
} catch (Exception err) {
} finally {
try {
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
fileSaved = true;
fileEdited=false;
}
if (rVal == JFileChooser.CANCEL_OPTION) {
// Do on Cancel
}
}
SpellChecker.this.setTitle(savedFileNAme);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Text file",
new String[] { "txt" });
c.setFileFilter(filter);
c.addChoosableFileFilter(filter);
String filename, dir;
// File "Open" dialog:
int rVal = c.showOpenDialog(SpellChecker.this);
if (rVal == JFileChooser.APPROVE_OPTION) {
filename = c.getSelectedFile().getName();
dir = c.getCurrentDirectory().toString();
savedFileNAme = dir + "\\" + filename ;
try {
String text = new Scanner(new File(savedFileNAme))
.useDelimiter("\\A").next();
txtrEditorpane.setText(text);
} catch (Exception err) {
} finally {
}
SpellChecker.this.setTitle(savedFileNAme);
fileSaved=true;
}
if (rVal == JFileChooser.CANCEL_OPTION) {
// filename.setText("You pressed cancel");
// dir.setText("");
}
}
}
}
以下是该问题的屏幕截图(Windows 8):
我正在使用Java 8.谢谢。
答案 0 :(得分:0)
我也遇到了Swing渲染问题。在MAC上运行良好的HelloWorld Java 8应用程序在Windows 7上无法运行。问题在于Windows上的视频驱动程序。
详细信息:http://yakovfain.com/2014/06/27/swing-rendering-seems-to-be-broken-in-java-8/