awt&swing 學習筆記(2)

小小雨傘發表於2020-12-05

對話方塊
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();
    }

}

相關文章