javaseday28(Swing 插件 做个小界面)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值