答题小软件
答题小软件
讲真这次的要求对现在的我来说难度是巨大的,GUI的编写还得到了同学的帮助,不然真心要GG,这次也花了七八个小时的时间,但做完之后也感觉的到自己的进步,看来真的是要多写代码。
课程理解:类图的3个基本组件:类名、属性、方法。其中类有6种关系:泛化,实现,依赖,关联,聚合,组合。
Dice.java
package touzi; import java.util.*; public class Dice { private int faceValue; public Dice(){ } public void Roll(){ Random random=new Random(); faceValue=random.nextInt(5)+1; } public int getFaceValue(){ return faceValue; } }
DiceGUI
package touzi; import java.awt.EventQueue; import java.awt.Image; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JButton; import java.awt.event.ActionListener; import java.util.Scanner; import java.awt.event.ActionEvent; import javax.swing.JTextField; import javax.swing.JEditorPane; import java.awt.Canvas; import javax.swing.JLabel; import javax.swing.ImageIcon; public class DiceGUI { private JFrame frame; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { DiceGUI window = new DiceGUI(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public DiceGUI() { initialize(); } private void initialize() { Dice fdice =new Dice(); Dice tdice =new Dice(); frame =new JFrame(); frame.setTitle("提问软件"); frame.setResizable(false); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon[] image = new ImageIcon[7]; image[0] = new ImageIcon("D:\\touzi\\0.gif"); image[1] = new ImageIcon("D:\\touzi\\1.png"); image[2] = new ImageIcon("D:\\touzi\\2.png"); image[3] = new ImageIcon("D:\\touzi\\3.png"); image[4] = new ImageIcon("D:\\touzi\\4.png"); image[5] = new ImageIcon("D:\\touzi\\5.png"); image[6] = new ImageIcon("D:\\touzi\\6.png"); JPanel panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); JLabel[] JDice = new JLabel[2]; JDice[0] = new JLabel(" "); JDice[0].setIcon(new ImageIcon("D:\\touzi\\0.gif")); JDice[0].setBounds(68, 104, 117, 111); panel.add(JDice[0]); JDice[1] = new JLabel(" "); JDice[1].setIcon(new ImageIcon("D:\\touzi\\0.gif")); JDice[1].setBounds(257, 104, 117, 111); panel.add(JDice[1]); JLabel pLabel = new JLabel(" "); pLabel.setVisible(false); pLabel.setBounds(189, 69, 255, 128); panel.add(pLabel); JButton usebutton = new JButton("抽取学生"); usebutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Dice[] Dice = new Dice[2]; int sum = 0; for(int i= 0;i < 2;i++){ Dice[i] = new Dice(); Dice[i].Roll(); System.out.println(Dice[i].getFaceValue()); sum = sum + Dice[i].getFaceValue(); switch(Dice[i].getFaceValue()){ case(1):JDice[i].setIcon(image[1]);break; case(2):JDice[i].setIcon(image[2]);break; case(3):JDice[i].setIcon(image[3]);break; case(4):JDice[i].setIcon(image[4]);break; case(5):JDice[i].setIcon(image[5]);break; case(6):JDice[i].setIcon(image[6]);break; } } System.out.println(sum + " 号同学上台回答问题"); } }); usebutton.setBounds(18, 225, 93, 23); panel.add(usebutton); JButton retbutton = new JButton("重置"); retbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDice[0].setIcon(image[0]); JDice[1].setIcon(image[0]); JDice[1].setVisible(true); pLabel.setVisible(false); } }); retbutton.setBounds(121, 225, 93, 23); panel.add(retbutton); JLabel lblNewLabel_1 = new JLabel(" "); ImageIcon title = new ImageIcon("D:\\touzi\\1.jpg"); title.setImage(title.getImage().getScaledInstance(300,80,Image.SCALE_DEFAULT)); lblNewLabel_1.setIcon(title); lblNewLabel_1.setBounds(67, 10, 307, 75); panel.add(lblNewLabel_1); JButton awbutton = new JButton("抽奖品"); awbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JDice[1].setVisible(false); pLabel.setVisible(true); fdice.Roll(); switch(fdice.getFaceValue()){ case(1):JDice[0].setIcon(image[1]);pLabel.setText("你获得了风扇");break; case(2):JDice[0].setIcon(image[2]);pLabel.setText("你获得了作业");break; case(3):JDice[0].setIcon(image[3]);pLabel.setText("你获得了零食");break; case(4):JDice[0].setIcon(image[4]);pLabel.setText("你获得了桌子");break; case(5):JDice[0].setIcon(image[5]);pLabel.setText("你获得了椅子");break; case(6):JDice[0].setIcon(image[6]);pLabel.setText("你获得了电脑");break; } } }); awbutton.setBounds(224, 225, 93, 23); panel.add(awbutton); } }
Kongzhitai.java
package touzi; import java.util.Scanner; import java.awt.EventQueue; public class Kongzhitai { public static void main(String[] args) { int i; Kongzhitai k = new Kongzhitai(); k.ChooseStudent(); System.out.println( "答对输入1,答错输入0:"); Scanner sc= new Scanner(System.in); i = sc.nextInt(); if(i==1){ k.ChooseAward(); } else System.out.println( "回去抄三遍"); } public Kongzhitai() { } private void ChooseStudent() { Dice[] Dice = new Dice[2]; int sum = 0; for(int i= 0;i < 2;i++){ Dice[i] = new Dice(); Dice[i].Roll(); System.out.println(Dice[i].getFaceValue()); sum = sum + Dice[i].getFaceValue(); System.out.println(sum + " 号同学上台回答问题"); } } private void ChooseAward() { Dice adice= new Dice(); adice.Roll(); switch(adice.getFaceValue()){ case(1): System.out.println("你获得了风扇");break; case(2): System.out.println("你获得了作业");break; case(3): System.out.println("你获得了零食");break; case(4): System.out.println("你获得了桌子");break; case(5): System.out.println("你获得了椅子");break; case(6): System.out.println("你获得了电脑");break; } }}
运行测试
开始是界面的显示,开始时是两个旋转的骰子
然后按抽取学生
如果回答正确的话抽取奖励
另外一个基于控制台的
课后小结:
学习到了关于界面的一些东西,虽然过程很痛苦,但结果应该是可喜的,希望自己能够再接再厉。