JAVA实验七 图形用户界面的设计与实现

本文档详述了JAVA图形用户界面(GUI)的设计实验,包括创建带有交互按钮的标签,设计多组件窗口,实现按钮文字切换功能,文本输入与显示,以及简单的字体样式和颜色选择菜单。此外,还提供了计算器的基础实现,支持整数四则运算。
摘要由CSDN通过智能技术生成

一、实验目的
1.掌握图形界面设计。

2.掌握常用GUI控制组件及其事件处理。


二、实验内容

1.编程包含一个标签和一个按钮,单击按钮时,标签的内容在"你好"和"再见"之间切换。
2.设计一个窗口,窗口中包含有一个文本框,一个标签,两个按钮,当按下“文本一”按纽时,使得“Hello java!”显示在文本框中,按下“文本二”按纽时,使得“你好 Java!” 显示在文本框中(要求两个按纽之间使用网格布局,与其他组件共使用Frame默认布局管理)。

3. 试设计一个窗口,内含一个按钮。开始运行时,按钮显示“Click Me”字样,当按钮按下时,按钮显示为“Click Me Again”字样,再按一次,则按钮显示“Click Me”字样,依此循环。(1,2,3可以选作其一)

4.编程包含一个文本框和一个文本区域,在文本框中按回车键时,把文本框的内容写入文本区域。

5.试设计一个窗口,窗口界面如下图。包含Style菜单、Color菜单和Exit菜单,Style菜单设计字体的样式(包括Plane、Bold、Italic),Color(红、绿、蓝、自定义)菜单设计字体的颜色、Exit菜单(CloseWindow)退出系统。



代码实现:

1.

方法一:
package p;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class T {

	public static void main(String[] args) {
		
		JFrame f=new JFrame();
		FlowLayout flow =new FlowLayout();
		f.setLayout(flow);//设置布局
		
		JButton b=new JButton("exchange");
		final JLabel j=new JLabel("hello");
		b.addActionListener(new ActionListener(){//利用匿名类

			public void actionPerformed(ActionEvent e) {
				if(j.getText().equals("hello"))
					j.setText("bye");
				else j.setText("hello");
			}
			
		});
		
		f.add(b); //先添加哪个,哪个组件就会在前面
		f.add(j);
		
		f.setBounds(20,20,200,200);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

方法二:
package cn.nefu.edu.cn;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyWindow extends JFrame implements ActionListener{
	JButton bt;
	JLabel bq1;
	
	public MyWindow(){
		FlowLayout flow =new FlowLayout();
		this.setLayout(flow);
		bq1=new JLabel("bye");
		bt=new JButton("exchange");
		
		bt.addActionListener(this);//监听本身,自身实现了接口ActionListener
		
		this.add(bt);	
		this.add(bq1);
		validate();
		this.setBounds(20,20,200,200);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值