怎么用java设计一个窗口,要求功能如下: 100
他要实现的功能就是:点击一个按钮(假如名为“输入文件”),就会出现一个窗口,让你选择电脑里的文件。就像你点击qq聊天窗口的“传输文件”按钮会出现让你选择文件的窗口一样同样...
他要实现的功能就是:点击一个按钮(假如名为“输入文件”),就会出现一个窗口,让你选择电脑里的文件。就像你点击qq聊天窗口的“传输文件”按钮会出现让你选择文件的窗口一样
同样,点击另一个按钮(假如名为“存储文件”),就会出现一个窗口让你选择一个存储路径
不知道大家明白没,谢谢了
主要是我不知道怎么下手,不知道怎么做
如果可行,奉上200积分,虽然不多,表达感谢之情
嗯?悬赏积分最高是100,那就100积分了,只要能解决问题,多少积分都可以
我的意思还有就是如果保存的是要一些文件(几个文件,如lucene索引后文件的存放位置),要的是选择一个文件夹,不要下面的要输入的文件名(因为几个文件),所以请问这样的对话框怎么设计,请给出代码,谢谢 展开
同样,点击另一个按钮(假如名为“存储文件”),就会出现一个窗口让你选择一个存储路径
不知道大家明白没,谢谢了
主要是我不知道怎么下手,不知道怎么做
如果可行,奉上200积分,虽然不多,表达感谢之情
嗯?悬赏积分最高是100,那就100积分了,只要能解决问题,多少积分都可以
我的意思还有就是如果保存的是要一些文件(几个文件,如lucene索引后文件的存放位置),要的是选择一个文件夹,不要下面的要输入的文件名(因为几个文件),所以请问这样的对话框怎么设计,请给出代码,谢谢 展开
3个回答
展开全部
我这有个联系swing的界面大全,里面包含了常用的主件,可以直接用
package demo.synth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.table.*;
public class SwingTest extends JFrame {
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public SwingTest() {
// 初始化所有模块
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
// c.setLayout(new BorderLayout());
this.setJMenuBar(menuTest);
c.add(leftPanel, BorderLayout.WEST);
c.add(rightPanel, BorderLayout.EAST);
c.add(centerPanel, BorderLayout.CENTER);
c.add(bottomPanel, BorderLayout.SOUTH);
// 利用无名内隐类,增加窗口事件
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
// 释放资源,退出程序
dispose();
System.exit(0);
}
});
setSize(800, 600);
setTitle("* All Groupware Of Swing *");
// 隐藏frame的标题栏,此功暂时关闭,以方便使用window事件
// setUndecorated(true);
setLocation(200, 150);
this.setVisible(true);
}
// //////////////////////////////////////////////////////////////////////////
/**
* 菜单栏处理模块 JMenuBar --+ --JMenu--+ --JMenuItem --ActionListener
*
*/
class MenuTest extends JMenuBar {
private JDialog aboutDialog;
/**
* 菜单初始化操作
*/
public MenuTest() {
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);
JMenuItem aboutMenuItem = new JMenuItem("About...", KeyEvent.VK_A);
fileMenu.add(exitMenuItem);
fileMenu.add(aboutMenuItem);
this.add(fileMenu);
aboutDialog = new JDialog();
initAboutDialog();
// 菜单事件
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
});
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// "关于"对话框的处理
aboutDialog.setVisible(true);
}
});
}
/**
* 返回关于对话框
*/
public JDialog getAboutDialog() {
return aboutDialog;
}
/**
* 设置"关于"对话框的外观及响应事件,操作和JFrame一样都是在内容 框架上进行的
*/
public void initAboutDialog() {
aboutDialog.setTitle("About");
Container con = aboutDialog.getContentPane();
// Swing 中使用html语句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel("<html><b><font size=5>"
+ "<center>All Groupware Of Swing !" + "<br>Kasam.Huang",
icon, JLabel.CENTER);
// JLabel aboutLabel = new JLabel("Swing
// 组件大全简体版!",icon,JLabel.CENTER);
con.add(aboutLabel, BorderLayout.CENTER);
aboutDialog.setSize(450, 225);
aboutDialog.setLocation(300, 300);
aboutDialog.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
dispose();
}
});
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最左边模块,继承JPanel,初始化内容为JTree JPanel--+ --JTree
*/
class LeftPanel extends JPanel {
private int i = 0;
public LeftPanel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("" + i);
root.add(child);
root.add(select);
child.add(child1);
JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
// 每个节点的行高
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// 内隐类不能直接引用外部类tree,1.外部变量可申明为final 2.新建外部类的对象
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode("" + i));
// Object s = tree.getLastSelectedPathComponent();//
// getSelectedValue();
// JOptionPane.showMessageDialog(null, s + " is selected",
// "Message !", JOptionPane.YES_OPTION);
}
});
tree.setPreferredSize(new Dimension(100, 300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
// scrollPane.setSize(100,350);
this.add(scrollPane);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最下面层模块,继承JPanel,初始化内容为进度条,并由定时器控制 JPanel--+ --JProcessBar --Timer
*/
class BottomPanel extends JPanel {
private JProgressBar pb;
public BottomPanel() {
pb = new JProgressBar();
pb.setPreferredSize(new Dimension(550, 20));
// 设置定时器,用来控制进度条的处理
Timer time = new Timer(1, new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent e) {
counter++;
pb.setValue(counter);
Timer t = (Timer) e.getSource();
// 如果进度条达到最大值重新开发计数
if (counter == pb.getMaximum()) {
t.stop();
counter = 0;
t.start();
}
}
});
time.start();
pb.setStringPainted(true);
pb.setMinimum(0);
pb.setMaximum(1000);
pb.setBackground(Color.white);
pb.setForeground(Color.red);
this.add(pb);
}
/**
* 设置进度条的数据模型
*/
public void setProcessBar(BoundedRangeModel rangeModel) {
pb.setModel(rangeModel);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最右边模块,继承JPanel,初始化各种按钮 JPanel--+ --JButton --JToggleButton -- JList --
* JCombox --JCheckBox ....
*/
class RightPanel extends JPanel {
public RightPanel() {
this.setLayout(new GridLayout(8, 1));
// 初始化各种按钮
JCheckBox checkBox = new JCheckBox("CheckButton");
JButton button = new JButton("OpenFile");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
int result = file.showOpenDialog(new JPanel());
if (result == file.APPROVE_OPTION) {
String fileName = file.getSelectedFile().getName();
String dir = file.getCurrentDirectory().toString();
JOptionPane.showConfirmDialog(null,
dir + "" + fileName, "SelectFile",
JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton = new JToggleButton("RadioButton");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("RadioButton 1", false);
JRadioButton radioButton2 = new JRadioButton("RadioButton 2", false);
// 组合框的处理
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("ComboBox");
comboBox.addItem("程序员");
comboBox.addItem("分析员");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox1 = (JComboBox) e.getSource();
comboBox1.addItem("程序员");
comboBox1.addItem("分析员");
}
});
// 列表框的处理
DefaultListModel litem = new DefaultListModel();
litem.addElement("SAT");
litem.addElement("SUN");
JList list = new JList(litem);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
JList l = (JList) e.getSource();
Object s = l.getSelectedValue();
JOptionPane.showMessageDialog(null, s, "Message !",
JOptionPane.YES_OPTION);
}
});
// 增加按钮组
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
// 增加各种按钮到JPanel中显示
add(button);
add(toggleButton);
add(checkBox);
add(radioButton1);
add(radioButton2);
add(comboBox);
add(list);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,
Color.LIGHT_GRAY, Color.blue));
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 中间层模块,继承JPanel,初始化页签,并在页签中设置文本区,表格, 文本区上下用分隔条分隔 JPanel--+ -JTabbedPane--+
* --Draw --JTable -JTextAreas -JText --JPopupMenu
*/
class CenterPanel extends JPanel {
public CenterPanel() {
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
JTextField textField = new JTextField(
"Text Area,Click <Text Button> Will select file !");
textField.setActionCommand("textField");
JTextPane textPane = new JTextPane();
textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
textPane.setText("Editor,Click the TextArea .");
// textPane.setForeground(new ImageIcon(""));
textPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JTextPane textPane = (JTextPane) e.getSource();
textPane.setText("Editor click command success!");
// textField.setText(""+textPane.getText());
}
});
/*
* UpperCaseDocument doc = new Document();
* textField.setDocumentsetDocument(doc);
* doc.addDocumentListener(new DocumentListener() { public void
* changedUpdate(DocumentEvent e){} public void
* removeUpdate(DocumentEvent e){} public void
* insertUpdate(DocumentEvent e) { Document text =
* (Document)e.getDocument(); text.setText("Copy Success !"); } });
*/
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textField, textPane);
JTable table = new JTable(10, 10);
// UIManager.get("Table.focusCellForeground");
// table.showHorizontalLines(true);
// table.showVerticalLines(true);
// table.setGridColor(Color.blue);
JPanel pane = new JPanel();
pane.add(table.getTableHeader(), BorderLayout.NORTH);
pane.add(table);
tab.addTab("Text Action", splitPane);
// tab.addTab(table.getTableHeader());
tab.addTab("Table Action", pane);
tab.setPreferredSize(new Dimension(500, 600));
this.add(tab);
this.setEnabled(true);
}
}
public static void main(String args[]){
new SwingTest();
}
}
package demo.synth;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.table.*;
public class SwingTest extends JFrame {
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public SwingTest() {
// 初始化所有模块
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
// c.setLayout(new BorderLayout());
this.setJMenuBar(menuTest);
c.add(leftPanel, BorderLayout.WEST);
c.add(rightPanel, BorderLayout.EAST);
c.add(centerPanel, BorderLayout.CENTER);
c.add(bottomPanel, BorderLayout.SOUTH);
// 利用无名内隐类,增加窗口事件
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
// 释放资源,退出程序
dispose();
System.exit(0);
}
});
setSize(800, 600);
setTitle("* All Groupware Of Swing *");
// 隐藏frame的标题栏,此功暂时关闭,以方便使用window事件
// setUndecorated(true);
setLocation(200, 150);
this.setVisible(true);
}
// //////////////////////////////////////////////////////////////////////////
/**
* 菜单栏处理模块 JMenuBar --+ --JMenu--+ --JMenuItem --ActionListener
*
*/
class MenuTest extends JMenuBar {
private JDialog aboutDialog;
/**
* 菜单初始化操作
*/
public MenuTest() {
JMenu fileMenu = new JMenu("File");
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_E);
JMenuItem aboutMenuItem = new JMenuItem("About...", KeyEvent.VK_A);
fileMenu.add(exitMenuItem);
fileMenu.add(aboutMenuItem);
this.add(fileMenu);
aboutDialog = new JDialog();
initAboutDialog();
// 菜单事件
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
});
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// "关于"对话框的处理
aboutDialog.setVisible(true);
}
});
}
/**
* 返回关于对话框
*/
public JDialog getAboutDialog() {
return aboutDialog;
}
/**
* 设置"关于"对话框的外观及响应事件,操作和JFrame一样都是在内容 框架上进行的
*/
public void initAboutDialog() {
aboutDialog.setTitle("About");
Container con = aboutDialog.getContentPane();
// Swing 中使用html语句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel("<html><b><font size=5>"
+ "<center>All Groupware Of Swing !" + "<br>Kasam.Huang",
icon, JLabel.CENTER);
// JLabel aboutLabel = new JLabel("Swing
// 组件大全简体版!",icon,JLabel.CENTER);
con.add(aboutLabel, BorderLayout.CENTER);
aboutDialog.setSize(450, 225);
aboutDialog.setLocation(300, 300);
aboutDialog.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
dispose();
}
});
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最左边模块,继承JPanel,初始化内容为JTree JPanel--+ --JTree
*/
class LeftPanel extends JPanel {
private int i = 0;
public LeftPanel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("" + i);
root.add(child);
root.add(select);
child.add(child1);
JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
// 每个节点的行高
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// 内隐类不能直接引用外部类tree,1.外部变量可申明为final 2.新建外部类的对象
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode("" + i));
// Object s = tree.getLastSelectedPathComponent();//
// getSelectedValue();
// JOptionPane.showMessageDialog(null, s + " is selected",
// "Message !", JOptionPane.YES_OPTION);
}
});
tree.setPreferredSize(new Dimension(100, 300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
// scrollPane.setSize(100,350);
this.add(scrollPane);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最下面层模块,继承JPanel,初始化内容为进度条,并由定时器控制 JPanel--+ --JProcessBar --Timer
*/
class BottomPanel extends JPanel {
private JProgressBar pb;
public BottomPanel() {
pb = new JProgressBar();
pb.setPreferredSize(new Dimension(550, 20));
// 设置定时器,用来控制进度条的处理
Timer time = new Timer(1, new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent e) {
counter++;
pb.setValue(counter);
Timer t = (Timer) e.getSource();
// 如果进度条达到最大值重新开发计数
if (counter == pb.getMaximum()) {
t.stop();
counter = 0;
t.start();
}
}
});
time.start();
pb.setStringPainted(true);
pb.setMinimum(0);
pb.setMaximum(1000);
pb.setBackground(Color.white);
pb.setForeground(Color.red);
this.add(pb);
}
/**
* 设置进度条的数据模型
*/
public void setProcessBar(BoundedRangeModel rangeModel) {
pb.setModel(rangeModel);
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 最右边模块,继承JPanel,初始化各种按钮 JPanel--+ --JButton --JToggleButton -- JList --
* JCombox --JCheckBox ....
*/
class RightPanel extends JPanel {
public RightPanel() {
this.setLayout(new GridLayout(8, 1));
// 初始化各种按钮
JCheckBox checkBox = new JCheckBox("CheckButton");
JButton button = new JButton("OpenFile");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
int result = file.showOpenDialog(new JPanel());
if (result == file.APPROVE_OPTION) {
String fileName = file.getSelectedFile().getName();
String dir = file.getCurrentDirectory().toString();
JOptionPane.showConfirmDialog(null,
dir + "" + fileName, "SelectFile",
JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton = new JToggleButton("RadioButton");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("RadioButton 1", false);
JRadioButton radioButton2 = new JRadioButton("RadioButton 2", false);
// 组合框的处理
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("ComboBox");
comboBox.addItem("程序员");
comboBox.addItem("分析员");
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox comboBox1 = (JComboBox) e.getSource();
comboBox1.addItem("程序员");
comboBox1.addItem("分析员");
}
});
// 列表框的处理
DefaultListModel litem = new DefaultListModel();
litem.addElement("SAT");
litem.addElement("SUN");
JList list = new JList(litem);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
JList l = (JList) e.getSource();
Object s = l.getSelectedValue();
JOptionPane.showMessageDialog(null, s, "Message !",
JOptionPane.YES_OPTION);
}
});
// 增加按钮组
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
// 增加各种按钮到JPanel中显示
add(button);
add(toggleButton);
add(checkBox);
add(radioButton1);
add(radioButton2);
add(comboBox);
add(list);
this.setBorder(new EtchedBorder(EtchedBorder.LOWERED,
Color.LIGHT_GRAY, Color.blue));
}
}
// //////////////////////////////////////////////////////////////////////////
/**
* 中间层模块,继承JPanel,初始化页签,并在页签中设置文本区,表格, 文本区上下用分隔条分隔 JPanel--+ -JTabbedPane--+
* --Draw --JTable -JTextAreas -JText --JPopupMenu
*/
class CenterPanel extends JPanel {
public CenterPanel() {
JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
JTextField textField = new JTextField(
"Text Area,Click <Text Button> Will select file !");
textField.setActionCommand("textField");
JTextPane textPane = new JTextPane();
textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
textPane.setText("Editor,Click the TextArea .");
// textPane.setForeground(new ImageIcon(""));
textPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
JTextPane textPane = (JTextPane) e.getSource();
textPane.setText("Editor click command success!");
// textField.setText(""+textPane.getText());
}
});
/*
* UpperCaseDocument doc = new Document();
* textField.setDocumentsetDocument(doc);
* doc.addDocumentListener(new DocumentListener() { public void
* changedUpdate(DocumentEvent e){} public void
* removeUpdate(DocumentEvent e){} public void
* insertUpdate(DocumentEvent e) { Document text =
* (Document)e.getDocument(); text.setText("Copy Success !"); } });
*/
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textField, textPane);
JTable table = new JTable(10, 10);
// UIManager.get("Table.focusCellForeground");
// table.showHorizontalLines(true);
// table.showVerticalLines(true);
// table.setGridColor(Color.blue);
JPanel pane = new JPanel();
pane.add(table.getTableHeader(), BorderLayout.NORTH);
pane.add(table);
tab.addTab("Text Action", splitPane);
// tab.addTab(table.getTableHeader());
tab.addTab("Table Action", pane);
tab.setPreferredSize(new Dimension(500, 600));
this.add(tab);
this.setEnabled(true);
}
}
public static void main(String args[]){
new SwingTest();
}
}
展开全部
java编程思想里现成的例子。
// Demonstration of File dialog boxes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FileChooserTest extends JFrame {
private JTextField
fileName = new JTextField(),
dir = new JTextField();
private JButton
open = new JButton("Open"),
save = new JButton("Save");
public FileChooserTest() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
add(p, BorderLayout.SOUTH);
dir.setEditable(false);
fileName.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(fileName);
p.add(dir);
add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
public static void main(String[] args) {
FileChooserTest f = new FileChooserTest();
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}
} ///:~
// Demonstration of File dialog boxes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FileChooserTest extends JFrame {
private JTextField
fileName = new JTextField(),
dir = new JTextField();
private JButton
open = new JButton("Open"),
save = new JButton("Save");
public FileChooserTest() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
add(p, BorderLayout.SOUTH);
dir.setEditable(false);
fileName.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(fileName);
p.add(dir);
add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
public static void main(String[] args) {
FileChooserTest f = new FileChooserTest();
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}
} ///:~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询