您好,欢迎来到小侦探旅游网。
搜索
您的当前位置:首页图形用户界面实验报告

图形用户界面实验报告

来源:小侦探旅游网
实验五 SWT图形用户界面

1.实验目的

(1)了解SWT 程序开发步骤。 (2)掌握SWT常用组件的使用。 (3)掌握SWT 的事件处理。

2.实验内容

实验题1完成图3.8 所示图形界面的制作。要求“查询结果”用group组件。

图3.8 数据查询界面

实验代码:(在cn.edu包中) package cn.edu;

public class Example {

public static void main(String[] args) { Window win =new Window(); win.setTitle(\"按学号查询\"); } }

package cn.edu;

import javax.swing.*;

public class Window extends JFrame {

JTextField textnum1,textnum2,textname,textage; JLabel p1,p2,p3,p4; JButton enter,reverse; public Window(){ init();

setBounds(400,150,500,500); setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false);

}

void init(){

setLayout(null);

p1=new JLabel(\"请输入学号:\"); p2=new JLabel(\"学号:\"); p3=new JLabel(\"姓名:\"); p4=new JLabel(\"年龄:\"); add(p1); add(p2); add(p3); add(p4);

p1.setBounds(50,50,100,100); p2.setBounds(50,180,100,100); p3.setBounds(50,230,100,100); p4.setBounds(50,280,100,100); textnum1 =new JTextField(10); textnum2=new JTextField(10); textname =new JTextField(10); textage =new JTextField(10); add(textnum1); add(textnum2); add(textname); add(textage);

textnum1.setBounds(150,90,200,20); textnum2.setBounds(200,220,150,20); textname.setBounds(200,270,150,20); textage.setBounds(200,320,150,20); enter =new JButton(\"确定\"); reverse = new JButton(\"重置\"); add(enter); add(reverse);

enter.setBounds(90,130,70,30); reverse.setBounds(300,130,70,30); } }

运行结果如下如所示:

实验题2 完成图3.9所示学籍管理主界面的设计与制作。

图3.9学籍管理主界面

实验代码:(在cn.edu包中) package cn.edu;

public class Example {

public static void main(String[] args) {

Window win=new Window(\"学籍管理系统\ } }

package cn.edu; import java.awt.*; import javax.swing.*;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent; import static javax.swing.JFrame.*; import java.awt.event.*; import javax.swing.*;

public class Window extends JFrame { JMenuBar menubar;

JMenu menu, menu2, menu3, menuhelp; JMenuItem item1, item2; JLabel text;

public Window() { }

public Window(String s, int x, int y, int w, int h) { init(s);

setLocation(x, y); setSize(w, h); setVisible(true);

setDefaultCloseOperation(DISPOSE_ON_CLOSE); }

void init(String s) { setTitle(s); setLayout(null);

text = new JLabel(\"欢迎使用学籍管理系统 !\"); text.setForeground(Color.red);

text.setFont(new java.awt.Font(\"Dialog\ add(text);

text.setBounds(140, 150, 250, 150); menubar = new JMenuBar(); menu = new JMenu(\"在线登陆\"); menu2 = new JMenu(\"用户管理\"); menu3 = new JMenu(\"数据维护\"); menuhelp = new JMenu(\"帮助\");

item1 = new JMenuItem(\"用户登录\

item2 = new JMenuItem(\"退 出\ menu.add(item1); menu.add(item2); menubar.add(menu); menubar.add(menu2); menubar.add(menu3); menubar.add(menuhelp); setJMenuBar(menubar); } }

运行结果如下如所示:

实验题3 在图3.9所示界面中,在“用户登录”菜单中添加组件选择事件,当选中“用户登录”时,打开图4.0所示用户登录界面。

图4.0 用户登录窗体

实验代码:(在cn.edu包中) package cn.edu;

public class Example {

public static void main(String[] args) {

Window win=new Window(\"学籍管理系统\ } }

package cn.edu; import java.awt.*; import javax.swing.*;

import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import static javax.swing.JFrame.*; import java.awt.event.*; import javax.swing.*;

public class Window extends JFrame implements ActionListener { JMenuBar menubar;

JMenu menu, menu2, menu3, menuhelp; JMenuItem item1, item2; JLabel text; MyDialog dialog; public Window() { }

public Window(String s, int x, int y, int w, int h) { init(s);

setLocation(x, y); setSize(w, h); setVisible(true);

setDefaultCloseOperation(DISPOSE_ON_CLOSE); }

void init(String s) { setTitle(s); setLayout(null);

text = new JLabel(\"欢迎使用学籍管理系统 !\"); text.setForeground(Color.red);

text.setFont(new java.awt.Font(\"Dialog\ add(text);

text.setBounds(140, 150, 250, 150); menubar = new JMenuBar(); menu = new JMenu(\"在线登陆\"); menu2 = new JMenu(\"用户管理\"); menu3 = new JMenu(\"数据维护\"); menuhelp = new JMenu(\"帮助\");

item1 = new JMenuItem(\"用户登录\ item1.addActionListener(this);

dialog = new MyDialog(this, \"SWT应用程序\

item2 = new JMenuItem(\"退 出\ menu.add(item1); menu.add(item2); menubar.add(menu); menubar.add(menu2); menubar.add(menu3); menubar.add(menuhelp); setJMenuBar(menubar);

}

public void actionPerformed(ActionEvent e) { if (e.getSource() == item1) {

int x = this.getBounds().x + this.getBounds().width; int y = this.getBounds().y; dialog.setLocation(x, y); dialog.setVisible(true);} } }

package cn.edu; import java.awt.*;

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

public class MyDialog extends JDialog implements ActionListener{ JLabel a1,a2;

JTextField textname,textnum; JButton p1,p2;

MyDialog(JFrame f, String s, boolean b){ super(f, s, b);

a1=new JLabel(\"请输入用户名:\"); a2=new JLabel(\"请输入密码:\"); textname=new JTextField(10); textnum=new JTextField(10); add(a1); add(a2);

add(textname); add(textnum);

a1.setBounds(100,10,220,220); a2.setBounds(100,120,220,220);

textname.setBounds(200,110,120,20); textnum.setBounds(200,220,120,20); p1=new JButton(\"提交\"); p2=new JButton(\"重置\"); p1.addActionListener(this); p2.addActionListener(this); setLayout(null); add(p1); add(p2);

p1.setBounds(100,350,70,30); p2.setBounds(300,350,70,30); setBounds(300,200,500,500);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) { setVisible(false);

} }); }

public void actionPerformed(ActionEvent e) { if (e.getSource() == p1) { setVisible(false); }

else if (e.getSource() == p1) { setVisible(false); } } }

运行结果如下如所示:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- xiaozhentang.com 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务