java实验报告实验六Java图形用户界面.docx
《java实验报告实验六Java图形用户界面.docx》由会员分享,可在线阅读,更多相关《java实验报告实验六Java图形用户界面.docx(31页珍藏版)》请在冰豆网上搜索。
java实验报告实验六Java图形用户界面
信息工程学院
Java程序设计实习报告
JAVA图形用户界面
实验六Java图形用户界面
1.实验目的
(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握Java事件处理机制。
2.实验容
实验题1编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:
加法:
主要代码:
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(){
publicvoidactionPerformed(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");
jButton5.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton5ActionPerformed(evt);
}
});
jButton6.setText("8");
jButton6.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton6ActionPerformed(evt);
}
});
jButton7.setText("4");
jButton7.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton7ActionPerformed(evt);
}
});
jButton8.setText("7");
jButton8.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton8ActionPerformed(evt);
}
});
jButton9.setText("0");
jButton9.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(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(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEventevt){
jButton14ActionPerformed(evt);
}
});
jButton15.setText("=");
jButton15.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(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中放置一个标签用来显示一些信息。
运行结果如图所示。
图3.8运行结果图
[基本要求]编写完整程序。
运行结果:
主要代码:
privateJLabel[]buttonDay=newJLabel[42];
privateJButton[]buttonWeek=newJButton[7];
privateJLabellabelMonth=newJLabel();
privateJButtonbuttonLastMonth=newJButton();
privateJButtonbuttonNextMonth=newJButton();
privateJPanelpCenter=newJPanel();
privateJPanelpNorth=newJPanel();
privateJPanelpSouth=newJPanel();
privateJLabeltime=newJLabel();
publicCalender(){
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(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],如果超出该围弹出对话框提示用户。
弹出提示对话框的时刻是光标离开第一个文本框时。
图3.9求和
运行结果:
检验输入数据围:
主要代码:
classMouseHanderextendsMouseAdapter
{
publicMouseHander(JTextFieldc)
{
current=c;
}
publicvoidmousePressed(MouseEventevent)
{
if(current==result)
{
doublefirstNumber=Double.parseDouble(first.getText());
doublesecondNumber=Double.parseDouble(second.getText());
doubleResult=firstNumber+secondNumber;
result.setText(""+Result);
}
elsecurrent.setText("");
}
privateJTextFieldcurrent;
}
classMouseMotionHanderextendsMouseMotionAdapter
{
publicvoidmouseMoved(MouseEventevent)
{
doublenumber=Double.parseDouble(first.getText());
if(number<100||number>200)
{
inttype=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null,newString("输入数字必须在100~200之间"),"提示",2);
}
}
}
实验题4编写一个显示图像文件的Application应用程序,在该程序JFrame窗体中添加JPanel面板和一个JToolBar工具栏,在工具栏上添加一个JButton“打开”按扭,单击“打开”按纽,弹出JFileChooser文件打开选择对话框,选择图像文件后将其显示在JPnel面板中。
运行结果:
主要代码:
publicclassNewJFrameextendsjavax.swing.JFrame{
privateJPaneljPanel1;
privateJPaneljPanel2;
privateJButtonjButton2;
privateJButtonjButton1;
privateStringfilePath="";
/**
*Auto-generatedmainmethodtodisplaythisJFrame
*/
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
NewJFrameinst=newNewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
publicNewJFrame(){
super();
initGUI();
}
privatevoidinitGUI(){
try{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1=newJPanel(true);
FlowLayoutjPanel1Layout=newFlowLayout();
getContentPane().add(jPanel1,BorderLayout.NORTH);
jPanel1.setPreferredSize(newjava.awt.Dimension(384,43));
jPanel1.setLayout(jPanel1Layout);
{
jButton2=newJButton();
jPanel1.add(jButton2);
jButton2.setText("Open");
jButton2.setBounds(104,12,62,22);
jButton2.setPreferredSize(newjava.awt.Dimension(76,22));
jButton2.addActionListener(newActionListener(){
publicvoidactionPerformed(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(){
publicvoidmousePressed(MouseEventevt){
System.exit(0);
//TODOaddyourcodeforjButton1.mousePressed
}
});
}
}
{
jPanel2=newJPanel();
getContentPane().add(jPanel2,BorderLayout.CENTER);
}
pack();
setSize(400,300);
}catch(Exceptione){
//addyourerrorhandlingcodehere
e.printStackTrace();
}
}
privatevoidjButton2ActionPerformed(ActionEventevt){
Stringaction=evt.getActionCommand();
JFileChooserfileChooser=newJFileChooser();
intindex=fileChooser.showOpenDialog(getContentPane());
if(index==JFileChooser.APPROVE_OPTION)
{
FileselectedFile=fileChooser.getSelectedFile();//取得选中的文件
filePath=selectedFile.getPath();
filePath=filePath.replace('\\','/');
ImageIconicon=newImageIcon(filePath);
JLabellabel=newJLabel(icon,JLabel.CENTER);
JLabelp=newJLabel("图片路径:
"+filePath);
jPanel2.add(p,BorderLayout.SOUTH);
jPanel2.add(label);
jPanel2.update(null);
}
}
}
实验题5使用SWT技术完成图3.10所示图形界面的制作。
要求“查询结果”用group组件。
图3.10数据查询界面
运行结果;
主要代码:
publicclassSearchextendsorg.eclipse.swt.widgets.Composite{
privateCompositecomposite1;
privateCompositecomposite2;
privateGroupgroup1;
privateTexttext4;
privateLabellabel4;
privateTexttext3;
privateLabellabel3;
privateTexttext2;
privateLabellabel2;
privateButtonbutton2;
privateButtonbutton1;
privateTexttext1;
privateLabellabel1;
{
//Registerasaresourceuser-SWTResourceManagerwill
//handletheobtaininganddisposingofresources
SWTResourceManager.registerResourceUser(this);
}
publicSearch(Comp