GUI

GUI

基本概念

GUI:全称图形用户界面,指用用图形方式显示的计算机操作用户界面

​ 与命令行界面相比,图形界面对于用户来说在视觉上更易于接受。但是比简单的消息呈现花上更多的计算能力。

​ 在java1.0刚刚出现的时候。就包含了一个用于基本Gui程序设计的类库,sun将他称为抽象窗口工具箱。

​ 基本AWT库采用将处理用户界面元素的任务委派给每个目标平台(Window、、Macintosh等)的本地GUI工具箱的方式,由本地GUI工具箱负责用户界面元素的创建和动作。在不同的平台上,操作行为存在着一些微妙的差别。

​ 在1996年,Netscape创建了一种称为IFC的GUI库,它采用了与AWT完全不同的工作方式。它将按钮、菜单这样的用户界面元素绘制在空白窗口上,面对等体只需要创建和绘制窗口。

​ 因此,Netscape的IFC逐渐在程序运行的所有平台上的外观和动作都一样。Sun与Netscape合作完善了这种方式,创建了一个名为Swing的用户界面库。Swing可作为Java 1.1的扩展部分使用,现已成为Java SE 1.2标准库的一部分。

注意:事实上,因为不美观,并且使用awt和string编写的程序运行需要jre的原因。这项技术已经很少使用了。因此我们学习时应该注意其中的算法逻辑,而不是这项技术本身。

代码示例

获得顶层框架与设置框架

public class MyTest {
    public static void main(String[] args) {
        //awt的基本操作
        Frame frame = new Frame("我第一个图形界面化编程");
        frame.setVisible(true);
        frame.setSize(160,150);
        frame.setLocation(200,200);
        frame.setResizable(false);
        frame.setBackground(Color.GREEN);
    }
}

注意:点击“x”并不能关闭程序,这是因为我们没有添加事件监听器的原因

public class MyTest3 {
    public static void main(String[] args) {
        //事件监听
        Frame frame = new Frame();
        Panel panel = new Panel();


        frame.setLayout(null);

        frame.setBackground(Color.BLACK);
        panel.setBackground(Color.GREEN);

        frame.setBounds(100,100,500,300);
        panel.setBounds(100,75,300,150);

        frame.add(panel);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

此时即可正常关闭

可以添加panel在frame框架里,下来演示创建并布局

public class MyTest6 {
    public static void main(String[] args) {
        //设置总体
        Frame frame = new Frame("MyTest");
        frame.setBounds(50, 50, 700, 500);
        frame.setBackground(Color.BLUE);
        frame.setLayout(new GridLayout(2, 1));
        frame.setResizable(false);
        frame.setVisible(true);
        //设置上半部分
        Panel panel = new Panel();
        panel.setLayout(new BorderLayout());
        //上一
        panel.add(new Button("button"), BorderLayout.WEST);
        //上二
        Panel panel1 = new Panel();
        panel1.setLayout(new GridLayout(2, 1));
        panel1.add(new Button("button"));
        panel1.add(new Button("button"));
        panel.add(panel1,BorderLayout.CENTER);
        //上三
        panel.add(new Button("button"),BorderLayout.EAST);
        frame.add(panel,BorderLayout.NORTH);
        //下
        Panel panel2 = new Panel();
        panel2.setLayout(new BorderLayout());
        //下1
        panel2.add(new Button("button"),BorderLayout.WEST);
        //下2
        Panel panel3 = new Panel();
        panel3.setLayout(new GridLayout(2,2));
        for (int i = 0; i < 4; i++) {
            panel3.add(new Button("button"));
        }
        panel2.add(panel3,BorderLayout.CENTER);
        panel2.add(new Button("button"),BorderLayout.EAST);
        frame.add(panel2);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

结果为下图,常用的布局方式有三种,分别是

流式布局 Flow

东西南北中 Border

表格布局 Grid

在此例中只使用了 Border和Grid。

我们可以增加事件监听器,当触发时,做一些我们想要的操作

下面是代码示例

public class MyTest7 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("aaa");

        frame.add(button);

        frame.setVisible(true);
        //设置事件监听器

        button.addActionListener(new MyActionListener());
        windowsclose(frame);



    }
    public static void windowsclose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("欢迎来到德莱联盟");
    }
}

也可以两个button共用一个事件触发器

public class MyTest8 {
    //两个button共用一个触发器
    public static void main(String[] args) {
        Frame frame = new Frame("开始-结束");
       Button button = new Button("start");
        button.setActionCommand("everything is start");
        Button button1 = new Button("end");
        button1.setActionCommand("nothing for everything");

        frame.add(button,BorderLayout.EAST);
        frame.add(button1,BorderLayout.WEST);

        button.addActionListener(new MyActionListene());
        button1.addActionListener(new MyActionListene());
        frame.setVisible(true);
    }
}
class MyActionListene implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {

        //该方法可以获得button的返回信息,如果不设置,返回默认信息
        System.out.println(e.getActionCommand());
        if (e.getActionCommand().equals("nothing for everything")){
            System.exit(0);
        }

    }
}
 button.setActionCommand("everything is start");
 //该方法可以设置button的返回信息

e.getActionCommand()
//该方法可以返回button设置的信息
//如果不设置,返回默认信息

下面演示获得文本框,并监听其中输入的信息

public class MyTest9 {
//文本框监听
    public static void main(String[] args) {
        //创建框架

        Frame frame = new Frame("xwrite");
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBounds(100,100,600,600);

        //添加文本框
        TextField field = new TextField();
        frame.add(field,BorderLayout.CENTER);

          //添加监听器,当在文本中输入空格时触发触发器

        field.addActionListener(new myfile());
        field.setEchoChar('x');

    }
}
class myfile implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {

        TextField textField=(TextField)e.getSource();
        //.gettext,获得文本框的内容
        String text = textField.getText();
        //打印文本框的内容
        System.out.println(text);
        //设置文本框中的内容
        textField.setText("");
    }
}
e.getSource()
//该方法返回调用他的组件
//本例中返回的是文本框

下面演示用gui做一个简陋的计算器

public class MyTest2 {
    public static void main(String[] args) {
        new calculate();
    }
}
class calculate {
    TextField num1, num2, num3;
    public  calculate() {
        //一个框架
        Frame frame = new Frame("calculate");
        frame.setBounds(50, 50, 500, 100);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setBackground(Color.BLUE);
        frame.setLayout(new FlowLayout());
        //三个文本组件
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //一个标签
        Label label = new Label("+");
        //一个按钮
        Button button = new Button("=");
        //拼接
        frame.add(num1);
        frame.add(label);
        frame.add(num2);
        frame.add(button);
        frame.add(num3);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        button.addActionListener(new actionlistener());
    }
    private class actionlistener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //gettext,返回string
            int i1 = Integer.parseInt(num1.getText());
            int i2 = Integer.parseInt(num1.getText());
            num3.setText(""+(i1+i2));
            num1.setText("");
            num2.setText("");
        }
    }
}

来演示重写获得画笔工具

public class MyPaint {
    public static void main(String[] args) {
        new paint().loadFrame();
    }
}
class paint extends Frame {
    public void loadFrame() {
        setBounds(200, 200, 600, 500);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        //重写paint方法获得画笔g
        g.drawOval(100, 100, 100, 100);
        g.fillOval(100, 100, 100, 100);
        g.fillRect(150, 200, 200, 200);
    }
}

下来演示用鼠标监听与画笔工具模拟画图过程

public class MyPaintTest {
    //在窗口跟随鼠标画点
    public static void main(String[] args) {
new MyFrame("paint");
    }
}
class MyFrame extends Frame {
    ArrayList points ;
    public MyFrame (String title){
        //获得面板
        super(title);
        setResizable(false);
        setBounds(200,200,600,400);
        setVisible(true);
        //获得监听
        addMouseListener(new MyMouseLisener());
        //获得集合
        points = new ArrayList();
    }

    private class MyMouseLisener extends MouseAdapter {
        //设置监听
        @Override
        public void mousePressed(MouseEvent e) {
             MyFrame myframe= (MyFrame)e.getSource();
             //将监听到的点,赋给集合
            myframe.addpoint(new Point(e.getX(),e.getY()));
            myframe.repaint();
        }
    }
    //定义函数,将点赋给集合
    public void addpoint(Point point){
        points.add(point);
    }
    //开始画
    @Override
    public void paint(Graphics g) {
        Iterator iterator = points.iterator();
        while ( iterator.hasNext()){
            Point point= (Point)iterator.next();
            g.setColor(Color.magenta);
            g.fillOval(point.x,point.y,10,10);
        }
    }
}

下来演示键盘监听

public class MyTest {
    public static void main(String[] args) {
new MyFrame("键盘监听");
    }
}
class MyFrame extends Frame{
    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 100, 500);
        setResizable(false);
        setVisible(true);
        setBackground(Color.GRAY);


        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode==KeyEvent.VK_H){
                    System.out.println("你敲了H键");
                }
            }
        });
    }

}

下来演示弹窗

public class MyTest2 extends JFrame {

    public void init() {
        setVisible(true);
        setResizable(false);
        setBounds(100, 100, 500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);

        JButton button = new JButton("点击就送");
        button.setBounds(30, 30, 200, 50);

        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });
        container.add(button);
    }

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

class MyDialog extends JDialog {
    public MyDialog() {
        setVisible(true);
        this.setBounds(100, 100, 300, 300);
        Container container = this.getContentPane();

        container.add(new JLabel("jiojio"),BorderLayout.CENTER);
    }
}

ionEvent e) {
new MyDialog();
}
});
container.add(button);
}

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

}

class MyDialog extends JDialog {
public MyDialog() {
setVisible(true);
this.setBounds(100, 100, 300, 300);
Container container = this.getContentPane();

    container.add(new JLabel("jiojio"),BorderLayout.CENTER);
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值