第十三周课程总结

第十三周课程总结

  • 菜单组件
    JMenuBar
    用来摆放JMenu组件,JMenuBar的常用方法
  • JMenu常用方法
  • JMenuItem
    为菜单中加入菜单项,可以理解为一种特殊的JButton组件
    JMenuItem的常用方法
  • JFileChooser的常用方法
  • 实现键盘监听
package org.lxh.demo18.keyeventdemo;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//此类定义时直接继承了JFrame类丶实现KeyListener接口
public class MyKeyHandle extends JFrame implements KeyListener {

    private JTextArea text=new JTextArea();
    public MyKeyHandle(){
        super.setTitle("Welcome To MLDN");
        JScrollPane scr =new JScrollPane(text);              //加入滚动条
        scr.setBounds(5,5,300,200);
        super.add(scr);                                      //在窗体中加入组件
        text.addKeyListener(this);                           //加入Key监听
        super.setSize(310,210);                              //设置窗体大小
        super.setVisible(true);                              //显示窗体
        super.addWindowListener(new WindowAdapter() {        //加入事件监听
        //覆写窗口关闭方法
            public void windowClosing(WindowEvent arg0) {
                System.exit(1);                              //系统退出
            }
    });
    public void keyTyped(KeyEvent e) {                        //键盘按下
        text.append(":"+e.getKeyChar()+"\n"); 
        
    }   
     
    public void keyReleased(KeyEvent e) {                      //键盘松开
        text.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"松开\n");
        
    }
    public void keyTyped(KeyEvent e) {                        //键入内容
        text.append("输入的内容是:"+e.getKeyChar()+"\n"); 
    public class MyKeyEventDemo01{
        public void main(String[] args) {
            new MyKeyHandle();
        }
    }

}
  • 显示单选钮
package org.lxh.demo18.jradiodemo;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.Jpanel;
import javax.swing.JRadioButton;
class MyRadio
{
    //定义窗体
    private JFrame frame = new JFrame("选择");
    //获取窗体容器
    private Container cont = frame.getContentPane();
    //定义一个单按钮
    private JRadioButton jradio1 = new JRadioButton("英雄联盟");
    //定义一个单选纽
    private JRadioButton jradio2 = new JRadioButton("穿越火线");
    //定义一个单选纽
    private JRadioButton jradio2 = new JRadioButton("地下城与勇士");
    //定义一个单选纽    
    private JPanel pan = new JPanel();
    //定义一个面板
    public MyRadio()
    {
        //定义一个面板的设置边框显示条
        pan.setBorder(BorderFactory.createTitledBorder("请选择你最喜欢的网络游戏"));
        //定义排版样式
        pan.setLayout(new GridLayout(3, 1));
        pan.add(this.jradio1);
        pan.add(this.jradio2);
        cont.add(pan);
        this.frame.setSize(400, 200);
        this.frame.setVisible(true);
        //监听窗体关闭事件
        this.frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent obj)
            {
                System.exit(1);
            }
        });
    }
}
public class JRadioButtonDemo01
{
    public static void main(String args[])
    {
        new MyRadio();
    }
}
posted @ 2019-11-23 22:53  无名xxx  Views(116)  Comments(0Edit  收藏  举报