java 打乱排序 抽奖_用JAVA程序实现,随即抽奖的九个按钮,其中有一个有奖,每次随机出现一个有奖的按钮...

展开全部

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.WindowConstants;import javax.swing.SwingUtilities;

/**

* This code was edited or generated using CloudGarden's Jigloo

* SWT/Swing GUI Builder, which is free for non-commercial

* use. If Jigloo is being used commercially (ie, by a corporation,

* company or business for any purpose whatever) then you

* should purchase a license for each developer using Jigloo.

* Please visit www.cloudgarden.com for details.

* Use of Jigloo implies acceptance of these licensing terms.

* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR

* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED

* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.

*/

public class NewJFrame extends javax.swing.JFrame implements ActionListener {

private JPanel jPanel1;

private JButton jButton8;

private JTextField jt1;

private JLabel jLabel1;

private JButton jButton7;

private JButton jButton6;

private JButton jButton5;

private JButton jButton4;

private JButton jButton3;

private JButton jButton2;

private JButton jButton1;

private JButton jb1;

/**

* Auto-generated main method to display this JFrame

*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

NewJFrame inst = new NewJFrame();

inst.setLocationRelativeTo(null);

inst.setVisible(true);

}

});

}

private Integer genRes(){

Random rnd = new Random();

return new Integer(rnd.nextInt(8)+1);

}

private Integer res=new Integer(0);

public NewJFrame() {

super();

initGUI();

res=genRes();

jt1.setText(res+"");

}

private void initGUI() {

try {

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

{

jPanel1 = new JPanel();

getContentPane().add(jPanel1, BorderLayout.CENTER);

jPanel1.setPreferredSize(new java.awt.Dimension(338, 266));

jPanel1.setLayout(null);

{

jb1 = new JButton();

jPanel1.add(jb1);

jb1.setText("1");

jb1.setBounds(28, 46, 48, 27);

}

{

jButton1 = new JButton();

jPanel1.add(jButton1);

jButton1.setText("2");

jButton1.setBounds(134, 46, 48, 27);

}

{

jButton2 = new JButton();

jPanel1.add(jButton2);

jButton2.setText("3");

jButton2.setBounds(238, 46, 48, 27);

}

{

jButton3 = new JButton();

jPanel1.add(jButton3);

jButton3.setText("4");

jButton3.setBounds(28, 103, 48, 27);

}

{

jButton4 = new JButton();

jPanel1.add(jButton4);

jButton4.setText("5");

jButton4.setBounds(134, 103, 48, 27);

}

{

jButton5 = new JButton();

jPanel1.add(jButton5);

jButton5.setText("6");

jButton5.setBounds(238, 103, 48, 27);

}

{

jButton6 = new JButton();

jPanel1.add(jButton6);

jButton6.setText("7");

jButton6.setBounds(28, 159, 48, 27);

}

{

jButton7 = new JButton();

jPanel1.add(jButton7);

jButton7.setText("8");

jButton7.setBounds(134, 159, 48, 27);

}

{

jButton8 = new JButton();

jPanel1.add(jButton8);

jButton8.setText("9");

jButton8.setBounds(238, 159, 48, 27);

}

{

jLabel1 = new JLabel();

jPanel1.add(jLabel1);

jLabel1.setText("\u4e2d\u5956\u53f7\u7801");

jLabel1.setBounds(60, 211, 63, 31);

}

{

jt1 = new JTextField();

jPanel1.add(jt1);

jt1.setBounds(135, 213, 88, 29);

}

jButton1.addActionListener(this);

jButton2.addActionListener(this);

jButton3.addActionListener(this);

jButton4.addActionListener(this);

jButton5.addActionListener(this);

jButton6.addActionListener(this);

jButton7.addActionListener(this);

jButton8.addActionListener(this);

jb1.addActionListener(this);

}

pack();

setSize(400, 300);

} catch (Exception e) {

//add your error handling code here

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e) {

JButton jb=(JButton) e.getSource();

if(jb.getText().equals(res.toString())){

JOptionPane.showMessageDialog(this, "中奖e68a84e8a2ad3231313335323631343130323136353331333330363232了");

}else{

JOptionPane.showMessageDialog(this, "没中奖");

}

res=genRes();

jt1.setText(res+"");

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值