图形用户界面的简单展示

 以上是用户界面展示。

  代码实现如下:

//主函数main

        public class Main {
            public static void main(String[] args) {
                // TODO code application logic here
                MyFrame myFrame=new MyFrame();
                myFrame.setVisible(true);
            }

        }



//MyFrame函数

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class MyFrame extends JFrame  //implements ActionListener
{
    private JButton button1;
    private JButton button2;
    private JTextField textField1;
    private JButton button3;
    private JTextField textField2;

    public MyFrame() {
        button1 = new JButton("这是一个按钮");
        button2 = new JButton("这也是一个按钮");
        button3 = new JButton("输出信息显示在右边的文本框中");
        textField1 = new JTextField("神秘");
        textField2 = new JTextField("神奇的事情要发生了");
        this.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 40));
        this.add(button1);
        this.add(button2);
        this.add(textField1);
        this.add(button3);
        this.add(textField2);


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                button1ActionPerformed();
            }
        });


        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {button2ActionPerformed();}
        });


        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                button3ActionPerformed();
            }
        });

        button1.setFont(new Font("宋体", 23, 46));
        button2.setFont(new Font("宋体", 23, 46));
        button3.setFont(new Font("宋体", 23, 46));
        textField1.setFont(new Font("宋体", 23, 46));
        textField1.setColumns(40);
        textField2.setFont(new Font("宋体", 23, 46));
        textField2.setColumns(20);


        this.addWindowListener(new WindowAdapter() {                   //窗口事件
            @Override
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }

        });
        this.addMouseListener(new MouseAdapter() {     //鼠标事件
            @Override
            public void mouseExited(MouseEvent me) {
                textField1.setText("不要乱晃鼠标,请点击按钮。");
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                textField1.setText("鼠标已移入窗体。");
            }

        });

        this.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent me) {
                textField1.setText("鼠标拖动的坐标:x:" + me.getX() + "y:" + me.getY());
            }

            @Override
            public void mouseMoved(MouseEvent me) {
                textField1.setText("鼠标移动的坐标:x:" + me.getX() + "y:" + me.getY());
            }
        });

        this.setBounds(400, 400, 1000, 600);//设置窗体的长宽各为:400 400让其显示在距屏幕左上方坐标(1000,600)处

                                                            //它们的计量单位是像素,这句话等效于:this.setLocation(400, 400)
                                                                                        // this.setSize(1000, 600);


        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

   /* @Override                                     //两个按钮按下输出不同内容的方法
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()==button1)
        {
            textField1.setText("Hello world!");
        }
        if(ae.getSource()==button2)
        {
            textField1.setText("Hello everyone!");
        }
    }*/
    
    /*class Button1Action implements ActionListener     //两个按钮按下输出不同内容的方法
    {

        @Override
        public void actionPerformed(ActionEvent ae) {
            textField1.setText("Hello world;");
        }
        
    }*/

    /*class Button2Action implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae) {
            textField1.setText("Hello everyone!");
        }
        
    }*/
    private void button1ActionPerformed() {
        textField1.setText("都说是按钮还按!");
    }

    private void button2ActionPerformed() {
        textField1.setText("老弟,我俩按钮是双胞胎啊!");
    }

    private void button3ActionPerformed() {
        //1.MessageDialog  消息对话框
        //JOptionPane.showMessageDialog(this, "这是一个对话框");

        //2.ConfirmDialog  确认对话框
         /*int confirmMesg=JOptionPane.showConfirmDialog(this, "是否同意以上协议?");  
         switch(confirmMesg)
         {
             case JOptionPane.CANCEL_OPTION:textField2.setText("取消");
             break;
             case JOptionPane.YES_OPTION:textField2.setText("是");
             break;
             case JOptionPane.NO_OPTION:textField2.setText("否");
             break;
         }*/
        //3.InputDialog  输入对话框
        String inputMesg = JOptionPane.showInputDialog(this, "请输入信息:");
        textField2.setText(inputMesg);

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值