java上机实验学习笔记————实验七 图形用户界面设计

java上机实验学习笔记————实验七 图形用户界面设计

题一: 计算器程序雏形

该窗口模拟Windows的计算器功能,添加一个文本行和4个按钮,单击【1】、【2】、【+】按钮时,将按钮的标签添加到文本行中;单击【C】按钮时,清空文本行中的内容;单击窗口的关闭按钮,将关闭该窗口。

MyCalculator.java: 

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class MyCalculator extends JFrame {
	
	private JTextField xianshi;
	private JButton button_1,button_2,button_add,button_c;
	
	public MyCalculator() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Calculator");
		getContentPane().setLayout(null);
		
		//设置文本框
		this.xianshi = new JTextField();
		this.xianshi.setBounds(10, 10, 416, 30);
		this.add(xianshi);
		
		//设置按钮“1”
		this.button_1 = new JButton("1");
		this.button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				xianshi.setText(xianshi.getText()+"1");
			}
		});//匿名内部类可以帮助提高编程速度
		this.button_1.setBounds(10, 50, 45, 45);
		this.add(button_1);
		
		//设置按钮“2”
		this.button_2 = new JButton("2");
		this.button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				xianshi.setText(xianshi.getText()+"2");
			}
		});
		this.button_2.setBounds(65, 50, 45, 45);
		this.add(button_2);
		
		//设置按钮“+”
		this.button_add = new JButton("+");
		this.button_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				xianshi.setText(xianshi.getText()+"+");
			}
		});
		this.button_add.setBounds(120, 50, 45, 45);
		this.add(button_add);
		
		
		//设置按钮“清空”
		this.button_c = new JButton("c");
		this.button_c.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				xianshi.setText("");
			}
		});
		this.button_c.setBounds(175, 50, 45, 45);
		this.add(button_c);
	}
	

}

Test.java:

import java.awt.Dimension;

public class Test {

	public static void main(String[] args) {
		MyCalculator onee= new MyCalculator();
		onee.setSize(new Dimension(500,350));
		onee.setVisible(true);

	}

}

题二:

通过继承JFrame来设计窗口,如下图所示,要求:窗体名称为“MyFrame”,并且有2个按钮,一个显示“show”,另一个显示“close”。当用户点击“show”按钮时,弹出右边对话框,当用户点击“close”按钮时,窗体关闭,系统退出。

        

MyFrame.java:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class MyFrame extends JFrame {
	private JButton button_show,button_close;
	public MyFrame() {
		this.setTitle("MyFrame");
		this.setLayout(new FlowLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.button_show = new JButton("show");
		this.add(button_show);
		this.button_show.addActionListener(new MyActionListener());
		
		this.button_close = new JButton("close");
		this.add(button_close);
		this.button_close.addActionListener(new MyActionListener());
		
	}
	class MyActionListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			if(event.getSource()==button_show)
				JOptionPane.showMessageDialog(null, "这是一个例子!");
			else if(event.getSource()==button_close)
				System.exit(EXIT_ON_CLOSE);
		}
	}

	public static void main(String[] args) {
		MyFrame twee=new MyFrame();
		twee.setSize(new Dimension(300,150));
		twee.setVisible(true);

	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值