[英]Java swing Rendering Issue
I am new to Java programming. 我是Java编程的新手。 I created a simple Notepad using swing in Windows 7. It was working fine there. 我在Windows 7中使用swing创建了一个简单的记事本。 Later I moved it to Windows 8 and now I got some rendering issue. 后来我将其移至Windows 8,现在出现了一些渲染问题。 The frames are not rendering properly as shown below. 帧无法正确渲染,如下所示。
Here is my Code: 这是我的代码:
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("");
}
}
}
}
Here's a screenshot of the problem (Windows 8): 这是问题的屏幕截图(Windows 8):
I am using Java 8. Thanks. 我正在使用Java8。谢谢。
I've also ran into a Swing rendering issue. 我还遇到了Swing渲染问题。 The HelloWorld Java 8 application that worked fine on MAC didn't work on Windows 7. The problem was with the video drivers on Windows. 在MAC上正常运行的HelloWorld Java 8应用程序在Windows 7上无法正常工作。问题出在Windows上的视频驱动程序上。
Details here: http://yakovfain.com/2014/06/27/swing-rendering-seems-to-be-broken-in-java-8/ 此处的详细信息:http: //yakovfain.com/2014/06/27/swing-rendering-seems-to-be-broken-in-java-8/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.