JTextArea中内容看不见
代码见链接http://yun.baidu.com/share/link?shareid=1006085437&uk=3037160217&third=0打开.txt或....
代码见链接
http://yun.baidu.com/share/link?shareid=1006085437&uk=3037160217&third=0
打开.txt或.java的文件后无法显示,但内容是存在的,因为再保存到新路径后有内容,但如果重新输入再保存就不行了,求助 展开
http://yun.baidu.com/share/link?shareid=1006085437&uk=3037160217&third=0
打开.txt或.java的文件后无法显示,但内容是存在的,因为再保存到新路径后有内容,但如果重新输入再保存就不行了,求助 展开
1个回答
2014-01-07
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JTextAreaLoadFile {
public static void main(String args[]){
new JTextAreaLoadFile().test();
}
void test(){
new NoteFrame();
}
class NoteFrame extends JFrame {
public NoteFrame() {
this.setLocation(200, 200);
setTitle("MINI JAVA IDE");
Container c = getContentPane();
add((p1 = new TextPanel()) );
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);
JMenu fileMenu = new JMenu("文件");
JMenuItem newItem = new JMenuItem("新建");
newItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p1.area1.setText("");
}
});
fileMenu.add(newItem);
JMenuItem openItem = new JMenuItem("打开");
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.showOpenDialog(NoteFrame.this);
filePath = chooser.getSelectedFile().getPath();
try{
File f = new File(filePath);
FileInputStream in = new FileInputStream(f);
int n = in.available();
byte[] b = new byte[n];
in.read(b, 0, n);
fileText = new String(b);
p1.area1.setText(fileText);
}
catch (IOException e1){
}
}
});
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("保存");
saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.showSaveDialog(NoteFrame.this);
String filePath = chooser.getSelectedFile().getPath();
try {
FileWriter out = new FileWriter(new File(filePath));
out.write(p1.area1.getText());
out.flush();
} catch (IOException e1){
}
}
});
fileMenu.add(saveItem);
JMenuItem exitItem = new JMenuItem("退出");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("编辑");
JMenuItem copyItem = new JMenuItem("复制");
copyItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p1.area1.copy();
}
});
editMenu.add(copyItem);
JMenuItem cutItem = new JMenuItem("剪切");
cutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p1.area1.cut();
}
});
editMenu.add(cutItem);
JMenuItem pasteItem = new JMenuItem("粘贴");
pasteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p1.area1.paste();
}
});
editMenu.add(pasteItem);
JMenu proMenu = new JMenu("编译");
JMenuItem debugItem = new JMenuItem("调试");
debugItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String str = p1.area1.getText();
Runtime ce = Runtime.getRuntime();
Process process = null;
try {
process = ce.exec("javac " + filePath);
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedInputStream is = new BufferedInputStream(process.getErrorStream());
byte[] buf = new byte[4096];
int n = -1;
boolean boo;
boolean boo1 = true;
try {
while ((n = is.read(buf, 0, buf.length)) != -1) {
boo = false;
boo1 = false;
}
} catch (IOException e1) {
e1.printStackTrace();
}
if (boo1) {
p1.area2.append("\n调试成功!");
boo = true;
} else {
p1.area2.append("\n调试失败!");
boo1 = true;
}
try {
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
process.destroy();
}
});
proMenu.add(debugItem);
JMenuItem runItem = new JMenuItem("运行");
runItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Runtime ce = Runtime.getRuntime();
byte[] buf = new byte[4096];
int n = -1;
String s1 = filePath;
int i=s1.lastIndexOf("\\");
int j=s1.lastIndexOf(".");
String s2 = s1.substring(i+1, j);
//取出文件名,存入s2
Process process1 = null;
try {
process1 = ce.exec("java " + s2);
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(process1.getInputStream());
try {
while ((n = bis.read(buf, 0, buf.length)) != -1) {
p1.area2.append('\n' + new String(buf, 0, n));
}
} catch (IOException e1) {
e1.printStackTrace();
}
try {
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
process1.destroy();
}
});
proMenu.add(runItem);
JMenu helpMenu = new JMenu("帮助");
JMenuItem aboutItem = new JMenuItem("快捷键");
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "提示:\nCtrl+C:复制\nCtrl+V:粘贴\nCtrl+X:剪切\nCtrl+A:全选");
}
});
helpMenu.add(aboutItem);
menubar.add(fileMenu);
menubar.add(editMenu);
menubar.add(proMenu);
menubar.add(helpMenu);
pack();// 使用该语句,使panel的大小与frame相适应
p1.area1.setText(fileText);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public String fileText, filePath;
public String title;
public TextPanel p1;
}
class TextPanel extends JPanel {
public TextPanel() {
this.setLayout(new BorderLayout());
this.setSize(200, 200);
area1 = new JTextArea(20, 40);
area2 = new JTextArea(20, 40);
area2.setEditable(false);
JScrollPane scroll1 = new JScrollPane(area1);
add(scroll1, BorderLayout.NORTH);
JScrollPane scroll2 = new JScrollPane(area2);
add(scroll2, BorderLayout.SOUTH);
}
public JTextArea area1;
public JTextArea area2;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询