西北農林科技大學_實驗8 Swing_第1頁
西北農林科技大學_實驗8 Swing_第2頁
西北農林科技大學_實驗8 Swing_第3頁
西北農林科技大學_實驗8 Swing_第4頁
西北農林科技大學_實驗8 Swing_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、信息工程學院Java語言課內實習報告 (20132014學年第 二學期)實習題目:_實驗四 姓 名:_夏偉華_ _ 學 號:_2012013279_ _ 專 業(yè):_計算機科學技術_年級班級:_12級2班_實驗8 大家終于等到使用Java做圖形界面啦,好Happy!今天我們就將一起了解圖形用戶界面基本組件的使用,布局管理器如何對組件進行管理以及事件處理機制的應用。任務一:小小界面設計請設計一個界面,包括:菜單,文本框,按鈕,標簽,文本區(qū),選擇框,單選按鈕和下拉列表。import java.awt.EventQueue;public class Ex8 private JFrame frmEcli

2、psev;private JTextField txtImportJava;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try Ex8 window = new Ex8();window.frmEclipsev.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */p

3、ublic Ex8() initialize();/* * Initialize the contents of the frame. */private void initialize() frmEclipsev = new JFrame();frmEclipsev.setTitle("Eclipseu6C49u5316u7248V 1.0");frmEclipsev.setBounds(100, 100, 450, 300);frmEclipsev.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frmEclipsev.ge

4、tContentPane().setLayout(null);txtImportJava = new JTextField();txtImportJava.setText("System.out.println("Hello u7A0Bu5E8Fu733F");");txtImportJava.setBounds(112, 79, 306, 43);frmEclipsev.getContentPane().add(txtImportJava);txtImportJava.setColumns(10);JButton btnDebug = new JBut

5、ton("Debug");btnDebug.setBounds(188, 135, 113, 27);frmEclipsev.getContentPane().add(btnDebug);JLabel label = new JLabel("u6211u662Fu6807u7B7EuFF0Cu663Eu793Au4FE1u606F");label.setBounds(265, 13, 153, 34);frmEclipsev.getContentPane().add(label);JTextArea textArea = new JTextArea();

6、textArea.setText("u6587u672Cu533A");textArea.setBounds(149, 19, 88, 34);frmEclipsev.getContentPane().add(textArea);JCheckBox checkBox = new JCheckBox("u9009u62E9u6846");checkBox.setBounds(10, 17, 133, 27);frmEclipsev.getContentPane().add(checkBox);JRadioButton radioButton = new J

7、RadioButton("u5355u9009u6309u94AE");radioButton.setBounds(30, 171, 157, 27);frmEclipsev.getContentPane().add(radioButton);JRadioButton rdbtnNewRadioButton = new JRadioButton("u5355u9009u6309u94AE");rdbtnNewRadioButton.setBounds(224, 177, 157, 27);frmEclipsev.getContentPane().add(

8、rdbtnNewRadioButton);JComboBox comboBox = new JComboBox();comboBox.setBounds(20, 53, 69, 24);frmEclipsev.getContentPane().add(comboBox);comboBox.addItem("Socket");comboBox.addItem("MVC");comboBox.addItem("Web");JCheckBox chckbxNewCheckBox = new JCheckBox("u9009u62E

9、9u6846");chckbxNewCheckBox.setBounds(10, 131, 133, 27);frmEclipsev.getContentPane().add(chckbxNewCheckBox);JMenuBar menuBar = new JMenuBar();frmEclipsev.setJMenuBar(menuBar);JMenu menu = new JMenu("u83DCu5355");menuBar.add(menu);JMenuItem mntmjavaj = new JMenuItem("u65B0u5EFAJAVA

10、u5DE5u7A0B");menu.add(mntmjavaj);JMenuItem menuItem_1 = new JMenuItem("u6253u5F00u6587u4EF6");menu.add(menuItem_1);JMenuItem menuItem_2 = new JMenuItem("u5173u95ED");menu.add(menuItem_2);JMenu menu_6 = new JMenu("u65B0u5EFAu5DE5u7A0B");menu.add(menu_6);JMenu mnXml

11、= new JMenu("XMLu5DE5u7A0B");menu_6.add(mnXml);JMenu menu_1 = new JMenu("u7F16u8F91");menuBar.add(menu_1);JMenu menu_2 = new JMenu("u67E5u627E");menuBar.add(menu_2);JMenu menu_3 = new JMenu("u8FD0u884C");menuBar.add(menu_3);JMenu menu_4 = new JMenu("u7A97

12、u53E3");menuBar.add(menu_4);JMenu menu_5 = new JMenu("u5E2Eu52A9");menuBar.add(menu_5);任務二: 計算器(1)必做內容:編寫一個計算器的程序,主界面由多個按鈕組建、文本框組建構成。運行結果如圖所示:(2)選做內容:同時,使用相應的事件對這個組件進行監(jiān)聽工作,達到計算的功能。import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JTextF

13、ield;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class Gui private JFrame frame;private JTextField textField;/* * Launch the application. */public static void main(String args) EventQueue.invokeLater(new Runnable() public void run() try Gui window = new Gui();window

14、.frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* * Create the application. */public Gui() initialize();/* * Initialize the contents of the frame. */private void initialize() frame = new JFrame();frame.setTitle("u7B80u6613u8BA1u7B97u5668");frame.setBounds(100, 100, 450,

15、 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(null);JButton btnNewButton = new JButton("9");btnNewButton.setBounds(14, 78, 62, 27);frame.getContentPane().add(btnNewButton);textField = new JTextField();textField.setBounds(14, 13, 393, 24);frame.

16、getContentPane().add(textField);textField.setColumns(10);JButton button = new JButton("6");button.setBounds(14, 118, 62, 27);frame.getContentPane().add(button);JButton button_1 = new JButton("3");button_1.setBounds(14, 158, 62, 27);frame.getContentPane().add(button_1);JButton but

17、ton_2 = new JButton("0");button_2.setBounds(14, 198, 62, 27);frame.getContentPane().add(button_2);JButton button_3 = new JButton("8");button_3.setBounds(90, 78, 62, 27);frame.getContentPane().add(button_3);JButton button_4 = new JButton("5");button_4.setBounds(90, 118,

18、62, 27);frame.getContentPane().add(button_4);JButton button_5 = new JButton("2");button_5.setBounds(90, 158, 62, 27);frame.getContentPane().add(button_5);JButton button_6 = new JButton("+/-");button_6.setBounds(90, 198, 62, 27);frame.getContentPane().add(button_6);JButton button_

19、7 = new JButton("7");button_7.setBounds(166, 78, 62, 27);frame.getContentPane().add(button_7);JButton button_8 = new JButton("4");button_8.setBounds(166, 118, 62, 27);frame.getContentPane().add(button_8);JButton button_9 = new JButton("1");button_9.setBounds(166, 158, 6

20、2, 27);frame.getContentPane().add(button_9);JButton button_10 = new JButton(".");button_10.setBounds(166, 198, 62, 27);frame.getContentPane().add(button_10);JButton button_11 = new JButton("/");button_11.setBounds(261, 78, 72, 27);frame.getContentPane().add(button_11);JButton but

21、ton_12 = new JButton("*");button_12.setBounds(261, 118, 72, 27);frame.getContentPane().add(button_12);JButton button_13 = new JButton("-");button_13.setBounds(261, 158, 72, 27);frame.getContentPane().add(button_13);JButton button_14 = new JButton("+");button_14.setBound

22、s(261, 198, 72, 27);frame.getContentPane().add(button_14);JButton btnSqrt = new JButton("Sqrt");btnSqrt.setBounds(347, 78, 72, 27);frame.getContentPane().add(btnSqrt);JButton button_16 = new JButton("%");button_16.setBounds(346, 118, 72, 27);frame.getContentPane().add(button_16);

23、JButton btnx = new JButton("1/X");btnx.setBounds(347, 158, 72, 27);frame.getContentPane().add(btnx);JButton button_18 = new JButton("=");button_18.setBounds(346, 198, 72, 27);frame.getContentPane().add(button_18);JButton btnBack = new JButton("Back");btnBack.setBounds(1

24、4, 38, 98, 27);frame.getContentPane().add(btnBack);JButton btnEc = new JButton("CE");btnEc.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) );btnEc.setBounds(126, 38, 88, 27);frame.getContentPane().add(btnEc);JButton btnC = new JButton("C");btn

25、C.setBounds(247, 38, 72, 27);frame.getContentPane().add(btnC);JButton btnAbout = new JButton("ABOUT");btnAbout.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) );btnAbout.setBounds(335, 38, 72, 27);frame.getContentPane().add(btnAbout);任務三:算數(shù)測試編寫一個算術測試小軟件,用來

26、訓練小學生的算術能力。程序由3個類組成,其中Teacher類對象負責給出算術題目,并判斷回答者的答案是否正確:ComputerFrame類對象提供的GUI界面看到題目并通過該GUI界面給出題目的答案;MailClass是主類。l 請童鞋們去作業(yè)管理系統(tǒng)下載QuestionAndAnswer.zip。Teacher.java和MailClassss.java已給出,請按照程序要求,將ComputerFrame.java中的【代碼】替換為程序代碼。textOnetextTwogetProblemgiveAnswermessageoperatorLabel giveAnswertextResultl

27、 運行結果如圖所示。圖中標出了各個組件的變量名稱。l 點擊“獲取題目”后,顯示題目:l 點擊“確認答案”后,如果輸入答案錯誤,顯示:“你回答錯誤”,如果答案正確,顯示“你回答正確”,如果輸入的不是數(shù)字,顯示“請輸入數(shù)字字符”。MainClass.javapublic class MainClass public static void main (String args) ComputerFrame frame; frame=new ComputerFrame(); frame.setTitle("算數(shù)測試"); frame.setBounds(100,100,650,18

28、0); ComputerFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class ComputerFrame extends JFrame JMenuBar menubar;JMenu choiceGrade;JMenuItem grade1,grade2,grade3; JButton getProblem,giveAnwser; JLabel operatorLabel,message; JTextField textOne,textTwo,textResult; Teache

29、r teacherzhang; ComputerFrame() teacherzhang=new Teacher(); teacherzhang.setMaxInteger(20); setLayout(new FlowLayout(); menubar=new JMenuBar(); choiceGrade=new JMenu("選擇級別"); grade1=new JMenuItem("幼兒級別"); grade2=new JMenuItem("兒童級別"); grade3=new JMenuItem("小學生級別&qu

30、ot;); grade1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) teacherzhang.setMaxInteger(10); ); grade2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) teacherzhang.setMaxInteger(50); ); grade3.addActionListener(new ActionListener()

31、public void actionPerformed(ActionEvent e) teacherzhang.setMaxInteger(100); ); choiceGrade.add(grade1); choiceGrade.add(grade2); choiceGrade.add(grade3); menubar.add(choiceGrade); setJMenuBar(menubar); textOne=new JTextField(5); textTwo=new JTextField(5); textResult=new JTextField(5); operatorLabel=

32、 new JLabel("+"); operatorLabel.setFont(new Font("Arial",Font.BOLD,20); message=new JLabel("你還沒有回答呢"); getProblem=new JButton("獲取題目"); giveAnwser=new JButton("確認答案"); add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new JLabel(&qu

33、ot;="); add(textResult); add(giveAnwser); add(message); textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false); getProblem.setActionCommand("getProblem"); textResult.setActionCommand("answer"); giveAnwser.setActionCommand("answer"); teach

34、erzhang.setJTextField(textOne, textTwo, textResult); teacherzhang.setJLabel(operatorLabel,message); getProblem.addActionListener(teacherzhang); textResult.addActionListener(teacherzhang); giveAnwser.addActionListener(teacherzhang); setVisible(true); validate(); setDefaultCloseOperation(DISPOSE_ON_CL

35、OSE); Teacher.javaimport java.util.*;import java.awt.event.*;import javax.swing.*;public class Teacher implements ActionListener int numberOne ,numberTwo;String operator=""boolean isRight;Random random;int maxInteger;JTextField textOne,textTwo,textResult;JLabel operatorLabel,message;Teache

36、r()random =new Random();public void setMaxInteger(int n) maxInteger=n;public void actionPerformed(ActionEvent e)String str=e.getActionCommand();if(str.equals("getProblem")numberOne=random.nextInt(maxInteger)+1;numberTwo=random.nextInt(maxInteger)+1;double d=Math.random();if(d<0.4) opera

37、tor="+"else if(d>0.6)operator="*"elseoperator="-"textOne.setText(""+numberOne);textTwo.setText(""+numberTwo);operatorLabel.setText(operator);message.setText("請回答");textResult.setText(null);else if(str.equals("answer")String answer=textResult.getText();tryint result=Integer.parseInt(answer);if (operator.equals("+")if(result=numberOne+numberTwo)message.setText("你回答正確");elsemessage.setText("你回答錯誤");else if(operator.equals("-")i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論