Java Experiment 8: Graphical Interface

  • Experiment name

graphic interface 

  • Experimental purposes and requirements

Familiar with the layout and event handling mechanism of the graphical interface; draw graphics in the Swing interface

  • lab environment

Multimedia microcomputer; Windows, jdk and Eclipse.

  • experimental design
  1. Experiment content
  • Write an English word display program for numbers. Arrange a text box, a button and a label in the form. Enter a number (0~9) from the text box and click the button to display the corresponding English word (such as: zero, one, two etc.) are displayed in the label.
  1. Experimental steps
  1. Program 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();

}

}

Guess you like

Origin blog.csdn.net/MYSELFWJC/article/details/131803336