Java语言程序设计----- 图形用户界面设计

Java语言程序设计----- 图形用户界面设计

1.实验目的

(1)掌握Java Swing组件的使用方法。
(2)理解委托事件处理模型。
(3)设计具有图形用户界面的、能够响应事件的Java应用程序。
2.实验内容
(1)计算器程序雏形
该窗口模拟Windows的计算器功能,添加一个文本行和4个按钮,单击【1】、【2】、【+】按钮时,将按钮的标签添加到文本行中;单击【C】按钮时,清空文本行中的内容;单击窗口的关闭按钮,将关闭该窗口。程序运行窗口如下图所示。

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

1.import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;


public class Calculator extends JFrame implements CaretListener,ActionListener{

	 JTextField text=new JTextField("",30);
	 JButton button1=new JButton("1");

	 
	 JButton button2=new JButton("2");

	 JButton button3=new JButton("+");

	 JButton button4=new JButton("c");

	
	public Calculator() {
		 super("Calculator");
		 this.setBounds(300, 300, 300, 200);
		 this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
		 this.getContentPane().add(text);
		 this.getContentPane().add(button1);
		 this.getContentPane().add(button2);
		 this.getContentPane().add(button3);
		 this.getContentPane().add(button4);
		 button3.addActionListener(this);
		 button2.addActionListener(this);
		 button1.addActionListener(this);		
		 button4.addActionListener(this);
		 this.setVisible(true);
		 

	
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==button1)
		{
			text.setText(text.getText()+"1");
		}
		if(e.getSource()==button2)
		{
			text.setText(text.getText()+"2");
		}
		if(e.getSource()==button3)
		{
			text.setText(text.getText()+"+");
		}
		if(e.getSource()==button4)
		{
			text.setText("");
		}
		
	}
	public void caretUpdate(CaretEvent e) {
		// TODO Auto-generated method stub
		
	}
	public static void main(String []args) {
		new Calculator();
	}
	
	
}

在这里插入图片描述

2.
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
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{
	mywindow(){
		JFrame f=new JFrame("myFrame");
		f.setLayout(new FlowLayout());
		f.setVisible(true);
		f.setSize(300,200);
		JButton bt1=new JButton("show");
		bt1.addActionListener(new other());

		JButton bt2=new JButton("closing");
		bt2.addActionListener(new wclose());
		f.add(bt1);
		f.add(bt2);
		
	}
	public static void main(String []args) {
		mywindow cs=new mywindow();
}
}
class wclose implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		System.exit(0);
		
	}
	
	
}
class other implements ActionListener{

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame("消息");	
		frame.setSize(300,200);
		//frame.setLayout(new GridLayout());
		frame.setLayout(new FlowLayout());
	JLabel L=new JLabel("这是一个好例子!");
		frame.add(L);
		JButton bt8 =new JButton("确定");
		frame.getContentPane().add(bt8);
		bt8.addActionListener(new wclose());
		frame.setVisible(true);
	}
	
	
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值