Java实验八:图形界面

  • 实验名称

图形界面 

  • 实验目的及要求

熟悉图形界面的布局与事件处理机制;在Swing界面中绘制图形

  • 实验环境

多媒体微型计算机 ; Windows ,jdk及Eclipse。

  • 实验设计
  1. 实验内容
  • 编写数字的英文单词显示程序,窗体中安排一个文本框、一个按钮和一个标签,从文本框输入一个数字(0~9),点击按钮将其对应的英文单词(如:zero、one、two等)显示在标签中。
  1. 实验步骤
  1. 程序1
package shiyan8;



import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class shiyan8_1{

int i=0;

TextField  tf=new TextField();

JLabel labOne=new JLabel("标签");

JButton bn1=new JButton("点击");

JPanel p=new JPanel();

public eight(){

p.add(tf);

p.add(labOne);

p.add(bn1);

bn1.addActionListener(new MyListener());



JFrame f=new JFrame();

f.setSize(300,300);

Container contentPane=f.getContentPane();

contentPane.add(p);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public class MyListener implements ActionListener{



public void actionPerformed(ActionEvent e)

{

 int x1=Integer.parseInt(tf.getText());

 if ( x1==0)

 labOne.setText("zero");

 else if(x1==1)

 labOne.setText("one");  

 else if(x1==2)

 labOne.setText("two");

 else if(x1==3)

 labOne.setText("three");

 else if(x1==4)

 labOne.setText("four");

else if(x1==5)

labOne.setText("five");

else if(x1==6)

labOne.setText("six");

else if(x1==7)

labOne.setText("seleve");

else if(x1==8)

labOne.setText("eight");

else if(x1==9)

labOne.setText("nine");

else

labOne.setText("fault");

}

}

public static void main(String[] args){

new shiyan8_1();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GP2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值