9.Java图形用户界面设计说明书.ppt

import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; public class JFileChooserDemo extends JFrame { public JFileChooserDemo() { super(使用JFileChooser); final JTextArea ta = new JTextArea(5,20); ta.setMargin(new Insets(5,5,5,5)); ta.setEditable(false); JScrollPane sp = new JScrollPane(ta); final JFileChooser fc = new JFileChooser(); JButton openBtn = new JButton(打开文件...); openBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog( JFileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); ta.append(打开: + file.getName() + .\n); } else ta.append(取消打开命令.\n); } }); JButton saveBtn = new JButton(保存文件...); saveBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int returnVal = fc.showSaveDialog( JFileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION){ File file = fc.getSelectedFile(); ta.append(Saving: + file.getName() + .\n); } else ta.append(取消保存命令。\n); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(openBtn); buttonPanel.add(saveBtn); openBtn.setNextFocusableComponent(saveBtn); saveBtn.setNextFocusableComponent(openBtn); Container c = getContentPane(); c.add(buttonPanel, BorderLayout.NORTH); c.add(sp, BorderLayout.CENTER); } public static void main(String[] args) { JFrame frame = new JFileChooserDemo(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } 程序运行的开始界面如下图左,选择“打开文件”按钮后,即出现下图右的打开文件对话框,选择文件后将选择文件名显示到文本区域中。选择“保存文件”按钮,将出现文件保存对话框。 例9.23的运行界面。 9.4 鼠标和键盘事件 在GUI程序中,通常使用鼠标来进行人机交互操作。鼠标的移动、单击、双击等都会引发鼠标事件。为输入数据、操作命令等,也使用键盘。键盘的按下,释放也会引发键盘事件。下面简单介绍AWT的鼠标和键盘事件的处理。 9.4.1 鼠标事件 处理鼠标事件的程序要实现在java.awt.event包中定义的两个接口MouseListener和MouseMotionListener,在这两个接口中定义了未实现的鼠标事件处理方法。 接口MouseListener中的方法为: public void mousePressed(MouseEvent e)处理按下鼠标左

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档