java实验报告实验六Java图形用户界面.docx
- 文档编号:14573381
- 上传时间:2023-06-24
- 格式:DOCX
- 页数:38
- 大小:246.84KB
java实验报告实验六Java图形用户界面.docx
《java实验报告实验六Java图形用户界面.docx》由会员分享,可在线阅读,更多相关《java实验报告实验六Java图形用户界面.docx(38页珍藏版)》请在冰点文库上搜索。
java实验报告实验六Java图形用户界面
信息工程学院1
Java程序设计实习报告
JAVA图形用户界面
实验六Java图形用户界面
1.实验目的
(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握Java事件处理机制。
2.实验内容
实验题1编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,
10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
小程序亘看器:
paclcagel.Calculator.dass
口I回
实验报告的内容与格式按任课教师的要求书写。
加法:
主要代码:
privatevoidinitComponents(){
jButton1=
:
newjavax.swing.JButton();
jButton2=
:
newjavax.swing.JButton();
jButton3=
:
newjavax.swing.JButton();
jButton4=
:
newjavax.swing.JButton();
jButton5=
:
newjavax.swing.JButton();
jButton6=
:
newjavax.swing.JButton();
jButton7=
:
newjavax.swing.JButton();
jButton8=
:
newjavax.swing.JButton();
jButton9=
:
newjavax.swing.JButton();
jButton10
=newjavax.swing.JButton();
jButton11
=newjavax.swing.JButton();
jButton12
=newjavax.swing.JButton();
jButton13
=newjavax.swing.JButton();
jButton14
=newjavax.swing.JButton();
jButton15
=newjavax.swing.JButton();
jTextField1
=newjavax.swing.JTextField();
setStub(null);jButton1.setText("3");
jButton1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton1ActionPerformed(evt);}
});
jButton2.setText("1");
jButton2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton2ActionPerformed(evt);}
});
jButton3.setText("5");
jButton3.addActionListener(newjava.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){jButton3ActionPerformed(evt);
}
});
jButton4.setText("2");
jButton4.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton4ActionPerformed(evt);}
});
jButton5.setText("6");
new
jButton5.addActionListener(
java.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton5ActionPerformed(evt);}
});
jButton6.setText("8");
jButton6.addActionListener(new
java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton6ActionPerformed(evt);}
});
jButton7.setText("4");
jButton7.addActionListener(new
java.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton7ActionPerformed(evt);}
});
jButton8.setText("7");
jButton8.addActionListener(new
java.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton8ActionPerformed(evt);}
});
jButton9.setText("0");
jButton9.addActionListener(new
java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton9ActionPerformed(evt);}
});
jButton10.setText("9");
jButton10.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton10ActionPerformed(evt);
}
});
jButton11.setText("\u00f7");
jButton11.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton11ActionPerformed(evt);}
});
jButton12.setText("\u00d7");
jButton12.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton12ActionPerformed(evt);}
});
jButton13.setText("-");
jButton13.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton13ActionPerformed(evt);
});
jButton14.setText("+");
jButton14.addActionListener(new
java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton14ActionPerformed(evt);
}
});
jButton15.setText("=");
jButton15.addActionListener(new
java.awt.event.ActionListener(){
publicvoid
actionPerformed(java.awt.event.ActionEventevt){
jButton15ActionPerformed(evt);
}
});
实验题2编写一个程序,有一个窗口,该窗口为BorderLayout布局。
窗口的中心
添加一个Panel容器:
pCenter,pCenter的布局是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历。
窗口北面添加一个Panel容器pNorth,其布局是FlowLayout布局,pNorth放置两个按钮:
nextMonth和previousMonth按钮,单击nextMonth,可以显示当前月的下一个月的日历;单击previousMonth按钮,可以显示当前月的上一个月的日历。
窗口的南面添加一个Panel容器pSouth,其布局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息。
运行结果如图所示
■ID1xl
上月|下甲|
1曰-
E_J
一
二I
1三1
四
五
六
1
2
3
4
5
6
7
8
□
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
20
27
28
29
30
31
S1S:
2OO7^10.9
图3.8运行结果图
[基本要求]编写完整程序
运行结果:
主要代码:
private
JLabel[]
buttonDay=newJLabel[42];
private
JButton[]
buttonWeek=newJButton[7];
private
JLabel
labelMonth=newJLabel();
private
JButton
buttonLastMonth=newJButton();
private
JButton
buttonNextMonth=newJButton();
private
JPanel
pCenter=newJPanel();
private
JPanel
pNorth=newJPanel();
private
JPanel
pSouth=newJPanel();
private
JLabel
time=newJLabel();
public
Calender(){
super("Calender");
EXIT_ON_CLOSE);
setBounds(250,200,600,500);setDefaultCloseOperation(JFrame.
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(
newGridLayout(7,7));
for(inti=0;i<7;i++){
buttonWeek[i]=newJButton();
buttonWeek[i].setText(stringWeekCn[i]);pCenter.add(buttonWeek[i]);
}
for(inti=0;i<42;i++){buttonDay[i]=newJLabel();buttonDay[i].setText("");pCenter.add(buttonDay[i]);
}
实验题3实现如图3.9所示的布局方式功能:
前两个文本框输入整型数据。
第三个文本框存放前两个文本框数据之和。
要求如下:
第一个文本框的数据是[100,200],如果超出该范围弹出对话框提示用户。
弹出提示对话框的时刻是光标离开第一个文本框时。
拿TextFieldTest
图3.9求和
运行结果:
检验输入数据范围:
TextFiddTest
主要代码:
classMouseHanderextendsMouseAdapter
{
publicMouseHanderJTextFieldc)
{
current=c;
}
publicvoidmousePressed(MouseEventevent)
{
if(current==result)
{
double
firstNumber=Double.parseDouble(first.getText());
double
secondNumber=Double.
parseDouble(second.getText());
doubleResult=firstNumber+secondNumber;
result.setText(""+Result);
}
elsecurrent.setText("");
}
privateJTextFieldcurrent;
}
classMouseMotionHanderextendsMouseMotionAdapter
{
publicvoidmouseMoved(MouseEventevent)
{
double
number=Double.parseDouble(first.getText());
if(number<100||number>200)
{
int
type=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null,newString("输入数字必须在100~200之间"),"提
示“,2);
}
}
}
实验题4编写一个显示图像文件的Application应用程序,在该程序JFrame窗体
中添加JPanel面板和一个JToolBar工具栏,在工具栏上添加一个JButton“打开”按扭,单击“打开”按纽,弹出JFileChooser文件打开选择对话框,选择图像文件后将
其显示在JPnel面板中。
运行结果:
OpenCJose
圈片貉径:
C:
jUs&rs)daixian(jn)e&ktvp)Ac3^兰jpO
主要代码:
public
classNewJFrame
extendsjavax.swing.JFrame{
privateJPaneljPanell
privateJPaneljPanel2
privateJButtonjButton2
privateJButtonjButton1
privateStringfilePath
/**
*Auto
-generatedmainmethodtodisplay
thisJFrame
*/
public
staticvoidmain(String[]args){
SwingUtilities.
invokeLater(new
Runnable(){
public
voidrun(){
NewJFrameinst=newNewJFrame();
null);
inst.setLocationRelativeTo(
public
inst.setVisible(
true);
});
NewJFrame(){
super();
initGUI();
private
voidinitGUI(){
try{
setDefaultCloseOperation(WindowConstants.
DISPOSE_ON_CLOSE);
jPanel1
newJPanel(
true);
FlowLayoutjPanel1Layout=
newFlowLayout();
BorderLayout.
NORTH);
getContentPane().add(
jPanel1
jPanel1
.setPreferredSize(
new
java.awt.Dimension(384,43));
jPanel1
.setLayout(jPanel1Layout);
jButton2
=newJButton();
jPanel1
.add(jButton2);
jButton2
.setText("Open");
jButton2
.setBounds(104,12,62,22);
jButton2
.setPreferredSize(
new
java.awt.Dimension(76,22));
jButton2.addActionListener(
ActionListener(){
publicvoid
actionPerformed(ActionEventevt){
jButton2ActionPerformed(evt);
}
});
}
{
jButton1=newJButton();
jPanel1.add(jButton1);
jButton1.setText("Close");
jButton1.setBounds(204,12,62,22);
jButton1.setPreferredSize(newjava.awt.Dimension(72,22));
jButton1.addMouseListener(newMouseAdapter(){
publicvoid
mousePressed(MouseEventevt){
System.exit(0);
//TODOaddyourcode
forjButton1.mousePressed
}
});
}
}
{
jPanel2=newJPanel();
getContentPane().add(jPanel2,
BorderLayout.CENTER);
}
pack();
setSize(400,300);
}catch(Exceptione){
//addyourerrorhandlingcodehere
e.printStackTrace();
}
private
voidjButton2ActionPerformed(ActionEventevt){
Stringaction=evt.getActionCommand();
JFileChooserfileChooser=newJFileChooser();
intindex=fileChooser.showOpenDialog(getContentPane());if(index==JFileChooser.APPROVE_OPTION)
{
//
FileselectedFile=fileChooser.getSelectedFile();
取得选中的文件
filePath=selectedFile.getPath();
filePath=filePath.replace('\\',7');
ImageIconicon=newImageIcon(filePath);
JLabellabel=newJLabel(icon,JLabel.CENTER);
JLabelp=newJLabel("图片路径:
"+filePath);
jPanel2.add(p,BorderLayout.SOUTH);
jPanel2.add(label);
jPanel2.update(null);
}
}
}
实验题5
group组件。
使用SWT技术完成图3.10所示图形界面的制作。
要求“查询结果”用
图3.10数据查询界面
运行结果;
■3旦育百
请输入学号:
誦走—畫醫—11
Lt„
査询结果
学号:
姓名:
年龄:
主要代码:
publicclassSearchextendsorg.eclipse.swt.widgets.Composite{
private
Compositecompositel
private
Compositecomposite2
private
Group
groupl;
private
Text
text4;
private
Label
label4;
private
Text
text3;
private
Label
label3;
private
Text
text2;
private
Label
labe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 报告 图形 用户界面
![提示](https://static.bingdoc.com/images/bang_tan.gif)