awt&swing 学习笔记(2)

对话框
Dome1

Frame frame =new Frame("这里测试dialog");

        //一个模式的,一个非模式的
        final Dialog d1 = new Dialog(frame, "模式对话框", true);
        final Dialog d2 = new Dialog(frame, "非模式对话框", false);

        //使用setbounds方法设置dialog的位置以及大小
        d1.setBounds(20,30,300,200);
        d2.setBounds(20,30,300,200);

        //创建两个按钮
        Button b1 = new Button("打开对话框模式");
        Button b2 = new Button("打开非对话框模式");
        //给这两个按钮添加,点击后的行为

        b1.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                d1.setVisible(true);
            }
        });

        b2.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                d2.setVisible(true);
            }
        });

        //把按钮添加到frame中

        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);


        frame.pack();

        frame.setVisible(true);

Dome2

Frame frame =new Frame("这里测试dialog");

        //一个模式的,一个非模式的
        final Dialog d1 = new Dialog(frame, "模式对话框", true);

        //使用setbounds方法设置dialog的位置以及大小
        d1.setBounds(20,30,300,200);

        d1.add(new TextField(30),BorderLayout.NORTH);

        d1.add(new Button("确定"));

        //创建两个按钮
        Button b1 = new Button("打开对话框模式");
        //给这两个按钮添加,点击后的行为

        b1.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                d1.setVisible(true);
            }
        });

        //把按钮添加到frame中

        frame.add(b1,BorderLayout.NORTH);


        frame.pack();

        frame.setVisible(true);

Dome3

Frame frame =new Frame("这里测试dialog");

        //创建两个文件框
        final FileDialog f1 = new FileDialog(frame, "打开文件获取文件",FileDialog.LOAD);
        final FileDialog f2 = new FileDialog(frame, "选择要打开的路径",FileDialog.SAVE);
        //创建两个按钮
        Button b1 = new Button("获取文件名");

        Button b2 = new Button("获取文件路径");



        //点击行为,获取文件名和路径
        b1.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                f1.setVisible(true);//代码会阻塞到这里

                //获取选择的路径,及文件
                String directory = f1.getDirectory();
                String file = f1.getFile();
                System.out.println("打开文件路径"+directory);
                System.out.println("打开文件名"+file);

            }
        });

        b2.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                f2.setVisible(true);

                String directory = f2.getDirectory();
                String file = f2.getFile();
                System.out.println("保存文件路径"+directory);
                System.out.println("保存文件名"+file);

            }
        });


        //把按钮添加到frame

        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);


        frame.pack();

        frame.setVisible(true);

事件

Frame frame =new Frame();

    TextField field =new TextField(30);

     Button ok=new Button("确定");


    public void init(){
    
    
        MyListener myListener =new MyListener();


        ok.addActionListener(myListener);


        //把tf和ok放到frame中
        frame.add(field,BorderLayout.NORTH);
        frame.add(ok);
        frame.pack();
        frame.setVisible(true);
    }
    private class MyListener implements ActionListener{
    
    

        public void actionPerformed(ActionEvent e) {
    
    
                field.setText("HelloWorld!");
        }
    }
    public static void main(String[] args) {
    
    
        new Dome1().init();
    }

Dome2

Frame frame =new Frame("测试");

    TextField tf =new TextField();

    Choice choice =new Choice();




    public void init(){
    
    


        choice.add("小明");
        choice.add("小红");
        choice.add("小白");


        //添加事件
        tf.addTextListener(new TextListener() {
    
    
            public void textValueChanged(TextEvent e) {
    
    
                System.out.println("当前的内容"+tf.getText());
            }
        });

        choice.addItemListener(new ItemListener() {
    
    
            public void itemStateChanged(ItemEvent e) {
    
    
                System.out.println("当前选中的内容是"+ e.getItem());
            }
        });


        //水平布局

        Box box = Box.createHorizontalBox();

        box.add(choice);
        box.add(tf);
        frame.add(box);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
    
    
        new Dome2().init();
    }
 public static void main(String[] args) {
    
    
        Frame frame =new Frame();

        frame.setBounds(300,300,500,400);

        frame.addWindowListener(new WindowAdapter() {
    
    
            @Override
            public void windowClosing(WindowEvent e) {
    
    
                //停止当前程序
                System.exit(0);
            }
        } );


        frame.pack();
        frame.setVisible(true);
    }

Graphics

public class Dome1 {
    
    


    private final String RECT="rect";
    private final String OVAL="oval";

    Frame frame =new Frame();
    Button btnRect =new Button("绘制矩形");
    Button btnOval =new Button("绘制椭圆");


    private String shape="";


    //自定义类,继承Canvas 类重写paint方法

    private class MyCanvas extends Canvas{
    
    
        @Override
        public void paint(Graphics g) {
    
    
            //绘制不同的图形
            if (shape.equals(RECT)){
    
    
                //绘制矩形
                g.setColor(Color.BLACK);//设置当前画笔的颜色为黑色
                g.drawRect(100,100,150,150);
            }else {
    
    
                //绘制椭圆
                g.setColor(Color.RED);//设置当前画笔的颜色为黑色
                g.drawOval(100,100,200,100);
            }


        }
    }

    //创建自定义的画布对象
    MyCanvas myCanvas =new MyCanvas();

    public void init(){
    
    


        btnRect.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                shape=RECT;
                myCanvas.repaint();
            }
        });

        btnOval.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                shape=OVAL;
                myCanvas.repaint();
            }
        });







        //创建panel

        Panel panel =new Panel();

        panel.add(btnRect);
        panel.add(btnOval);



        frame.add(panel,BorderLayout.SOUTH);

        //设置大小
        myCanvas.setPreferredSize(new Dimension(500,500));

        frame.add(myCanvas,BorderLayout.NORTH);



        frame.pack();
        frame.setVisible(true);


    }


    public static void main(String[] args) {
    
    

        new Dome1().init();


    }


}


菜单

Dome1

public class MeauDome1 {
    
    

    Frame frame =new Frame();


    MenuBar menuBar =new MenuBar();

    Menu fileMenu =new Menu("文件");
    Menu editMenu =new Menu("编辑");
    Menu fromMenu =new Menu("格式");


    //菜单项

    MenuItem auto =new MenuItem("自动换行");
    MenuItem copy =new MenuItem("复制");
    MenuItem paste =new MenuItem("粘贴");

    MenuItem comment = new MenuItem("注释",new MenuShortcut(KeyEvent.VK_Q,true));
    MenuItem cancelComment = new MenuItem("取消注释");


    TextArea ta =new TextArea(6,40);



    public void init(){
    
    

        //组装视图
        comment.addActionListener(new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                ta.append("你点击菜单: "+e.getActionCommand());
            }
        });

        fromMenu.add(comment);
        fromMenu.add(cancelComment);


        editMenu.add(auto);
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.add(fromMenu);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);

        frame.setMenuBar(menuBar);
        frame.add(ta);



        frame.pack();
        frame.setVisible(true);

    }




    public static void main(String[] args) {
    
    
        new MeauDome1().init();
    }
}

Dome2

public class MeauDome2 {
    
    

    Frame frame =new Frame();

    //文本区域
    TextArea textArea =new TextArea("我爱中华",6,40);

    //创建一个panel容器

    Panel panel =new Panel();

    //创建popupMenu

    PopupMenu popupMenu = new PopupMenu();

    MenuItem comment =new MenuItem("注释");
    MenuItem cancelComment =new MenuItem("取消注释");
    MenuItem copy =new MenuItem("复制");
    MenuItem save =new MenuItem("保存");
    public void init(){
    
    

        //组装视图

        ActionListener actionListener =new ActionListener() {
    
    
            public void actionPerformed(ActionEvent e) {
    
    
                String command = e.getActionCommand();
                textArea.append("你点击了: "+command);
            }
        };
        //添加点击事件

        comment.addActionListener(actionListener);
        cancelComment.addActionListener(actionListener);
        copy.addActionListener(actionListener);
        save.addActionListener(actionListener);

        popupMenu.add(comment);
        popupMenu.add(cancelComment);
        popupMenu.add(copy);
        popupMenu.add(save);

        panel.add(popupMenu);

        //设置panel的大小

        panel.setPreferredSize(new Dimension(400,300));

        //为panel注册鼠标事件

        panel.addMouseListener(new MouseAdapter() {
    
    
            @Override
            public void mouseReleased(MouseEvent e) {
    
    

                boolean flag = e.isPopupTrigger();

                if (flag){
    
    
                    //显示popmune
                    popupMenu.show(panel,e.getX(),e.getY());
                }



            }
        });
        frame.add(textArea);
        frame.add(panel,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
    
    
        new MeauDome2().init();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_42794826/article/details/110728268