实验十一:图形界面二
Posted baiyunchao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验十一:图形界面二相关的知识,希望对你有一定的参考价值。
实验程序:
package shiyan;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Jisuanqi extends Frame implements ActionListener
public Jisuanqi()
super("计算器");
Frame ff=new Frame("framework test");
ff.setSize(400,100);
ff.setLocation(300,240);
ff.setLayout(new FlowLayout());
final TextField f1=new TextField("10",8);
ff.add(f1);
Label l1=new Label("+");
ff.add(l1);
TextField f2=new TextField("10",8);
ff.add(f2);
Button b1=new Button("=");
ff.add(b1);
TextField f3=new TextField(10);
ff.add(f3);
ff.addWindowListener(new myclose());
ff.setVisible(true);
b1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
double c;
String s1=f1.getText();
double a=Integer.parseInt(s1);
String s2=f2.getText();
double b=Integer.parseInt(s2);
c=a+b;
String m=String.valueOf(c);
f3.setText(m);
private double Integer(String s)
return 0;
);
class myclose implements WindowListener
public void windowActivated(WindowEvent arg0)
public void windowClosed(WindowEvent arg0)
public void windowClosing(WindowEvent arg0)
System.exit(0);
public void windowDeactivated(WindowEvent arg0)
public void windowDeiconified(WindowEvent arg0)
// TODO Auto-generated method stub
public void windowIconified(WindowEvent arg0)
public void windowOpened(WindowEvent arg0)
public static void main(String[] args)
new Jisuanqi();
public void actionPerformed(ActionEvent arg0)
二、结果
10+10=20
三、心得
1、创建按钮以后,要用add把它添加到窗口上
2、得到一个输入框中的内容用gettext
3、要先把输入框中的内容转换成字符串类型
以上是关于实验十一:图形界面二的主要内容,如果未能解决你的问题,请参考以下文章
201671010110 2016 2017 2《java程序设计》