图形用户界面设计-基本控件 java实验报告第四个

来源:互联网 发布:java 获取方法的泛型 编辑:程序博客网 时间:2024/04/30 08:48

第一个:

编写程序包含一个标签、一个文本框和一个按钮,当用户单击按钮时,程序把文本框中的内容复制到标签中。

我用SWT写的如下:

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class HelloWorldSwing {public static void main(String[] args) {JFrame fa = new JFrame("First");JLabel jl = new JLabel("Label区域");JTextField jt = new JTextField(12);fa.setSize(200, 200);fa.setLocation(400, 200);fa.setLayout(new FlowLayout());Button mybut = new Button("Button");fa.add(mybut);fa.add(jt);fa.add(jl);fa.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {super.windowClosing(e);System.exit(0);}});mybut.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {jl.setText(jt.getText());}});fa.setVisible(true);}}
第二个:

设计一个加法器,在文本框中输入两个整数,单击“=”按钮时,在第三个文本框中显示这两个数的和。

import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowStateListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class jiafa {public static void main(String[] args) {JFrame jf = new JFrame("加法");jf.setLayout(new FlowLayout());jf.setBounds(400, 150, 400,70);JTextField jt1 = new JTextField(5);JTextField jt2 = new JTextField(5);JTextField jt3 = new JTextField(5);JLabel jl = new JLabel("+    ");JButton jb = new JButton("  =  ");jf.add(jt1);jf.add(jl);jf.add(jt2);jf.add(jb);jf.add(jt3);jf.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {super.windowClosing(e);System.exit(0);}});jb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {int a,b,sum;a = Integer.parseInt(jt1.getText());b = Integer.parseInt(jt2.getText());sum = a+b;jt3.setText(String.valueOf(sum));}});jf.setVisible(true);}}




0 0
原创粉丝点击