
c#中怎样给一个button按钮的位置赋一个随机数
展开全部
import java.awt.FlowLayout;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestRandom extends JFrame {
JTextField field;
JButton randomButton;
RandomNumber t = new RandomNumber();
public TestRandom() {
field = new JTextField(15);
randomButton = new JButton("产生随机数");
randomButton.addActionListener(new RandomListener());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(field);
this.getContentPane().add(randomButton);
this.setSize(300,100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String actionCommand = randomButton.getText();
if(actionCommand.equals("产生随机数")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("产生随机数");
t.stop = true;
}
}
}
class RandomNumber implements Runnable {
Random random;
StringBuffer num;
boolean stop = false;
public RandomNumber() {
random = new Random();
num = new StringBuffer("");
}
public void run() {
while(!stop) {
num.setLength(0);
for(int i=0;i<6;i++) {
num.append(random.nextInt(9));
}
field.setText(num.toString());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TestRandom();
}
}
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.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestRandom extends JFrame {
JTextField field;
JButton randomButton;
RandomNumber t = new RandomNumber();
public TestRandom() {
field = new JTextField(15);
randomButton = new JButton("产生随机数");
randomButton.addActionListener(new RandomListener());
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(field);
this.getContentPane().add(randomButton);
this.setSize(300,100);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
class RandomListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String actionCommand = randomButton.getText();
if(actionCommand.equals("产生随机数")) {
randomButton.setText("停止");
t.stop = false;
new Thread(t).start();
} else if(actionCommand.equals("停止")) {
randomButton.setText("产生随机数");
t.stop = true;
}
}
}
class RandomNumber implements Runnable {
Random random;
StringBuffer num;
boolean stop = false;
public RandomNumber() {
random = new Random();
num = new StringBuffer("");
}
public void run() {
while(!stop) {
num.setLength(0);
for(int i=0;i<6;i++) {
num.append(random.nextInt(9));
}
field.setText(num.toString());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new TestRandom();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Random random=new Random();
int left=random.Rext(500);
int top=random.Next(500);//500是随机范围
button1.Left=left;
button1.Top=top;
int left=random.Rext(500);
int top=random.Next(500);//500是随机范围
button1.Left=left;
button1.Top=top;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int X=new Random().Next(0,int.MaxValue);
int Y=new Random().Next(0,int.MaxValue);
btnOCO.Location = new Point(X, Y);
int Y=new Random().Next(0,int.MaxValue);
btnOCO.Location = new Point(X, Y);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
locationg属性,通过不同的坐标来定位
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询