import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class _36x7a extends Frame {
public static void main(String[] args) {
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("36选7");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 5; i++) {
int[] result = randoms();
StringBuilder builder = new StringBuilder("第"+(i+1)+"注"+": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panel);
myFrame.pack();
myFrame.setSize(500, 300);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
这个是一个选择彩票的源码,我想再面板上增加一个功能就是输入数字后,就随机出现多少注.就是我输入"4"就出4注的功能.
13 个解决方案
#1
写个简单的,要比较好的话要自己在调节下。没有加滚动条。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TT extends Frame {
public static void main(String[] args) {
Panel panelAllPanel = new Panel(new GridLayout(2, 1));
Panel panelMenu = new Panel(new FlowLayout());
Label label = new Label("输入注数");
panelMenu.add(label);
final JTextField jt = new JTextField(10);
panelMenu.add(jt);
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("36选7");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jt.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入数值!");
}else
{
try
{
int num = Integer.valueOf(jt.getText().trim());
for (int i = 0; i < num; i++)
{
int[] result = randoms();
StringBuilder builder = new StringBuilder("第"+(i+1)+"注"+": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2)
{
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panelAllPanel);
panelAllPanel.add(panelMenu);
panelAllPanel.add(panel);
myFrame.pack();
myFrame.setSize(500, 300);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TT extends Frame {
public static void main(String[] args) {
Panel panelAllPanel = new Panel(new GridLayout(2, 1));
Panel panelMenu = new Panel(new FlowLayout());
Label label = new Label("输入注数");
panelMenu.add(label);
final JTextField jt = new JTextField(10);
panelMenu.add(jt);
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("36选7");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jt.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入数值!");
}else
{
try
{
int num = Integer.valueOf(jt.getText().trim());
for (int i = 0; i < num; i++)
{
int[] result = randoms();
StringBuilder builder = new StringBuilder("第"+(i+1)+"注"+": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2)
{
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panelAllPanel);
panelAllPanel.add(panelMenu);
panelAllPanel.add(panel);
myFrame.pack();
myFrame.setSize(500, 300);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#2
如果可以改一下输入数字的框框大小最好
可以吗?
搞好给分
可以吗?
搞好给分
#3
final JTextField jt = new JTextField(10);
这行,自己设大小。我的是10。看你要多少,自己改。
这行,自己设大小。我的是10。看你要多少,自己改。
#4
还有,我想应用到其他程序,能说说重要加了哪些东西?
#5
还是不行啊,改那里只是字符长度
并不是整个面板的长度,能改一下吗?
并不是整个面板的长度,能改一下吗?
#6
面板的高度修改,就是输入框所属的面板框高度修改
搞好继续追加分数
搞好继续追加分数
#7
程序完善了一下。
/**
* @author bzwm
*
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class _36x7a extends JFrame {
private final JLabel[] labels = new JLabel[] { new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel() };
private JLabel title = new JLabel("输入注数:");
private JTextField txt = new JTextField(6);
public _36x7a() {
super("Test");
Button button2 = new Button("36选7");
Font f = new Font("Dialog", Font.BOLD, 30);
button2.setFont(f);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
String input = txt.getText().trim();
if (input.matches("//d+") && Integer.parseInt(input) <= 5) {
JOptionPane.showMessageDialog(null, "请正确输入注数!");
} else {
try {
int num = Integer.valueOf(txt.getText().trim());
for (int i = 0; i < num; i++) {
int[] result = randoms();
StringBuilder builder = new StringBuilder("第" + (i + 1) + "注" + ": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
});
JPanel panel = new JPanel(new GridLayout(7, 1));
panel.add(button2);
JPanel inputPnl = new JPanel(new FlowLayout());
inputPnl.add(title);
inputPnl.add(txt);
panel.add(inputPnl);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
this.add(panel);
this.pack();
this.setSize(500, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new _36x7a();
}
public void clear() {
for (JLabel lbl : labels) {
lbl.setText("");
}
}
public int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = { 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36 };
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#8
首先,由于的你的类_36x7a继承了Frame,我改成继承自JFrame了。
而且,由于你的类本身就是一个JFrame,所以,在main函数里就没有必要再去重新定义一个Frame了。
用它自身就可以了。
所以我把你的main方法里的操作,都转移到_36x7a的构造方法里。
然后再main函数里,简单的 new _36x7a();就可以了。
JPanel panel = new JPanel(new GridLayout(7, 1));//这个是整个面板
panel.add(button2);//加入了你的按钮
JPanel inputPnl = new JPanel(new FlowLayout());//重新定义一个面板,打算装 新加入的 JLabel 和 输入框
inputPnl.add(title); //追加到面板
inputPnl.add(txt);//追加到面板
panel.add(inputPnl);//把我们新加的面板加入到主面板中去。
还加入了一个方法:
clear()
他用来清空原来的字符串,
否则的,如果第一个此你输入4,
然后点击了按钮,
第二次再输入2,然后点击按钮,
你会发现,还是4个注数。
#9
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class _ssq extends Frame {
public static void main(String[] args) {
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("双色球");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 5; i++) {
int[] result = randoms();
int[] result2 = randoms2();
StringBuilder builder = new StringBuilder((i+1)+": 红球:");
for (int j = 0; j < 6; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
builder.append("蓝球:"+result2[0]);
labels[i].setText(builder.toString());
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panel);
myFrame.pack();
myFrame.setSize(600, 400);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33};
int len = send.length;
int returnValue[] = new int[33];
for (int i = 0; i < 33; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
public static int[] randoms2() {
Random r = new Random();
int temp1, temp2;
int send[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int len = send.length;
int returnValue[] = new int[16];
for (int i = 0; i < 16; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
可以多改这个吗?
#10
/**
* @author bzwm
*
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class _ssq extends JFrame {
private static JLabel title = new JLabel("输入注数:");
static final JLabel[] labels = new JLabel[] { new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel() };
private static JTextField txt = new JTextField(6);
public static void main(String[] args) {
Button button2 = new Button("双色球");
Font f = new Font("Dialog", Font.BOLD, 30);
button2.setFont(f);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
String input = txt.getText().trim();
if (input.matches("//d+") && Integer.parseInt(input) <= 5) {
JOptionPane.showMessageDialog(null, "请正确输入注数!");
} else {
int num = Integer.valueOf(txt.getText().trim());
for (int i = 0; i < num; i++) {
int[] result = randoms();
int[] result2 = randoms2();
StringBuilder builder = new StringBuilder((i + 1) + ": 红球:");
for (int j = 0; j < 6; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
builder.append("蓝球:" + result2[0]);
labels[i].setText(builder.toString());
}
}
}
});
final JFrame myFrame = new JFrame("Test");
Panel panel = new Panel(new GridLayout(7, 1));
panel.add(button2);
JPanel inputPnl = new JPanel(new FlowLayout());
inputPnl.add(title);
inputPnl.add(txt);
panel.add(inputPnl);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panel);
myFrame.pack();
myFrame.setSize(600, 400);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = { 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33 };
int len = send.length;
int returnValue[] = new int[33];
for (int i = 0; i < 33; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
public static void clear() {
for (JLabel lbl : labels) {
lbl.setText("");
}
}
public static int[] randoms2() {
Random r = new Random();
int temp1, temp2;
int send[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
int len = send.length;
int returnValue[] = new int[16];
for (int i = 0; i < 16; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#11
我发的两个代码都有个地方搞错了。
就是对输入数字的判断,
改成:
if (!input.matches("\\d+") && Integer.parseInt(input) <= 5)
就是对输入数字的判断,
改成:
if (!input.matches("\\d+") && Integer.parseInt(input) <= 5)
#12
用WindowBuilder或VE做swing的界面设计吧.比较简单.
#13
选号码这一部分应该提取出来。
import javax.swing.*;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class NumberSelectorFrame extends JFrame {
private JLabel[] labels = new JLabel[5];
private JButton button = new JButton("36 选 7");
private Font font = new Font("Dialog", Font.BOLD, 30);
private NumberSelector selector = new NumberSelector();
public NumberSelectorFrame() throws HeadlessException {
initFrame();
initControls();
initButtonAction();
}
private void initFrame() {
this.setSize(500, 300);
this.setTitle("36 选 7");
this.setResizable(false);
this.setLayout(new GridLayout(6, 1));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initButtonAction() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[][] selections = selector.select();
for (int i = 0; i < labels.length; i++) {
JLabel label = labels[i];
label.setText(getText(i, selections[i]));
}
}
});
}
private String getText(int i, int[] selection) {
return "第 " + (i + 1) + " 注:" + Arrays.toString(selection);
}
private void initControls() {
button.setFont(font);
getContentPane().add(button);
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel();
labels[i].setFont(font);
getContentPane().add(labels[i]);
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
NumberSelectorFrame frame = new NumberSelectorFrame();
frame.setVisible(true);
}
}
// 选号码的类
class NumberSelector {
private List<Integer> numbers = new ArrayList<Integer>();
private Random r = new Random();
NumberSelector() {
for (int i = 1; i < 36; i++) {
numbers.add(i);
}
}
public int[][] select() {
int[][] result = new int[5][];
for (int i = 0; i < result.length; i++) {
result[i] = selectOnce();
}
return result;
}
private int[] selectOnce() {
List<Integer> numbers = new ArrayList<Integer>(this.numbers); // 拷贝一个 List
int[] result = new int[7];
for (int i = 0; i < result.length; i++) {
int index = r.nextInt(numbers.size());
result[i] = numbers.get(index);
numbers.remove(index);
}
return result;
}
}
#1
写个简单的,要比较好的话要自己在调节下。没有加滚动条。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TT extends Frame {
public static void main(String[] args) {
Panel panelAllPanel = new Panel(new GridLayout(2, 1));
Panel panelMenu = new Panel(new FlowLayout());
Label label = new Label("输入注数");
panelMenu.add(label);
final JTextField jt = new JTextField(10);
panelMenu.add(jt);
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("36选7");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jt.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入数值!");
}else
{
try
{
int num = Integer.valueOf(jt.getText().trim());
for (int i = 0; i < num; i++)
{
int[] result = randoms();
StringBuilder builder = new StringBuilder("第"+(i+1)+"注"+": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2)
{
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panelAllPanel);
panelAllPanel.add(panelMenu);
panelAllPanel.add(panel);
myFrame.pack();
myFrame.setSize(500, 300);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TT extends Frame {
public static void main(String[] args) {
Panel panelAllPanel = new Panel(new GridLayout(2, 1));
Panel panelMenu = new Panel(new FlowLayout());
Label label = new Label("输入注数");
panelMenu.add(label);
final JTextField jt = new JTextField(10);
panelMenu.add(jt);
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("36选7");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jt.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "请输入数值!");
}else
{
try
{
int num = Integer.valueOf(jt.getText().trim());
for (int i = 0; i < num; i++)
{
int[] result = randoms();
StringBuilder builder = new StringBuilder("第"+(i+1)+"注"+": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2)
{
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panelAllPanel);
panelAllPanel.add(panelMenu);
panelAllPanel.add(panel);
myFrame.pack();
myFrame.setSize(500, 300);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#2
如果可以改一下输入数字的框框大小最好
可以吗?
搞好给分
可以吗?
搞好给分
#3
final JTextField jt = new JTextField(10);
这行,自己设大小。我的是10。看你要多少,自己改。
这行,自己设大小。我的是10。看你要多少,自己改。
#4
还有,我想应用到其他程序,能说说重要加了哪些东西?
#5
还是不行啊,改那里只是字符长度
并不是整个面板的长度,能改一下吗?
并不是整个面板的长度,能改一下吗?
#6
面板的高度修改,就是输入框所属的面板框高度修改
搞好继续追加分数
搞好继续追加分数
#7
程序完善了一下。
/**
* @author bzwm
*
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class _36x7a extends JFrame {
private final JLabel[] labels = new JLabel[] { new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel() };
private JLabel title = new JLabel("输入注数:");
private JTextField txt = new JTextField(6);
public _36x7a() {
super("Test");
Button button2 = new Button("36选7");
Font f = new Font("Dialog", Font.BOLD, 30);
button2.setFont(f);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
String input = txt.getText().trim();
if (input.matches("//d+") && Integer.parseInt(input) <= 5) {
JOptionPane.showMessageDialog(null, "请正确输入注数!");
} else {
try {
int num = Integer.valueOf(txt.getText().trim());
for (int i = 0; i < num; i++) {
int[] result = randoms();
StringBuilder builder = new StringBuilder("第" + (i + 1) + "注" + ": ");
for (int j = 0; j < 7; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
labels[i].setText(builder.toString());
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "请输入整数!");
}
}
}
});
JPanel panel = new JPanel(new GridLayout(7, 1));
panel.add(button2);
JPanel inputPnl = new JPanel(new FlowLayout());
inputPnl.add(title);
inputPnl.add(txt);
panel.add(inputPnl);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
this.add(panel);
this.pack();
this.setSize(500, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new _36x7a();
}
public void clear() {
for (JLabel lbl : labels) {
lbl.setText("");
}
}
public int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = { 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36 };
int len = send.length;
int returnValue[] = new int[36];
for (int i = 0; i < 7; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#8
首先,由于的你的类_36x7a继承了Frame,我改成继承自JFrame了。
而且,由于你的类本身就是一个JFrame,所以,在main函数里就没有必要再去重新定义一个Frame了。
用它自身就可以了。
所以我把你的main方法里的操作,都转移到_36x7a的构造方法里。
然后再main函数里,简单的 new _36x7a();就可以了。
JPanel panel = new JPanel(new GridLayout(7, 1));//这个是整个面板
panel.add(button2);//加入了你的按钮
JPanel inputPnl = new JPanel(new FlowLayout());//重新定义一个面板,打算装 新加入的 JLabel 和 输入框
inputPnl.add(title); //追加到面板
inputPnl.add(txt);//追加到面板
panel.add(inputPnl);//把我们新加的面板加入到主面板中去。
还加入了一个方法:
clear()
他用来清空原来的字符串,
否则的,如果第一个此你输入4,
然后点击了按钮,
第二次再输入2,然后点击按钮,
你会发现,还是4个注数。
#9
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class _ssq extends Frame {
public static void main(String[] args) {
final Label[] labels = new Label[]{new Label(), new Label(), new Label(), new Label(), new Label()};
Button button2 = new Button("双色球");
Font f=new Font( "Dialog", Font.BOLD, 30 ) ;
button2.setFont(f);
button2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 5; i++) {
int[] result = randoms();
int[] result2 = randoms2();
StringBuilder builder = new StringBuilder((i+1)+": 红球:");
for (int j = 0; j < 6; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
builder.append("蓝球:"+result2[0]);
labels[i].setText(builder.toString());
}
}
}
);
final Frame myFrame = new Frame("Test");
Panel panel = new Panel(new GridLayout(6, 1));
panel.add(button2);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panel);
myFrame.pack();
myFrame.setSize(600, 400);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = {22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,23,24,25,26,27,28,29,30,31,32,33};
int len = send.length;
int returnValue[] = new int[33];
for (int i = 0; i < 33; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
public static int[] randoms2() {
Random r = new Random();
int temp1, temp2;
int send[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int len = send.length;
int returnValue[] = new int[16];
for (int i = 0; i < 16; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
可以多改这个吗?
#10
/**
* @author bzwm
*
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class _ssq extends JFrame {
private static JLabel title = new JLabel("输入注数:");
static final JLabel[] labels = new JLabel[] { new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel() };
private static JTextField txt = new JTextField(6);
public static void main(String[] args) {
Button button2 = new Button("双色球");
Font f = new Font("Dialog", Font.BOLD, 30);
button2.setFont(f);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
String input = txt.getText().trim();
if (input.matches("//d+") && Integer.parseInt(input) <= 5) {
JOptionPane.showMessageDialog(null, "请正确输入注数!");
} else {
int num = Integer.valueOf(txt.getText().trim());
for (int i = 0; i < num; i++) {
int[] result = randoms();
int[] result2 = randoms2();
StringBuilder builder = new StringBuilder((i + 1) + ": 红球:");
for (int j = 0; j < 6; j++) {
builder.append(String.valueOf(result[j]) + " ");
}
builder.append("蓝球:" + result2[0]);
labels[i].setText(builder.toString());
}
}
}
});
final JFrame myFrame = new JFrame("Test");
Panel panel = new Panel(new GridLayout(7, 1));
panel.add(button2);
JPanel inputPnl = new JPanel(new FlowLayout());
inputPnl.add(title);
inputPnl.add(txt);
panel.add(inputPnl);
for (int i = 0; i < 5; i++) {
panel.add(labels[i], BorderLayout.CENTER);
labels[i].setFont(f);
}
myFrame.add(panel);
myFrame.pack();
myFrame.setSize(600, 400);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
myFrame.setVisible(false);
System.exit(0);
}
});
}
public static int[] randoms() {
Random r = new Random();
int temp1, temp2;
int send[] = { 22, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33 };
int len = send.length;
int returnValue[] = new int[33];
for (int i = 0; i < 33; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
public static void clear() {
for (JLabel lbl : labels) {
lbl.setText("");
}
}
public static int[] randoms2() {
Random r = new Random();
int temp1, temp2;
int send[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
int len = send.length;
int returnValue[] = new int[16];
for (int i = 0; i < 16; i++) {
temp1 = Math.abs(r.nextInt()) % len;
returnValue[i] = send[temp1];
temp2 = send[temp1];
send[temp1] = send[len - 1];
send[len - 1] = temp2;
len--;
}
return returnValue;
}
}
#11
我发的两个代码都有个地方搞错了。
就是对输入数字的判断,
改成:
if (!input.matches("\\d+") && Integer.parseInt(input) <= 5)
就是对输入数字的判断,
改成:
if (!input.matches("\\d+") && Integer.parseInt(input) <= 5)
#12
用WindowBuilder或VE做swing的界面设计吧.比较简单.
#13
选号码这一部分应该提取出来。
import javax.swing.*;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class NumberSelectorFrame extends JFrame {
private JLabel[] labels = new JLabel[5];
private JButton button = new JButton("36 选 7");
private Font font = new Font("Dialog", Font.BOLD, 30);
private NumberSelector selector = new NumberSelector();
public NumberSelectorFrame() throws HeadlessException {
initFrame();
initControls();
initButtonAction();
}
private void initFrame() {
this.setSize(500, 300);
this.setTitle("36 选 7");
this.setResizable(false);
this.setLayout(new GridLayout(6, 1));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initButtonAction() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[][] selections = selector.select();
for (int i = 0; i < labels.length; i++) {
JLabel label = labels[i];
label.setText(getText(i, selections[i]));
}
}
});
}
private String getText(int i, int[] selection) {
return "第 " + (i + 1) + " 注:" + Arrays.toString(selection);
}
private void initControls() {
button.setFont(font);
getContentPane().add(button);
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel();
labels[i].setFont(font);
getContentPane().add(labels[i]);
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
NumberSelectorFrame frame = new NumberSelectorFrame();
frame.setVisible(true);
}
}
// 选号码的类
class NumberSelector {
private List<Integer> numbers = new ArrayList<Integer>();
private Random r = new Random();
NumberSelector() {
for (int i = 1; i < 36; i++) {
numbers.add(i);
}
}
public int[][] select() {
int[][] result = new int[5][];
for (int i = 0; i < result.length; i++) {
result[i] = selectOnce();
}
return result;
}
private int[] selectOnce() {
List<Integer> numbers = new ArrayList<Integer>(this.numbers); // 拷贝一个 List
int[] result = new int[7];
for (int i = 0; i < result.length; i++) {
int index = r.nextInt(numbers.size());
result[i] = numbers.get(index);
numbers.remove(index);
}
return result;
}
}