信 息 工 程 学 院 1
Java 程序设计 实习报告 A JAVA 图形用户界面
实验六 Java 图形用户界面 1.实验目的 (1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握 Java 事件处理机制。
2.实验内容 题 实验题 1
编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10 个数字按钮(0-9),4 个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
实验报告的内容与格式按任课教师的要求书写。
带 格式 的: 首页采用不同的页眉
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米加法:
主要代码:
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
setStub(null);
jButton1.setText("3");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("1");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("5");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("2");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
}
});
jButton5.setText("6");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setText("8");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setText("4");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setText("7");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
}
});
jButton9.setText("0");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("9");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setText("\u00f7");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setText("\u00d7");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
}
});
jButton13.setText("-");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setText("+");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
jButton15.setText("=");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
实验题 2 编写一个程序,有一个窗口,该窗口为 BorderLayout 布局。窗口的中心添加一个 Panel 容器:pCenter,pCenter 的布局是 7 行 7 列的 GridLayout 布局,pCenter的中放置 49 个标签,用来显示日历。窗口北面添加一个 Panel 容器 pNorth,其布局是FlowLayout 布局,pNorth 放置两个按钮:nextMonth 和 previousMonth 按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击 previousMonth 按钮,可以显示当
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米前月的上一个月的日历。窗口的南面添加一个 Panel 容器 pSouth,其布局是 FlowLayout布局,pSouth 中放置一个标签用来显示一些信息。运行结果如图所示。
图 3.8 运行结果图 [基本要求] 编写完整程序。
运行结果:
主要代码:
private JLabel[] buttonDay = new JLabel[42]; private JButton[] buttonWeek = new JButton[7]; private JLabel labelMonth = new JLabel(); private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米private JPanel pCenter=new JPanel(); private JPanel pNorth=new JPanel(); private JPanel pSouth=new JPanel(); private JLabel time=new JLabel(); public Calender() {
super("Calender");
setBounds(250, 200, 600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonLastMonth.setText("上月");
buttonLastMonth.addActionListener(this);
pNorth.add(buttonLastMonth);
buttonNextMonth.setText("下月");
buttonNextMonth.addActionListener(this);
pNorth.add(buttonNextMonth);
getContentPane().add(pNorth,BorderLayout.NORTH);
getContentPane().add(pCenter,BorderLayout.CENTER);
pCenter.setLayout(new GridLayout(7,7));
for (int i = 0; i < 7; i++) {
buttonWeek[i] = new JButton();
buttonWeek[i].setText(stringWeekCn[i]);
pCenter.add(buttonWeek[i]);
}
for (int i = 0; i < 42; i++) {
buttonDay[i] = new JLabel();
buttonDay[i].setText("
");
pCenter.add(buttonDay[i]);
}
实验题 3 3 实现如图 3.9 所示的布局方式 功能:前两个文本框输入整型数据。第三个文本框存放前两个文本框数据之和。
要求如下:
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米第一个文本框的数据是[100,200],如果超出该范围弹出对话框提示用户。弹出提示对话框的时刻是光标离开第一个文本框时。
图 3.9 求和 运行结果:
检验输入数据范围:
主要代码:
class MouseHander extends MouseAdapter
{
public MouseHander(JTextField c)
{
current=c;
}
public void mousePressed(MouseEvent event)
{
if(current==result)
{
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
double firstNumber=Double.parseDouble(first.getText());
double secondNumber=Double.parseDouble(second.getText());
double Result=firstNumber+secondNumber;
result.setText(""+Result);
}
else current.setText("");
}
private JTextField current;
} class MouseMotionHander extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent event)
{
double number=Double.parseDouble(first.getText());
if(number<100||number>200)
{
int type=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null,new String("输入数字必须在100~200之间"),"提示", 2);
}
}
}
实验题 实验题 4 编写一个显示图像文件的 Application 应用程序,在该程序 JFrame 窗体中添加 JPanel 面板和一个 JToolBar 工具栏,在工具栏上添加一个 JButton“打开”按扭,单击“打开”按纽,弹出 JFileChooser 文件打开选择对话框,选择图像文件后将其显示在 JPnel 面板中。
运行结果:
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
主要代码:
public class NewJFrame extends javax.swing.JFrame {
private JPanel jPanel1;
private JPanel jPanel2;
private JButton jButton2;
private JButton jButton1;
private String filePath = "";
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel(true);
FlowLayout jPanel1Layout = new FlowLayout();
getContentPane().add(jPanel1, BorderLayout.NORTH);
jPanel1.setPreferredSize(new java.awt.Dimension(384, 43));
jPanel1.setLayout(jPanel1Layout);
{
jButton2 = new JButton();
jPanel1.add(jButton2);
jButton2.setText("Open");
jButton2.setBounds(104, 12, 62, 22);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
jButton2.setPreferredSize(new java.awt.Dimension(76, 22));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}
{
jButton1 = new JButton();
jPanel1.add(jButton1);
jButton1.setText("Close");
jButton1.setBounds(204, 12, 62, 22);
jButton1.setPreferredSize(new java.awt.Dimension(72, 22));
jButton1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
System.exit(0);
//TODO add your code for jButton1.mousePressed
}
});
}
}
{
jPanel2 = new JPanel();
getContentPane().add(jPanel2, BorderLayout.CENTER);
}
pack();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jButton2ActionPerformed(ActionEvent evt) {
String action = evt.getActionCommand();
JFileChooser fileChooser = new JFileChooser();
int index = fileChooser.showOpenDialog(getContentPane());
if(index == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
//取得选中的文件
filePath = selectedFile.getPath();
filePath = filePath.replace("\\","/");
ImageIcon icon = new ImageIcon(filePath);
JLabel label = new JLabel(icon,JLabel.CENTER);
JLabel p=new JLabel("图片路径:" + filePath);
jPanel2.add(p,BorderLayout.SOUTH);
jPanel2.add(label);
jPanel2.update(null);
}
}
}
实验题 实验题 5 5 使用 SWT 技术完成图 3.10 所示图形界面的制作。要求“查询结果”用group 组件。
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 图 3.10 数据查询界面 运行结果;
主要代码:
public class Search extends org.eclipse.swt.widgets.Composite {
private Composite composite1;
private Composite composite2;
private Group group1;
private Text text4;
private Label label4;
private Text text3;
private Label label3;
private Text text2;
private Label label2;
private Button button2;
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 private Button button1;
private Text text1;
private Label label1;
{
//Register as a resource user - SWTResourceManager will
//handle the obtaining and disposing of resources
SWTResourceManager.registerResourceUser(this);
}
public Search(Composite parent, int style) {
super(parent, style);
initGUI();
}
/**
* Initializes the GUI.
*/
private void initGUI() {
try {
this.setSize(new org.eclipse.swt.graphics.Point(400,300));
this.setBackground(SWTResourceManager.getColor(192, 192, 192));
FormLayout thisLayout = new FormLayout();
this.setLayout(thisLayout);
{
composite1 = new Composite(this, SWT.NONE);
FormLayout composite1Layout = new FormLayout();
FormData composite1LData = new FormData();
composite1LData.left =
new FormAttachment(0, 1000, 0);
composite1LData.top =
new FormAttachment(0, 1000, 0);
composite1LData.width = 400;
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
composite1LData.height = 300;
composite1.setLayoutData(composite1LData);
composite1.setLayout(composite1Layout);
{
group1 = new Group(composite1, SWT.NONE);
GridLayout group1Layout = new GridLayout();
group1Layout.marginBottom = 30;
group1Layout.numColumns = 2;
group1Layout.verticalSpacing = 15;
group1Layout.marginTop = 10;
group1.setLayout(group1Layout);
FormData group1LData = new FormData();
group1LData.left =
new FormAttachment(0, 1000, 38);
group1LData.top =
new FormAttachment(0, 1000, 106);
group1LData.width = 318;
group1LData.height = 145;
group1.setLayoutData(group1LData);
group1.setText("\u67e5\u8be2\u7ed3\u679c");
group1.setFont(SWTResourceManager.getFont("微软雅黑", 11, 0, false, false));
{
label2 = new Label(group1, SWT.NONE);
GridData label2LData = new GridData();
label2LData.widthHint = 70;
label2LData.heightHint = 25;
label2.setLayoutData(label2LData);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
label2.setText("\u5b66\u53f7\uff1a");
label2.setFont(SWTResourceManager.getFont("微软雅黑", 11, 0, false, false));
}
{
GridData text2LData = new GridData();
text2LData.widthHint = 181;
text2LData.heightHint = 27;
text2 = new Text(group1, SWT.NONE);
text2.setLayoutData(text2LData);
}
{
label3 = new Label(group1, SWT.NONE);
GridData label3LData = new GridData();
label3LData.widthHint = 63;
label3LData.heightHint = 23;
label3.setLayoutData(label3LData);
label3.setText("\u59d3\u540d\uff1a");
label3.setFont(SWTResourceManager.getFont("微软雅黑", 11, 0, false, false));
}
{
GridData text3LData = new GridData();
text3LData.widthHint = 181;
text3LData.heightHint = 27;
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
text3 = new Text(group1, SWT.NONE);
text3.setLayoutData(text3LData);
}
{
label4 = new Label(group1, SWT.NONE);
GridData label4LData = new GridData();
label4LData.widthHint = 57;
label4LData.heightHint = 28;
label4.setLayoutData(label4LData);
label4.setText("\u5e74\u9f84\uff1a");
label4.setFont(SWTResourceManager.getFont("微软雅黑", 11, 0, false, false));
}
{
GridData text4LData = new GridData();
text4LData.widthHint = 182;
text4LData.heightHint = 31;
text4 = new Text(group1, SWT.NONE);
text4.setLayoutData(text4LData);
}
}
{
composite2 = new Composite(composite1, SWT.NONE);
GridLayout composite2Layout = new GridLayout();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
composite2Layout.numColumns = 2;
composite2Layout.marginLeft = 40;
composite2Layout.horizontalSpacing = 10;
composite2Layout.verticalSpacing = 15;
FormData composite2LData = new FormData();
composite2LData.left =
new FormAttachment(0, 1000, 12);
composite2LData.top =
new FormAttachment(0, 1000, 12);
composite2LData.width = 376;
composite2LData.height = 88;
composite2.setLayoutData(composite2LData);
composite2.setLayout(composite2Layout);
{
label1 = new Label(composite2, SWT.NONE);
GridData label1LData = new GridData();
label1LData.widthHint = 96;
label1LData.heightHint = 22;
label1.setLayoutData(label1LData);
label1.setText("\u8bf7\u8f93\u5165\u5b66\u53f7\uff1a");
label1.setFont(SWTResourceManager.getFont("微软雅黑", 12, 0, false, false));
}
{
GridData text1LData = new GridData();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
text1LData.widthHint = 182;
text1LData.heightHint = 28;
text1 = new Text(composite2, SWT.NONE);
text1.setLayoutData(text1LData);
}
{
button1 = new Button(composite2, SWT.PUSH | SWT.CENTER);
GridData button1LData = new GridData();
button1LData.widthHint = 70;
button1LData.heightHint = 27;
button1.setLayoutData(button1LData);
button1.setText("\u786e\u5b9a");
}
{
button2 = new Button(composite2, SWT.PUSH | SWT.CENTER);
GridData button2LData = new GridData();
button2LData.widthHint = 78;
button2LData.heightHint = 27;
button2.setLayoutData(button2LData);
button2.setText("\u91cd\u7f6e");
}
}
}
this.layout();
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
Search inst = new Search(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
实验题 6 6 使用 SWT 技术完成图 3.11 所示学籍管理主界面的设计与制作。
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 图 3.11 学籍管理主界面 运行结果:
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 主要代码:
public class Example {
public static void main(String[] args) {
Window win=new Window("学籍管理系统",300,200,500,500);
} } package package6;
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() {
}
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 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", 1, 22));
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("用户登录", new ImageIcon("Login,gif"));
item2 = new JMenuItem("退
出", new ImageIcon("Exit.gif"));
menu.add(item1);
menu.add(item2);
menubar.add(menu);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menuhelp);
setJMenuBar(menubar);
} }
实验题7
7
使用SWT技术在图3.11所示界面中,在“用户登录”菜单中添加组件选择事件,当选中“用户登录”时,打开图3.12所示用户登录界面。
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米 图 3.12 用户登录窗体
运行结果:
主要代码 主要代码:
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) {
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
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", 1, 22));
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("用户登录", new ImageIcon("a,gif"));
item1.addActionListener(this);
dialog = new MyDialog(this, "SWT应用程序", true);
item2 = new JMenuItem("退
出", new ImageIcon("b.gif"));
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);
精选文库 —
2 带 格式 的: 右带 格式 的: 右侧:
0.63 厘米
dialog.setVisible(true);}
} } 实验总结:
按照实验要求,在本次实验中我需要掌握图形用户界面基本组件,了解如何使用布局管理器对组件进行管理,掌握 Java 事件处理机制。
在实验过程中我遇到了一些问题,首先是在软件的配置上,SWT 组件的运用,因为对拖放设计界面不熟悉,所以在操作起来很慢。还有在事件监听处理上,尤其是在写计算器那个题目时问题比较多,通过同学的帮助最终还是解决了这个问题。
通过这次试验,我还是收获了很多东西。尤其是像在图形界面设计中,我们更应该多动手实际操作,这样才可以熟练,高效的完成任务。
V V ery good!
2012- - 06- - 13