您的位置:首页 > 产品设计 > UI/UE

javaSE基础编程——GUI窗体设计之记事本(使用监听器)

2015-09-05 13:25 477 查看
编写一个记事本,可以实现新建,帮助,和退出提示功能,并将图形界面的内容写入文件中

package com.cissst.software.notepad;

import java.awt.BorderLayout;

import java.awt.Window;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.FileWriter;

import java.io.IOException;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.KeyStroke;

import com.cissst.software.view.QQLogin2;

public class NotePad extends JFrame{

 private JMenuBar menuBar;

 private JTextArea txt;

 

 public NotePad(){

  this.setTitle("记事本");

  this.setLayout(new BorderLayout());

  //设置一个文本区域

  txt = new JTextArea();

  txt.setLineWrap(true);

  this.add(txt);

  //给文本域添加滚动面板

  JScrollPane scrollpane = new JScrollPane(txt);

  this.add(scrollpane);

  //窗口添加菜单

  JMenuBar menuBar = new JMenuBar();

  this.add(menuBar,BorderLayout.NORTH);

  //创建菜单对象“文件”

  JMenu menu = new JMenu("文件");

  menuBar.add(menu);

  //创建“文件”的二级标签

  JMenuItem newmenu = new JMenuItem("新建");

  //设置快捷键

  newmenu.setAccelerator(KeyStroke.getKeyStroke("F2"));

  menu.add(newmenu);

  JMenuItem openmenu = new JMenuItem("打开");

  menu.add(openmenu);

  JMenuItem exitmenu = new JMenuItem("保存");

  menu.add(exitmenu);

  JMenuItem rexitmenu = new JMenuItem("另存为");

  menu.add(rexitmenu);

  //添加分割线

  menu.addSeparator();

  JMenuItem installmenu = new JMenuItem("页面设置");

  menu.add(installmenu);

  JMenuItem printmenu = new JMenuItem("打印");

  menu.add(printmenu);

  //添加分割线

  menu.addSeparator();

  JMenuItem quitmenu = new JMenuItem("退出");

  menu.add(quitmenu);

  

  //创建“编辑”菜单

  JMenu menu1 = new JMenu("编辑");

  menuBar.add(menu1);

  //创建“编辑”的二级标题

  JMenuItem canclemenu = new JMenuItem("撤销");

  menu1.add(canclemenu);

  //添加分割线

  menu1.addSeparator();

  JMenuItem cutmenu = new JMenuItem("剪切");

  menu1.add(cutmenu);

  JMenuItem copymenu = new JMenuItem("复制");

  menu1.add(copymenu);

  JMenuItem pastemenu = new JMenuItem("粘贴");

  menu1.add(pastemenu);

  //添加分割线

  menu1.addSeparator();

  JMenuItem deletemenu = new JMenuItem("删除");

  menu1.add(deletemenu);

  JMenu menu2 = new JMenu("格式");

  menuBar.add(menu2);

  JMenu menu3 = new JMenu("查看");

  menuBar.add(menu3);

  JMenu menu4 = new JMenu("帮助");

  menuBar.add(menu4);

  //创建“帮助”的二级标题

  JMenuItem helpmenu = new JMenuItem("帮助");

  menu4.add(helpmenu);

  

  

  //1.给"新建"注册监听器

  newmenu.addActionListener(new ActionListener(){

   @Override

   public void actionPerformed(ActionEvent e) {

    openDialog(e);

   }

  });

  

  //2.给"打开"注册监听器

  openmenu.addActionListener(new ActionListener(){

   @Override

   public void actionPerformed(ActionEvent e) {

    openWindow(e);

   }

  });

  //3.给“保存”注册监听器

  exitmenu.addActionListener(new ActionListener(){

   @Override

   public void actionPerformed(ActionEvent e) {

    saveWindow(e);

   }

  });

  

  

  //4.给“另存为”注册监听器

  rexitmenu.addActionListener(new ActionListener(){

   @Override

   public void actionPerformed(ActionEvent e) {

    rsaveWindow(e);

   }

  });

  

  //给“退出”注册监听器

  quitmenu.addActionListener(new ActionListener(){

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    System.exit(0);

    

   }

  });

  

  //给”帮助“注册监听器

  helpmenu.addActionListener(new ActionListener(){

  @Override

  public void actionPerformed(ActionEvent e) {

   // TODO Auto-generated method stub

   JOptionPane.showOptionDialog(null, "帮助", "关于应用",

     JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,

     null, null, null);

  }

 });

  

  //设置窗体最大化显示

  this.setExtendedState(JFrame.MAXIMIZED_BOTH);

  //给当前窗口注册监听器

  this.addWindowListener(new WindowAdapter() {

  @Override

   public void windowClosing(WindowEvent e) {

   // TODO Auto-generated method stub

   super.windowClosing(e);

   closeWindow(e);

   }

  });

  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  this.setVisible(true);

 }

 

 //关闭窗体

 private void closeWindow(WindowEvent e) {

 // TODO Auto-generated method stub

 if(JOptionPane.showConfirmDialog(this, "确认退出系统吗?",

   "退出系统", JOptionPane.OK_CANCEL_OPTION)

   ==JOptionPane.OK_OPTION){

   Window mywindow = (Window) e.getSource();

   mywindow.setVisible(false);

   mywindow.dispose();

   System.exit(0);

   }

 }

 

 /*

  * 1.打开一个窗口

  */

 public void openDialog(ActionEvent e){

  //打开一个窗口

  NotePad np = new NotePad();

  np.setSize(600,600);

  np.setVisible(true);

 }

 //2.打开一个对话框

 public void openWindow(ActionEvent e){

  JDialog dialog = new JDialog();

  dialog.setTitle("文件编辑对话框");

  dialog.setSize(200, 200);

  dialog.setLocationRelativeTo(null);

  dialog.setVisible(true);

 }

 //3.保存

  public void saveWindow(ActionEvent e){

   String gender="";

       String content =  txt.getText();

       System.out.println("文本内容:" + content);

       

       //将图形界面的内容写入文件中

       try {

        FileWriter writer = new FileWriter("test.txt");

        writer.write(content);

        writer.flush();//清空缓存

       } catch (IOException e1) {

        // TODO Auto-generated catch block

        e1.printStackTrace();

       }

     }

 //4.另存为

  public void rsaveWindow(ActionEvent e){

   //打开一个窗口

   QQLogin2 qq = new QQLogin2();

   qq.setSize(600,600);

   qq.setVisible(true);

  }

  

 public static void main(String[] args) {

  new NotePad();

 }

}


package com.cissst.software.view;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

/**

 * 登录窗体

 * @author Administrator

 * @date 2015-8-11

 */

public class QQLoginFrame extends JFrame {

 //用户名

 private JLabel lbl_user;

 private JTextField txt_user;

 //密码

 private JLabel lbl_pwd;

 private JPasswordField txt_pwd;

 //登录按钮

 private JButton btnLogin;

 //退出按钮

 private JButton btnCancel;

 

 public QQLoginFrame(){

  this.setTitle("QQ窗口");

  this.setBounds(400, 400, 300, 400);

  //取消布局管理器,自定义组件的大小和位置

  this.setLayout(null);

  //用户名

  lbl_user = new JLabel("用户名:");

  lbl_user.setLocation(10, 10);

  lbl_user.setSize(50, 30);

  this.add(lbl_user);

  

  txt_user = new JTextField();

  txt_user.setLocation(70, 10);

  txt_user.setSize(200, 30);

  this.add(txt_user);

  

  //密码

  lbl_pwd = new JLabel("密码:");

  lbl_pwd.setLocation(10,40);

  lbl_pwd.setSize(50, 50);

  this.add(lbl_pwd);

  

  txt_pwd = new JPasswordField();

  txt_pwd.setLocation(70, 50);

  txt_pwd.setSize(200, 30);

  this.add(txt_pwd);

  

  //登陆按钮

  btnLogin = new JButton("登陆");

  btnLogin.setLocation(40, 100);

  btnLogin.setSize(70, 25);

  this.add(btnLogin);

  

  //退出按钮

  btnCancel = new JButton("退出");

  btnCancel.setLocation(150, 100);

  btnCancel.setSize(70, 25);

  this.add(btnCancel);

  

  

  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  this.setVisible(true);

  

 }

 

 public static void main(String[] args) {

  new QQLoginFrame();

 }

}

package com.cissst.software.view;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.FileWriter;

import java.io.IOException;

import java.util.LinkedList;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JChe
b6a5
ckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

import javax.swing.JRadioButton;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

/**

 * 登录窗体

 * @author Administrator

 * @date 2015-8-11

 */

public class QQLogin2 extends JFrame{

 //昵称

 private JLabel lbl_user;

 private JTextField txt_user;

 //密码

 private JLabel lbl_pwd;

 private JPasswordField txt_pwd;

 //确认密码

 private JLabel lbl_pwd1;

 private JPasswordField txt_pwd1;

 //性别

 private JLabel lbl_sex;

 private ButtonGroup btnGroup;

 private JRadioButton rdoMale;

 private JRadioButton rdoFemale;

 //爱好

 private JLabel lbl_hobby;

 private JCheckBox chcStudy;

 private JCheckBox chcReStudy;

 private JCheckBox chcRRStudy;

 //个性签名

 private JLabel lbl_sign;

 private JTextArea txt_sign;

 //登录按钮

 private JButton btnLogin;

 //退出按钮

 private JButton btnCancel;

 private JScrollPane scrollpane;

 

 public QQLogin2(){

  this.setTitle("QQ窗口");

  this.setBounds(400, 400, 300, 400);

  //取消布局管理器,自定义组件的大小和位置

  this.setLayout(null);

  //用户名

  lbl_user = new JLabel("昵称:");

  lbl_user.setLocation(10, 10);

  lbl_user.setSize(50, 30);

  this.add(lbl_user);

  

  txt_user = new JTextField();

  txt_user.setLocation(70, 10);

  txt_user.setSize(200, 30);

  this.add(txt_user);

  

  //密码

  lbl_pwd = new JLabel("密码:");

  lbl_pwd.setLocation(10,50);

  lbl_pwd.setSize(50, 30);

  this.add(lbl_pwd);

  

  txt_pwd = new JPasswordField();

  txt_pwd.setLocation(70, 50);

  txt_pwd.setSize(200, 30);

  this.add(txt_pwd);

  

  //确认密码

  lbl_pwd = new JLabel("确认密码:");

  lbl_pwd.setLocation(10,80);

  lbl_pwd.setSize(90,60);

  this.add(lbl_pwd);

    

  txt_pwd = new JPasswordField();

  txt_pwd.setLocation(70, 90);

  txt_pwd.setSize(200, 30);

  this.add(txt_pwd);

  

  //性别

  lbl_sex = new JLabel("性别:");

  lbl_sex.setLocation(10,140);

  lbl_sex.setSize(50, 30);

  this.add(lbl_sex);

  

  //创建单选按钮组件男

  btnGroup = new ButtonGroup();

  rdoMale = new JRadioButton("男");

  rdoMale.setLocation(60,125);

  rdoMale.setSize(80, 60);

  //默认该单选按钮组件选中状态

  rdoMale.setSelected(true);

  btnGroup.add(rdoMale);

  this.add(rdoMale);

  //创建单选按钮组件女

  rdoFemale = new JRadioButton("女");

  rdoFemale.setLocation(150,125);

  rdoFemale.setSize(80, 60);

  //默认该单选按钮组件选中状态

  rdoFemale.setSelected(true);

  btnGroup.add(rdoFemale);

  this.add(rdoFemale);

  

  //爱好

  lbl_hobby = new JLabel("爱好:");

  lbl_hobby.setLocation(10,185);

  lbl_hobby.setSize(50, 30);

  this.add(lbl_hobby);

  //第一个复选框

  chcStudy = new JCheckBox("学习");

  chcStudy.setLocation(60, 190);

  chcStudy.setSize(60, 20);

  this.add(chcStudy);

  //第二个复选框

  chcReStudy = new JCheckBox("再学");

  chcReStudy.setLocation(120, 190);

  chcReStudy.setSize(60, 20);

  this.add(chcReStudy);

  //第三个复选框

  chcRRStudy = new JCheckBox("又学");

  chcRRStudy.setLocation(180, 190);

  chcRRStudy.setSize(60, 20);

  this.add(chcRRStudy);

  

  //个性签名

  lbl_sign = new JLabel("个性签名:");

  lbl_sign.setLocation(10, 210);

  lbl_sign.setSize(90, 60);

  this.add(lbl_sign);

  

  txt_sign=new JTextArea(6,10);

  txt_sign.setLineWrap(true);

  txt_sign.setBounds(70, 210, 200, 100);

  //给文本域添加滚动面板

  scrollpane=new JScrollPane(txt_sign);

  scrollpane.setBounds(70,210,200,100);

  this.add(scrollpane);

  //读取按钮

  btnLogin = new JButton("读出注册信息");

  btnLogin.setLocation(70, 470);

  btnLogin.setSize(70, 25);

  this.add(btnLogin);

  

  //退出按钮

  btnCancel = new JButton("退出");

  btnCancel.setLocation(170, 470);

  btnCancel.setSize(70, 25);

  this.add(btnCancel);

  

  //给按钮添加事件

  //1.匿名函数

  btnLogin.addActionListener(new ActionListener(){

  @Override

  public void actionPerformed(ActionEvent e){

   String gender="";

    if(rdoMale.isSelected()){

     gender ="男";

     }else{

       gender="女";

      }

      String hobby ="";

      if(chcStudy.isSelected()){

       hobby ="学习";

      }

      if (chcReStudy.isSelected()){

       hobby = hobby +" " + "再学";

      }

      if (chcRRStudy.isSelected()){

       hobby = hobby +" " + "又学";

      }

      

      //同学们完成:注册窗体读取注册然后将用户注册的内容写入文件.

       

       String myname =  txt_user.getText();

       String mysign =  txt_sign.getText();

       char[] password = txt_pwd.getPassword();

       

       System.out.println("昵称:" + myname);

       System.out.println("性别:" + gender);

       System.out.println("密码:" + password);

       System.out.println("兴趣爱好:" + hobby);

       System.out.println("个性签名:" + mysign);

       

       //将图形界面的内容写入文件中

       try {

        FileWriter writer = new FileWriter("test.txt");

        writer.write(myname);

        writer.write(gender);

        writer.write(password);

        writer.write(hobby);

        writer.write(mysign);

        writer.flush();//清空缓存

       } catch (IOException e1) {

        // TODO Auto-generated catch block

        e1.printStackTrace();

       }

     }

    });

    this.add(btnLogin);

    

    

  //关闭窗体程序结束

  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  this.setVisible(true);

  

 }

 

 public static void main(String[] args) throws Exception {

  new QQLogin2();

  

 }

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: