这周主要是学习了监听器还有制作记事本,主要可以由一下代码来体现(把书上代码集合的)

监听器

实验代码


package text12;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.*;
public class Text12 extends JFrame implements ItemListener/* KeyListener,MouseListener*/ {
    
    JTextArea ta;
    JScrollPane sp;
    JRadioButton pb,pb1,pb2;
    ButtonGroup group;
    JLabel la,la1;
    JPanel pane;
    JCheckBox cb,cb1;
    Container con;
    JList list,list1;
    String tv[] = {"美剧","韩剧","日剧","泰剧","港剧"};
    Vector<String> v;
    
    public Text12() {
        
        this.setTitle("监听器");
        v=new Vector<String>();
        v.add("男");
        v.add("女");
        list=new JList(tv);
        list1=new JList(v);
        list.setBorder(BorderFactory.createTitledBorder("喜欢看哪个国家的剧"));
        list1.setBorder(BorderFactory.createTitledBorder("性别"));
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        con=this.getContentPane();
        cb=new JCheckBox("舒畅");
        cb1=new JCheckBox("杨蓉");
        pane=new JPanel(new FlowLayout(FlowLayout.LEFT));
        pane.setBorder(BorderFactory.createTitledBorder("调查表"));
        la=new JLabel("最喜欢的男明星是:         ");
        la1=new JLabel("最喜欢的女明星是:          ");
        pb=new JRadioButton("朱一龙");
        pb1=new JRadioButton("肖战");
        pb2=new JRadioButton("邓伦");
        group=new ButtonGroup();
        group.add(pb);
        group.add(pb1);
        group.add(pb2);
        pane.add(la);
        pane.add(pb);
        pane.add(pb1);
        pane.add(pb2);
        
        pane.add(la1);
        pane.add(cb);
        pane.add(cb1);

        pane.add(list);
        pane.add(list1);
        pb.addItemListener(this);
        pb1.addItemListener(this);
        pb2.addItemListener(this);
        con.add(pane);
    //  ta=new JTextArea(4,5);
    //  sp=new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    //  ta.addKeyListener(this);
    //  ta.addMouseListener(this);
        
        this.pack();    
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }


    public static void main(String[] args) {
        new Text12();

    }


    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==pb) {
            System.out.println(pb.getText());
        }   
        
    }


    

/*  @Override
    public void keyTyped(KeyEvent e) {
        ta.append(e.getKeyChar()+"\n");
        
    }


    @Override
    public void keyPressed(KeyEvent e) {
        ta.append(e.getKeyCode()+"\n");
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        ta.append(KeyEvent.getKeyText(e.getKeyCode())+"\n");
        
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int c=e.getButton();
        String mouse;
        if(c==MouseEvent.BUTTON1) {
            mouse="左键";
        }else if(c==MouseEvent.BUTTON3){
            mouse="右键";
        }else {
            mouse="中键";
        }
        ta.append(mouse);
        
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int a=e.getClickCount();
        ta.append(a+"\n");
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        String str=MouseEvent.getMouseModifiersText(WIDTH);
        ta.append(str);
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();
        ta.append("("+x+","+y+")");
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
*/
}

记事本


package jishiben;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.*;
import javax.swing.undo.UndoManager;

public class Jishiben implements ActionListener{

    JFrame frame;
    JTextArea text;
    JScrollPane sp;
    JMenuItem newItem,windomItem,openItem,saveItem,othersaveItem,pagesetupItem,printItem,closeItem;
    JMenuBar bar;
    JMenu fileMenu,editMenu,formatMenu,checkMenu,helpMenu;
    JFileChooser choose;
    File file;
    JTable table;
    JMenuItem blankItem;
    UndoManager undoManager;
    
    public void Wenjian() {
        
        frame=new JFrame("记事本");
        text=new JTextArea();
        sp=new JScrollPane(text);
        bar=new JMenuBar();
        
        fileMenu=new JMenu("文件(F)");
        editMenu=new JMenu("编辑(E)");
        formatMenu=new JMenu("格式(O)");
        checkMenu=new JMenu("查看(V)");
        helpMenu=new JMenu("帮助(H)");
        
        //关于文件的选项
        newItem=new JMenuItem("新建(N)");
        windomItem=new JMenuItem("新窗口(W)");
        openItem=new JMenuItem("打开(O)");
        saveItem=new JMenuItem("保存(S)");
        othersaveItem=new JMenuItem("另存为(A)");
        pagesetupItem=new JMenuItem("页面设置(U)");
        printItem=new JMenuItem("打印(P)");
        closeItem=new JMenuItem("退出(X)");       
        
        //关于文件助记符的设置
        newItem.setMnemonic('N');
        windomItem.setMnemonic('W');
        openItem.setMnemonic('O');
        saveItem.setMnemonic('S');
        othersaveItem.setMnemonic('A');
        pagesetupItem.setMnemonic('U');
        printItem.setMnemonic('P');
        closeItem.setMnemonic('X');
        
        //关于文件快捷键的设置
        newItem.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_DOWN_MASK));
        windomItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
        openItem.setAccelerator(KeyStroke.getKeyStroke('O',KeyEvent.CTRL_DOWN_MASK));
        saveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK));
        othersaveItem.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_DOWN_MASK|KeyEvent.SHIFT_DOWN_MASK));
        printItem.setAccelerator(KeyStroke.getKeyStroke('P',KeyEvent.CTRL_DOWN_MASK));
        
        
        //关于文件菜单的动作监听
        newItem.addActionListener(this);
        windomItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        othersaveItem.addActionListener(this);
        pagesetupItem.addActionListener(this);
        printItem.addActionListener(this);
        closeItem.addActionListener(this);
        
        
        //向菜单栏添加
        bar.add(fileMenu);
        bar.add(editMenu);
        bar.add(formatMenu);
        bar.add(checkMenu);
        bar.add(helpMenu);
        
        
        frame.add(sp);
        frame.setJMenuBar(bar);
        frame.setSize(400,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        
    }
    
    public static void main(String[] args) {
        Jishiben shi=new Jishiben();
        shi.Wenjian();

    }
    public void actionPerformed(ActionEvent e){
        //关于文件下面选项的监听
        if(e.getSource()==newItem) {
            text.setText("");
        }else if(e.getSource()==windomItem) {
            Jishiben ben =new Jishiben();
            ben.Wenjian();
        }else if(e.getSource()==openItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                FileInputStream input=new FileInputStream(file);
                byte[] b=new byte[input.available()];
                input.read(b);
                text.append(new String(b));
                input.close();
                
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==othersaveItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                if(!file.exists()) {
                  file.createNewFile();
                }
                FileOutputStream output=new FileOutputStream(file);
                byte[] b=text.getText().getBytes();
                output.write(b);
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==saveItem) {
            choose=new JFileChooser();
            choose.showOpenDialog(null);
            file=choose.getSelectedFile();
            try {
                FileOutputStream output=new FileOutputStream(file);
                byte[] b=text.getText().getBytes();
                output.write(b);
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else if(e.getSource()==pagesetupItem) {
            
        }else if(e.getSource()==printItem) {
            
        }else if(e.getSource()==closeItem) {
            System.exit(1);
        }
    }

}

主要学习监听器及各种事件处理

1)键盘监听

使用KeyListener接口

2)鼠标监听

使用MouseListener接口,其中MouseEvent中有getButton(),getX(),getY()等方法

3)单选钮:JRadioButton及事件处理

方法:

setSelected();是否被选中

setText(String);设置显示文本

setIcon(Icon);设置图片

其事件处理使用ItenListener

4)复选框:JCheckBoxji事件处理

方法:

JCheckBox();创建文本对象

其事件处理也是用ItemListener接口

5)列表框:JList及事件处理

方法:

int[] getSeletrdIndices();返回所选择的所有数组

Jlist(ListModel/Object[] listData/Vector<?>listData);构造JList

其事件处理使用ListSelectionListener接口

菜单组件

JMenu、JMenuBar、JMenuItem可实现简单的菜单外观,使用事件监听可对应作出一些功能的实现,如代码记事本