信 息 工 程 学 院1
Java程序设计 实习报告
JAVA图形用户界面
实验六Java图形用户界面
1.实验目的
(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。 (3)掌握Java事件处理机制。 2.实验内容
实验题1 编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
加法:
主要代码:
private void initComponents() { new new
(\);
jButton1 = new jButton4 = new jButton7 = new jButton10 = new jButton13 = new
jButton2 = new jButton5 = new jButton8 = new jButton11 = new jButton14 = new setStub(null);
jButton3 = new jButton6 = new jButton9 = new jButton12 = jButton15 =
jTextField1 = new
实验报告的内容与格式按任课教师的要求书写。
(new { });
public void actionPerformed evt) { }
jButton1ActionPerformed(evt);
(\); (new {
});
(\); (new { });
(\); (new { });
(\); (new { });
(\); (new {
public void actionPerformed evt) { jButton2ActionPerformed(evt);
}
public void actionPerformed evt) { jButton3ActionPerformed(evt);
}
public void actionPerformed evt) { jButton4ActionPerformed(evt);
}
public void actionPerformed evt) { jButton5ActionPerformed(evt);
}
public void actionPerformed evt) {
jButton6ActionPerformed(evt);
});
}
(\); (new { public void actionPerformed evt) {
jButton7ActionPerformed(evt);
}
});
(\); (new { public void actionPerformed evt) { jButton8ActionPerformed(evt);
}
});
(\); (new { public void actionPerformed evt) { jButton9ActionPerformed(evt);
}
});
(\); (new { public void actionPerformed evt) { jButton10ActionPerformed(evt);
}
});
(\); (new { public void actionPerformed evt) { jButton11ActionPerformed(evt);
}
});
(\); (new { });
public void actionPerformed evt) { }
jButton12ActionPerformed(evt);
(\); (new { });
public void actionPerformed evt) { }
jButton13ActionPerformed(evt);
(\); (new { });
public void actionPerformed evt) { }
jButton14ActionPerformed(evt);
(\); (new { });
public void actionPerformed evt) { }
jButton15ActionPerformed(evt);
实验题2 编写一个程序,有一个窗口,该窗口为BorderLayout布局。窗口的中心添加一个Panel容器:pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历。窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击previousMonth按钮,可以显示当前月的上一个月的日历。窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息。运行结果如图所示。
图 运行结果图
[基本要求] 编写完整程序。 运行结果:
主要代码:
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(); private JPanel pCenter=new JPanel(); private JPanel pNorth=new JPanel(); private JPanel pSouth=new JPanel(); private JLabel time=new JLabel(); public Calender() { super(\);
setBounds(250, 200, 600, 500); setDefaultCloseOperation;
(\上月\); (this);
(buttonLastMonth);
(\下月\); (this);
(buttonNextMonth);
getContentPane().add(pNorth,;
getContentPane().add(pCenter,; (new GridLayout(7,7)); for (int i = 0; i < 7; i++) { buttonWeek[i] = new JButton();
buttonWeek[i].setText(stringWeekCn[i]);