java实验报告实验六Java图形用户界面.docx
- 格式:docx
- 大小:292.25 KB
- 文档页数:27
信息工程学院1JaVa程序设计实习报告JAVA图形用户界面实验六JaVa图形用户界面1. 实验目的(1)掌握图形用户界面基本组件。
(2)了解如何使用布局管理器对组件进行管理。
(3)掌握JaVa事件处理机制。
2. 实验内容实验题1编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9) , 4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。
运行结果:_•小程序亘看器:PaClCageI.Calculator.classI实验报告的内容与格式按任课教师的要求书写。
加法:主要代码:PriVate Void in itComp Onen ts() {jButt on1 = :new javax.swing.JButton();jButt on2 = :new javax.swing.JButton();jButt on3 = :new javax.swing.JButton();jButt on4 = :new javax.swing.JButton();jButt on5 = :new javax.swing.JButton();jButt on6 = :new javax.swing.JButton();jButt on7 = :new javax.swing.JButton();jButt on8 = :new javax.swing.JButton();jButt on9 = :new javax.swing.JButton();jButto n10 =new javax.swing.JButton();jButto n11 =new javax.swing.JButton();jButto n12 =new javax.swing.JButton();jButto n13 =new javax.swing.JButton();jButto n14 =new javax.swing.JButton();jButto n15 =new javax.swing.JButton();jTextField1 =new javax.swing.JTextField();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 voidactionPerformed(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); }});jButton5 .setText( "6" );jButton5 .addActionListener(newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton5ActionPerformed(evt); }});jButton6 .setText( "8" );jButton6 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton6ActionPerformed(evt); }});jButton7 .setText( "4" );jButton7 .addActionListener( newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton7ActionPerformed(evt); }});jButton8 .setText( "7" );jButton8 .addActionListener( newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton8ActionPerformed(evt); }});jButton9 .setText( "0" );jButton9 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(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); }});jButton13 .setText( "-" );jButton13 .addActionListener( new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {jButton13ActionPerformed(evt);});jButton14 .setText( "+" );jButton14 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(java.awt.event.ActionEvent evt) {jButton14ActionPerformed(evt);}});jButton15 .setText( "=" );jButton15 .addActionListener( newjava.awt.event.ActionListener() {public voidactionPerformed(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 按钮,可以显示当前月的上一个月的日历。
窗口的南面添加一个Panel容器PSOUth,其布局是FlowLayout 布局, PSOuth 中放置一个标签用来显示一些信息。
运行结果如图所示图3.8运行结果图[基本要求]编写完整程序运行结果:主要代码:PriVate JLabel[] butt on Day = new JLabeI[42];PriVate JBUttO n[] butt on Week = new JBUtt on[ 7]; PriVate JLabeI labelMo nth = new JLabel();PriVate JBUtt On butto nLastMo nth = new JBUtt on(); PriVate JBUtt on butt onN extM on th = new JBUtt on(); PriVate JPanel PCe nter =new JPa nel();PriVate JPanel PNOrth =new JPa nel();PriVate JPaneI PSOUth =new JPa nel();PriVate JLabeI time =new JLabel();PUbIiCCale nder() {super ( "Calender" );setBounds(250, 200, 600, 500); setDefaultCloseOperation(JFrame.buttonLastMonth .setText( " 上月 " );buttonLastMonth .addActionListener(thispNorth .add( buttonLastMonth );buttonNextMonth .setText( " 下月 " );buttonNextMonth .addActionListener( this ); pNorth .add( buttonNextMonth ); getContentPane().add( pNorth ,BorderLayout. NORTH);pCenter ,BorderLayout. CENTER);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.9 所示的布局方式 功能:前两个文本框输入整型数据。