
package com.xd.gui
import java.awt.FlowLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JTextArea
public class RandomNum {
private JFrame frame
private JButton btn
private JTextArea text
public static void main(String[] args) {
new RandomNum()
}
public RandomNum() {
init()
}
public void init() {
frame=new JFrame()
btn =new JButton()
text=new JTextArea()
//设置组件属性
btn.setText("生成随机数")
text.setRows(5)
text.setColumns(20)
//设置流式布局,增加组件
frame.setLayout(new FlowLayout())
frame.add(btn)
frame.add(text)
//设判慧置窗体起始位置穗冲碰
frame.setBounds(600, 300, 300, 300)
//添加功能
fun ()
textFun()
//设置窗体可见
frame.setVisible(true)
}
public void fun() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText((int)(Math.random()*899999+100000)+"")
}
})
}
//文本框获得焦点显示6位随机数
public void textFun() {
text.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
text.setText((int)(Math.random()*899999+100000)+"")
}
@Override
public void focusLost(FocusEvent e) {
//失去猜谈焦点什么都不做
}
})
}
}
1、源代码如下
import java.util.*
public class Main
{
public static void main(String[] args) {
System.out.println("输入N:")
Scanner sc = new Scanner(System.in)
int N = sc.nextInt()
int start = 0
Random random = new Random()
for(int i = 0 i<慎镇银10i++){
//含旅顷义 ,需要0 - N,宽宴结果 就是 0+(N-0),需要3-N 就是3+(N-3)
int num = random.nextInt(N-start)+start
System.out.print(" "+num)
}
}
}
2、运行效果如图
这个是我写的一源正个产生0-100的随机数的程序,当然数的范围你可以自己定 Math.round(Math.random()*100),后雹雹悔面这个100你可以改成你自己想要的数
import javax.swing.*
import java.awt.event.*
public class RandomUsage extends JFrame
implements ActionListener
{
JButton bt=new JButton("随机数"肆早)
JLabel jt=new JLabel()
public RandomUsage()
{
this.setTitle("产生随机数")
this.setBounds(100,100,300,150)
this.setLayout(null)
this.add(bt)
bt.addActionListener(this)
bt.setBounds(20,20,80,50)
this.add(jt)
jt.setBounds(120,20,80,50)
this.setVisible(true)
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt)
{
jt.setText(String.valueOf(Math.round(Math.random()*100)))
}
}
public static void main(String args[])
{
new RandomUsage()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)