要求:是彩票生成器,点击按钮,随机生成一行彩票号码,再点击一下,接着显示第二组号码(总10行)
谢啦半天没有解决,点击按钮,10行就一下子全出来了~最后在网友的帮助想解决了,在此感谢!
package com.liqimo.lianxi03;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RandomFrame extends JFrame implements ActionListener
{
JButton jb;
JPanel jp;
JButton[][] jbn;
public static int n = 0;
public RandomFrame()
{
this.setTitle("彩票生成器");
this.setSize(500, 500);
this.setLocation(200, 300);
jp = new JPanel();
jp.setLayout(new GridLayout(10, 8));
jb = new JButton("随机生成按钮");
jb.addActionListener(this);
jbn = new JButton[10][8];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 8; j++)
{
jbn[i][j] = new JButton();
jp.add(jbn[i][j]);
}
}
this.add(jb, "North");
this.add(jp, "Center");
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args)
{
new RandomFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jb)
{
for (int i = n; i <= n; i++)
{
// int i = 1;
for (int j = 0; j < 8; j++)
{
if (j == 0)
{
jbn[i][0].setText(i + "");
}
else if (j == 7)
{
jbn[i][j].setBackground(Color.blue);
jbn[i][j].setText((int)(Math.random() * 33) + "");
}
else
{
jbn[i][j].setBackground(Color.yellow);
jbn[i][j].setText((int)(Math.random() * 33) + "");
}
}
}
if (n < 9)
{
n++;
}
else
{
n = 0;
}
}
}
}
运行效果图: