栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

GUI入门——小界面(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GUI入门——小界面(2)

问题描述:设置一个GUI小界面,实现加减法运算;

package dao;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//添加一个主函数,对窗体显示情况进行测试;
public class GUI_Test2 {
	public static void main(String[] args) {
		// 初始化窗体对象并调用display方法;
		new Algorithm_Method().display();
	}
}

@SuppressWarnings("serial")
//定义一个窗口类继承JFrame类
class Algorithm_Method extends JFrame {
	// 创建JButton,JTextField,JLabel,LPanel对象;
	JButton b1, b2;
	JTextField f1, f2, f3;
	JLabel l1;
	JPanel p1;

	// 使用该类的构造方法
	public Algorithm_Method() {
		// 分别实例化上述创建的对象;
		l1 = new JLabel("=");
		f1 = new JTextField(5);
		f2 = new JTextField(5);
		f3 = new JTextField(5);
		p1 = new JPanel();
		p1.setLayout(new GridLayout(2, 1));
		;
		b1 = new JButton("  +  ");
		b2 = new JButton("  -  ");
		// 创建Method_Monitor类对象并实例化
		Method_Monitor m = new Method_Monitor();
		b1.addActionListener(m); // 注册监听器
		b2.addActionListener(m); // 注册监听器
//注册监听器,使用匿名类  实现WindowsListener里的windowClosing方法;
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// 注册监听器
		f1.addKeyListener(m);
		// 注册动作监听器
		f1.addActionListener(m);
		// 注册监听器
		f2.addKeyListener(m);
		// 注册动作监听器
		f2.addActionListener(m);
	}

	// 定义一个方法;
	void display() {
		// 设置f1可编辑且从右输入
		f1.setEditable(true);
		f1.setHorizontalAlignment(JTextField.RIGHT);
		// 设置f2可编辑且从左输入
		f2.setEditable(true);
		f2.setHorizontalAlignment(JTextField.LEFT);
		// 设置f3不可编辑
		f3.setEditable(false);
		// 设置该窗体为流式布局
		this.setLayout(new FlowLayout());
		// 将组件分别加到对应的容器中
		p1.add(b1);
		p1.add(b2);
		this.add(f1);
		this.add(p1);
		this.add(f2);
		this.add(l1);
		this.add(f3);
		// 由系统自动设置适宜的窗体位置及大小
		this.pack();
		this.setLocationRelativeTo(null);
		// 设置窗体为可视
		this.setVisible(true);
	}

	// ActionListener,KeyListener实现接口
	class Method_Monitor implements ActionListener, KeyListener {
		// ActionListener实现接口中的actionPerformed匿名方法
		public void actionPerformed(ActionEvent e) {
			int i1 = 0, i2 = 0;
			int result = 0;
			// 将f1中输入的内容保存到s1中,如果s1中不为空,则将内容转为整形i1;
			String s1 = f1.getText();
			if (!s1.equals(""))
				i1 = Integer.parseInt(s1);
			// 将f2中输入的内容保存到s2中,如果s2中不为空,则将内容转为整形i2;
			String s2 = f2.getText();
			if (!s2.equals(""))
				i2 = Integer.parseInt(s2);
			// 创建Object对象o并实例化 ,将传入的参数赋给o;
			Object o = new Object();
			o = e.getSource();
			char c = '+';
//如果o是不是JButton类型,如果是,下转型为JButton对象 
			if (o instanceof JButton) {
				JButton bt = new JButton();
				bt = (JButton) o;
				// 获取JButton对象上内容
				String s = bt.getText();
				// 将其中内容的空格去掉,检索第一个字符
				c = s.trim().charAt(0);
			}
			// 如果点击的是'+'或者键盘上的enter键,则进行加操作
			if (c == '+') {
				result = i1 + i2;
				// 将f3内容填充为输入两者之和
				f3.setText(result + "");
			}
			// 如果点击的是'—',则进行减操作
			else if (c == '-') {
				result = i1 - i2;
				// 将f3内容填充为输入两者之和
				f3.setText(result + "");
			}
		}

		// 实现KeyListener接口的相关方法
		public void keyTyped(KeyEvent e) {
			char c = e.getKeyChar();
			// 将判断是否输入的是否为非数字,若是,自动清除
			if (!Character.isDigit(c))
				e.consume();
		}

		public void keyPressed(KeyEvent e) {
		}

		public void keyReleased(KeyEvent e) {
		}
	}
}

运行界面:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号