javaseday28(Swing 插件 做个小界面)
来源:互联网 发布:滤镜软件原宿风 编辑:程序博客网 时间:2024/06/05 15:13
public class SwingDemo_02 extends javax.swing.JFrame{//需要继承后才能用它的对话框 protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); protected TextArea textArea = new TextArea(); private JFrame frame; private JTextField textField; private final Action action = new SwingAction(); protected JDialog dialog; private JFileChooser chooser = null; private JMenuBar menuBar; private JMenu mnNewMenu; private JMenu menu; private JDialog jd; private JMenuItem menuItem_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SwingDemo_02 window = new SwingDemo_02(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public SwingDemo_02() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textArea.setBounds(10, 77, 300, 186); frame.getContentPane().add(textArea); textField = new JTextField(); textField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER); showDir(textArea); } }); textField.setBounds(10, 32, 197, 23); frame.getContentPane().add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("ee"); btnNewButton.setAction(action); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /* * 通过点击按钮获取文本框中目录 * 将目录中的内容演示到文本区域 */ showDir(textArea); } }); btnNewButton.setBounds(280, 32, 93, 23); frame.getContentPane().add(btnNewButton); menuBar = new JMenuBar(); menuBar.setBounds(40, 10, 333, 21); frame.getContentPane().add(menuBar); menu = new JMenu("\u6211\u64E6"); menuBar.add(menu); mnNewMenu = new JMenu("\u83DC\u5355"); menuBar.add(mnNewMenu); JMenuItem menuItem = new JMenuItem("\u6253\u5F00"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { jMenuItemlActionPerformed( e); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); menuItem.setAction(action); mnNewMenu.add(menuItem); menuItem_1 = new JMenuItem("\u4FDD\u5B58"); menuItem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { jMenuItem2ActionPerformed(e); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); mnNewMenu.add(menuItem_1); } protected void showDir(TextArea textArea2) { // TODO Auto-generated method stub String dir_str = textField.getText(); File dir = new File(dir_str); if(dir.exists()&&dir.isDirectory()) { textArea.setText(""); String[] names = dir.list(); for(String name :names) { textArea.append(name+LINE_SEPARATOR); } } } private void jMenuItemlActionPerformed(ActionEvent e) throws IOException { chooser = new JFileChooser();// FileNameExtensionFilter filter = new FileNameExtensionFilter(// "JPG & GIF Images", "jpg", "gif");// chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION) { System.out.println("没有选取文件"); return; } File file = chooser.getSelectedFile(); BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while((line = br.readLine())!=null) { textArea.append(line+LINE_SEPARATOR); } br.close();// if(returnVal == JFileChooser.APPROVE_OPTION) { // System.out.println("You chose to open this file: " +// chooser.getSelectedFile().getName());// } } private void jMenuItem2ActionPerformed(ActionEvent e) throws IOException { chooser = new JFileChooser(); int returnVal = chooser.showSaveDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION) { System.out.println("没有选取文件"); return; } File file = chooser.getSelectedFile(); String txte = textArea.getText(); BufferedWriter br = new BufferedWriter(new FileWriter(file)); br.write(txte);//记事本不能解析 可以用别的 就有换行了 br.close(); } private class SwingAction extends AbstractAction { private static final long serialVersionUID = 1L; public SwingAction() { putValue(NAME, "呵呵"); putValue(SHORT_DESCRIPTION, "Some short description"); } public void actionPerformed(ActionEvent e) { } }}
阅读全文
0 0
- javaseday28(Swing 插件 做个小界面)
- eclipse安装插件 windowbuilder --- 可视化swing设计界面
- eclipse安装插件 windowbuilder --- 可视化swing设计界面
- eclipse安装插件 windowbuilder --- 可视化swing设计界面
- eclipse上安装使用Swing界面绘制插件WindowBuilder
- swing设置windows界面
- Swing界面布局(FormLayout)
- java swing 登陆界面
- Swing界面和模式
- Swing界面刷新问题
- Swing界面美化
- Swing界面刷新问题
- swing 界面编程
- swing界面编程记录
- Swing界面嵌套浏览器
- 【swing应用】注册界面
- Java Swing界面制作
- swing调整界面字体大小
- 词频统计python
- WebStorm设置手机测试服务器-局域网内其他设备访问
- 学生信息管理系统知识点
- python实现-字符串最后一个单词的长度
- java泛型
- javaseday28(Swing 插件 做个小界面)
- 遇到的问题---百度ueditor----An error occurred at line: 12 in the jsp file: /ueditor/jsp/controller.jsp
- v4l2 ioctl框架分析
- 矩阵快速幂
- oracle10g提示“身份证明检索失败”
- 关于顺序数组求最大差值或最大收益算法
- ubuntu14 上安装tensorflow 遇到的问题
- 数据结构之栈、队列算法
- 【51Nod1241】 特殊的排序