62.【GUI编程】(二)

简介: 62.【GUI编程】

6.TextField and TextArea(文本文框)

文本文档以及事件操作:
基本思想: 
标签 :Label=new Label();
文本文框: TextField =new TextField();
按钮:Button =new Button();
对文本问框进行加密: textField_user.setEchoChar('*');
获得文本框中的的内容:TextField textField=(TextField) (e.getSource());

JavaBean类

package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Student extends Frame implements ActionListener {
    //按钮
    Button button_login=new Button("登入");
    Button button_cancel=new Button("退出");
    //文本文框
    TextField textField_user=new TextField();
    TextField textField_password=new TextField();
        public Student(){
            super.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    if(JOptionPane.showConfirmDialog(null,"您确定要退出此界面?")==0){
                        System.exit(0);
                    }
                }
            });
            this.setSize(300,300);
            this.setBackground(Color.BLUE);
            this.setLocationRelativeTo(null);
            //标签
            Label label_user=new Label("账号");
            Label label_password=new Label("密码");
            //第一次整合
            Panel panel_one=new Panel();
            panel_one.setLayout(new GridLayout(2,2));
            panel_one.add(label_user);
            panel_one.add(textField_user);
            panel_one.add(label_password);
            panel_one.add(textField_password);
            //进行第二次整合
            Panel panel_two=new Panel();
            panel_two.setLayout(new FlowLayout());
            panel_two.add(button_login);
            panel_two.add(button_cancel);
            //进行画板的整合
            this.setLayout(new BorderLayout());
            this.add(panel_one,BorderLayout.CENTER);
            this.add(panel_two,BorderLayout.SOUTH);
            //设置替换编码
            textField_user.setEchoChar('*');
            //进行监听
            getTextField_user().addActionListener(this);
        }
    @Override
    public void actionPerformed(ActionEvent e) {
            //
           TextField textField=(TextField) (e.getSource());
        System.out.println(textField.getText());
        }
    public Button getButton_login() {
        return button_login;
    }
    public Button getButton_cancel() {
        return button_cancel;
    }
    public TextField getTextField_user() {
        return textField_user;
    }
    public TextField getTextField_password() {
        return textField_password;
    }
}

Test类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

7.calc 计算器的操作

基本思路:
字符串转整形:   intger.parseint(字符串A)  把字符串转换成数字
对文本框的内容进行清空的操作 :  textField.setText("");

JavaBean类

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student extends Frame {
    //3个文本框
    TextField textField_one=new TextField(10);  //文本框最多储存的长度
    TextField textField_two=new TextField(10);
    TextField textField_three=new TextField(10);
    //1个按钮
    Button button=new Button("=");
    public Student(){
     this.setLocationRelativeTo(null);
     //一个标签
     Label label=new Label("+");
     设置监听,文本文框的内容设置为构造器,便于提取
     button.addActionListener(new MyImplement(textField_one,textField_two,textField_three));
     //进行窗体布局
     this.setLayout(new FlowLayout());
     this.add(textField_one);
     this.add(label);
     this.add(textField_two);
     this.add(button);
     this.add(textField_three);
     pack();
    }
    public TextField getTextField_one() {
        return textField_one;
    }
    public TextField getTextField_two() {
        return textField_two;
    }
    public TextField getTextField_three() {
        return textField_three;
    }
    public Button getButton() {
        return button;
    }
}

接口类:

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyImplement implements ActionListener {
    private TextField num1,num2,num3;
    构造器进行获取数据
    public MyImplement(TextField num1,TextField num2,TextField num3){
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    //获得加数
       int a=Integer.parseInt(num1.getText());
       int b=Integer.parseInt(num2.getText());
        //求和
        num3.setText(""+(a+b));
        //清屏
        num1.setText("");
        num2.setText("");
    }
}

测试类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

8.calc 计算机的操作(组合类)

基本思路 : 
清空的操作: setText()
字符串转阿拉布数字 Integr.parseint(字符串)

JavaBean类

package KuangStudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student extends Frame implements ActionListener {
    //3个文本框
    TextField textField_one=new TextField(10);  //文本框最多储存的长度
    TextField textField_two=new TextField(10);
    TextField textField_three=new TextField(10);
    //1个按钮
    Button button=new Button("=");
    public Student(){
     this.setLocationRelativeTo(null);
     //一个标签
     Label label=new Label("+");
     //进行窗体布局
     this.setLayout(new FlowLayout());
     this.add(textField_one);
     this.add(label);
     this.add(textField_two);
     this.add(button);
     this.add(textField_three);
     pack();
     //进行设置监听:
        getButton().addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       int a=Integer.parseInt(textField_one.getText());
       int b=Integer.parseInt(textField_two.getText());
       textField_three.setText(""+(a+b));
       textField_one.setText("");
       textField_two.setText("");
    }
    public TextField getTextField_one() {
        return textField_one;
    }
    public TextField getTextField_two() {
        return textField_two;
    }
    public TextField getTextField_three() {
        return textField_three;
    }
    public Button getButton() {
        return button;
    }
}

Test类:

package KuangStudy;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
    student.setVisible(true);
}
}

9.Frame下的Paint

基本思想:
1.对paint()的覆盖
2.画板添加到框体中去
package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
        Frame frame=new Frame("傻子");
        frame.setSize(700,500);
        Student student=new Student();
        frame.setBackground(Color.CYAN);
        frame.add(student);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
}
}
package KuangStudy;
import java.awt.*;
public class Student extends Panel {
    public void paint(Graphics g){
        g.fillOval(200,200,100,100);
        Color c=new Color(255,5,5);
        g.setColor(c);
    }
}

相关文章
|
容器
|
数据安全/隐私保护
|
27天前
|
数据可视化 搜索推荐
GUI图形用户界面
【10月更文挑战第8天】
|
6月前
|
UED C++ Python
GUI开发入门指南
GUI开发入门指南
|
6月前
|
Python 容器
Python与GUI编程:创建图形用户界面
Python的Tkinter库是用于构建GUI应用的内置工具,无需额外安装。它提供了丰富的控件,如按钮、文本框等,让用户通过图形界面与程序交互。创建GUI窗口的基本步骤包括:导入Tkinter库,创建窗口对象,设置窗口属性,添加控件(如标签和按钮),并使用布局管理器(如`pack()`或`grid()`)来组织控件的位置。此外,可以通过绑定事件处理函数来响应用户操作,例如点击按钮。Tkinter还有更多高级功能,适合开发复杂GUI应用。
|
6月前
|
开发框架 程序员 开发者
Python GUI编程:从入门到精通3.2 GUI编程:学习使用Tkinter、PyQt或wxPython等库创建图形用户界面。
Python GUI编程:从入门到精通3.2 GUI编程:学习使用Tkinter、PyQt或wxPython等库创建图形用户界面。
|
6月前
|
API 开发工具 C++
Python图形用户界面(GUI)编程:大解密
Python图形用户界面(GUI)编程:大解密
369 0
|
Python 容器
tkinter GUI编程(学习笔记-1 窗口组件与布局)
tkinter GUI编程(学习笔记-1 窗口组件与布局)
131 0