关于保存 另存方法的问题

harvis 发布于 2016/03/25 22:00
阅读 53
收藏 0
public class JMenuTest {
private JFrame frame = new JFrame("记事本");
private JTextArea jta = new JTextArea(20, 20);
private JPopupMenu jpm = new JPopupMenu();
private FileDialog fd_load=new FileDialog(frame,"打开文件",FileDialog.LOAD);
private FileDialog fd_save=new FileDialog(frame,"保存文件",FileDialog.SAVE);
    private String file="";
public void fangfa() {
MyMonitor mm = new MyMonitor();
jta.setOpaque(true);
frame.add(jta, BorderLayout.CENTER);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("编辑");
JMenu m3 = new JMenu("格式");
JMenu m4 = new JMenu("帮助");


JMenuItem mi11 = new JMenuItem("新建");
JMenuItem mi12 = new JMenuItem("打开");
JMenuItem mi13 = new JMenuItem("保存");
JMenuItem mi14 = new JMenuItem("另存");
JMenuItem mi15 = new JMenuItem("退出");
mi12.addActionListener(mm);
mi13.addActionListener(mm);
mi14.addActionListener(mm);
mi15.addActionListener(mm);


m1.add(mi11);
m1.add(mi12);
m1.add(mi13);
m1.add(mi14);
m1.addSeparator();
m1.add(mi15);


JCheckBoxMenuItem mi31 = new JCheckBoxMenuItem("二进制");// 可选择的
JMenuItem mi32 = new JMenuItem("字体");
JMenu mi33 = new JMenu("背景");
JMenuItem red1 = new JMenuItem("red");
JMenuItem green1 = new JMenuItem("green");
JMenuItem blue1 = new JMenuItem("blue");
JMenuItem zdy = new JMenuItem("用户自定义");


mi31.addActionListener(mm);
red1.addActionListener(mm);
blue1.addActionListener(mm);
green1.addActionListener(mm);
m3.add(mi31);
m3.add(mi32);
m3.add(mi33);
mi33.add(red1);
mi33.add(blue1);
mi33.add(green1);
mi33.addSeparator();
mi33.add(zdy);


mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);


JMenuItem start = new JMenuItem("start");
JMenuItem stop = new JMenuItem("stop");
jpm.add(start);
jpm.add(stop);
start.addActionListener(mm);
stop.addActionListener(mm);
jta.add(jpm);
jta.addMouseListener(mm);


frame.setJMenuBar(mb);
frame.setBounds(200, 100, 400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


class MyMonitor extends MouseAdapter implements ActionListener {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {               //???
jpm.show(jta, e.getX(), e.getY());
}
}


public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
jpm.show(jta, e.getX(), e.getY());
}
}


public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("red")) {
jta.setBackground(Color.RED);
}
if (s.equals("blue")) {
jta.setBackground(Color.BLUE);


}
if (s.equals("green")) {
jta.setBackground(Color.green);
}
if (s.equals("start")) {
jta.setText("生活多美好");
}
if (s.equals("stop")) {
jta.setText("bye!");
}
if (s.equals("退出")) {
System.exit(0);
}
if(s.equals("打开")){            ------------------------------------------------------??
fd_load.setVisible(true);
String d=fd_load.getDirectory();//??
String f=fd_load.getFile();
if((d!=null) && (f!=null)){
file=d+f;
this.loadFile();
}
}
if(s.equals("保存")){
if((file==null)||file.equals("")){
this.saveFileAs();
}else{
this.saveFile();
}
}
if(s.equals("另存")){

this.saveFileAs();
}


}


private void saveFile() {
String content=jta.getText();
try {
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.println(content);
pw.close();
} catch (IOException e) {
e.printStackTrace();
}

}


private void saveFileAs() {
fd_save.setVisible(true);
String d=fd_save.getDirectory();
String f=fd_save.getFile();
if((d!=null)&&(f!=null)){
file=d+f;
this.saveFile();
frame.setTitle("my notepad-"+file);
}

}


private void loadFile() {
jta.setText("");
frame.setTitle("my notepad-"+file);
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String s=br.readLine();
while(s!=null){
jta.append(s+"\n");
s=br.readLine();
}
br.close();
} catch (IOException e) {
e.printStackTrace();    ------------------------------------------?
}

}


}


public static void main(String[] args) {
new JMenuTest().fangfa();


}


}


求大神帮我解释一下从打问号开始之后的地方吧 基本这里都不懂

加载中
返回顶部
顶部