java第六次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java第六次作业相关的知识,希望对你有一定的参考价值。
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Insets; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashSet; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class choujiang extends JFrame { private JPanel imagePanel, p2; private ImageIcon background, anniu; private JButton kaishi, one, second, third, four, five, six; protected Object s; public static int b = 1; public JTextField jt; public String text="记录:\\n"; public void run() { System.out.println("1"); } public choujiang() { background = new ImageIcon("img/1.jpg");// 背景图片 anniu = new ImageIcon("img/2.png"); JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面 label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight()); // //把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明 imagePanel = (JPanel) this.getContentPane(); imagePanel.setOpaque(false); // //内容窗格默认的布局管理器为BorderLayout kaishi = new JButton(anniu); kaishi.setSize(45, 40); kaishi.setLocation(168, 249); kaishi.setBackground(Color.yellow); kaishi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(b<=3){ b++; t t = new t(); t.run(); Random r = new Random(); int a = r.nextInt(6) + 1; chushihua(); if (a == 1) { one.setBackground(Color.yellow); jt.setText("恭喜您获得了一等奖"); text=text+"恭喜您获得了一等奖\\n"; } else if (a == 2) { second.setBackground(Color.yellow); jt.setText("恭喜您获得了二等奖"); text=text+"恭喜您获得了二等奖\\n"; } else if (a == 3) { third.setBackground(Color.yellow); jt.setText("恭喜您获得了三等奖"); text=text+"恭喜您获得了三等奖\\n"; } else if (a == 4) { four.setBackground(Color.yellow); jt.setText("恭喜您获得了优秀奖"); text=text+"恭喜您获得了优秀奖\\n"; } else if (a == 5) { five.setBackground(Color.yellow); jt.setText("恭喜您获得了再抽一次"); text=text+"恭喜您获得了再抽一次\\n"; } else if (a == 6) { six.setBackground(Color.yellow); jt.setText("别灰心,下次加油"); text=text+"别灰心,下次加油\\n"; } }else{ JOptionPane.showMessageDialog(imagePanel, "您的抽奖次数已经用完请点击退出!","关闭!",JOptionPane.WARNING_MESSAGE); } } }); Random random = new Random(); Object[] values = new Object[6]; HashSet hashSet = new HashSet(); for(int i = 0;i < values.length;i++){ int number = random.nextInt(1000) + 1; hashSet.add(number); } values = hashSet.toArray(); one = new JButton("一"); one.setMargin(new Insets(0, 0, 0, 0)); one.setSize(30, 30); one.setLocation(108, 174); one.setBackground(Color.PINK); second = new JButton("二"); second.setMargin(new Insets(0, 0, 0, 0)); second.setSize(30, 30); second.setLocation(219, 162); second.setBackground(Color.PINK); third = new JButton("三"); third.setMargin(new Insets(0, 0, 0, 0)); third.setSize(30, 30); third.setLocation(305, 249); third.setBackground(Color.PINK); four = new JButton("四"); four.setMargin(new Insets(0, 0, 0, 0)); four.setSize(30, 30); four.setLocation(248, 332); four.setBackground(Color.PINK); five = new JButton("五"); five.setMargin(new Insets(0, 0, 0, 0)); five.setSize(30, 30); five.setLocation(128, 349); five.setBackground(Color.PINK); six = new JButton("六"); six.setMargin(new Insets(0, 0, 0, 0)); six.setSize(30, 30); six.setLocation(48, 259); six.setBackground(Color.PINK); jt = new JTextField(); jt.setSize(332, 32); jt.setLocation(36, 565); imagePanel.setLayout(null); imagePanel.add(kaishi); imagePanel.add(one); imagePanel.add(second); imagePanel.add(third); imagePanel.add(four); imagePanel.add(five); imagePanel.add(six); imagePanel.add(jt); this.getLayeredPane().setLayout(null); // 把背景图片添加到分层窗格的最底层作为背景 this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(background.getIconWidth(), background.getIconHeight()+42); this.setVisible(true); } public static void main(String[] args) { choujiang t = new choujiang(); t.setVisible(true); } public void chushihua(){ one.setBackground(Color.PINK); second.setBackground(Color.PINK); third.setBackground(Color.PINK); four.setBackground(Color.PINK); five.setBackground(Color.PINK); six.setBackground(Color.PINK); } public JTextField getJT(){ return jt; } } class t extends Thread { public void run() { try { sleep(1000); System.out.println("a"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
本周作业要求制作限定抽奖人数和不重复奖项的抽奖系统,由于我们组上学期的期末设计就是抽奖系统,所以本次作业只是进行了改进一部分代码,主要是添加了限定人数的代码。运行结果如图所示。
以上是关于java第六次作业的主要内容,如果未能解决你的问题,请参考以下文章