Java中JFileChooser中有showNewDialog吗?
2个回答
2014-01-03
展开全部
1。。
JFileChooser这个类里只有showOpenDialog和showSaveDialog连个,如果要想showNewDialog的话,可以自己重写一下;
2。。但是这个showNewDialog干什么用呢???
3。。 下面是你的代码,我改过了。。,但是我不知道你想干什么,只是目前不报错。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class jsb implements ActionListener {
JFrame f;
JMenuBar bar;
JMenu file;
JMenu edit;
JMenuItem fo;
JMenuItem fn;
JMenuItem bc;
JMenuItem lbc;
JMenuItem fz;
JMenuItem zt;
JTextArea ta;
jsb() {
f = new JFrame("记事本");
Container con = f.getContentPane();
f.setSize(300, 400);
f.show();
ta = new JTextArea("写作");
bar = new JMenuBar();
f.setJMenuBar(bar);
con.add(bar);
file = new JMenu("文件");
edit = new JMenu("编辑");
fo = new JMenuItem("打开");
fn = new JMenuItem("新建");
bc = new JMenuItem("保存");
lbc = new JMenuItem("另存为");
fz = new JMenuItem("复制");
zt = new JMenuItem("粘贴");
bar.add(file);
bar.add(edit);
file.add(fo);
file.add(fn);
file.add(bc);
file.add(lbc);
edit.add(fz);
edit.add(zt);
fo.addActionListener(this);
fn.addActionListener(this);
bc.addActionListener(this);
lbc.addActionListener(this);
fz.addActionListener(this);
zt.addActionListener(this);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ae) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String s1 = null;
JFileChooser fc = new JFileChooser();// JFileChooser类
if (e.getSource() == fo) {
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)// JFileChooser.APPROVE_OPTION
// 选择确认(yes、ok)后返回该值
{
FileReader fr;
try {
fr = new FileReader(fc.getSelectedFile());
BufferedReader br = new BufferedReader(fr);
String s = "";
ta.setText("");
s = br.readLine();
if (s != null)
ta.append(s);
s = br.readLine();
fr.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
if (e.getSource() == fn) {
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String s = "";
ta.setText("");
}
}
if (e.getSource() == bc) {
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
FileWriter fw;
try {
fw = new FileWriter(fc.getSelectedFile());
fw.write(ta.getText());
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if (e.getSource() == lbc) {
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
FileWriter fw;
try {
fw = new FileWriter(fc.getSelectedFile());
fw.write(ta.getText());
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if (e.getSource() == fz) {
while (ta != null) {
s1 = ta.getText();
}
}
if (e.getSource() == zt) {
while (s1 != null)
ta.setText(s1);
}
}
public static void main(String args[]) {
new jsb();
}
}
JFileChooser这个类里只有showOpenDialog和showSaveDialog连个,如果要想showNewDialog的话,可以自己重写一下;
2。。但是这个showNewDialog干什么用呢???
3。。 下面是你的代码,我改过了。。,但是我不知道你想干什么,只是目前不报错。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class jsb implements ActionListener {
JFrame f;
JMenuBar bar;
JMenu file;
JMenu edit;
JMenuItem fo;
JMenuItem fn;
JMenuItem bc;
JMenuItem lbc;
JMenuItem fz;
JMenuItem zt;
JTextArea ta;
jsb() {
f = new JFrame("记事本");
Container con = f.getContentPane();
f.setSize(300, 400);
f.show();
ta = new JTextArea("写作");
bar = new JMenuBar();
f.setJMenuBar(bar);
con.add(bar);
file = new JMenu("文件");
edit = new JMenu("编辑");
fo = new JMenuItem("打开");
fn = new JMenuItem("新建");
bc = new JMenuItem("保存");
lbc = new JMenuItem("另存为");
fz = new JMenuItem("复制");
zt = new JMenuItem("粘贴");
bar.add(file);
bar.add(edit);
file.add(fo);
file.add(fn);
file.add(bc);
file.add(lbc);
edit.add(fz);
edit.add(zt);
fo.addActionListener(this);
fn.addActionListener(this);
bc.addActionListener(this);
lbc.addActionListener(this);
fz.addActionListener(this);
zt.addActionListener(this);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ae) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String s1 = null;
JFileChooser fc = new JFileChooser();// JFileChooser类
if (e.getSource() == fo) {
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)// JFileChooser.APPROVE_OPTION
// 选择确认(yes、ok)后返回该值
{
FileReader fr;
try {
fr = new FileReader(fc.getSelectedFile());
BufferedReader br = new BufferedReader(fr);
String s = "";
ta.setText("");
s = br.readLine();
if (s != null)
ta.append(s);
s = br.readLine();
fr.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
if (e.getSource() == fn) {
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String s = "";
ta.setText("");
}
}
if (e.getSource() == bc) {
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
FileWriter fw;
try {
fw = new FileWriter(fc.getSelectedFile());
fw.write(ta.getText());
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if (e.getSource() == lbc) {
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
FileWriter fw;
try {
fw = new FileWriter(fc.getSelectedFile());
fw.write(ta.getText());
fw.flush();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if (e.getSource() == fz) {
while (ta != null) {
s1 = ta.getText();
}
}
if (e.getSource() == zt) {
while (s1 != null)
ta.setText(s1);
}
}
public static void main(String args[]) {
new jsb();
}
}
2014-01-03
展开全部
有 getSelectedText();方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询