随机点名系统 java

随机点名系统 java,第1张

import java.awt.Font

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.ArrayList

import java.util.List

import java.util.Random

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

public class test_random extends JFrame implements ActionListener{

public static void main(String[] args) {

test_random t = new test_random()

t.init()

}

public void init(){

initListDate()

but = new JButton("开始点名")

but.setBounds(100,150,100,40)

but.addActionListener(this)

label = new JLabel("随机点名")

label.setBounds(60,20,300,40)

label.setFont(new Font("楷体",Font.BOLD,40))

show = new JLabel("")

show.setBounds(110,80,200,30)

show.setFont(new Font("楷体",Font.BOLD,30))

reset = new JButton("重新点名")

reset.setBounds(203,246,90,25)

reset.addActionListener(this)

add(but)

add(label)

add(show)

add(reset)

setLayout(null)

setVisible(true)

setResizable(false)

setBounds(100,100,300,300)

setTitle("点名")

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

public void initListDate(){

//巧银在这个地方添加姓名就可以了

list.add("刘天廷")

list.add("孔老二")

list.add("张三")

list.add("李四")

list.add("王悄指五")

list.add("麻六")

list.add("冯七孝运宴")

list.add("京八")

list.add("茅台九")

}

public void actionPerformed(ActionEvent e) {

/*确保每人选一次*/

Object obj = e.getSource()

if(obj == but){

if(list.size()==0){

JOptionPane.showMessageDialog(null,"没有人了!")

return

}else{

Random ran = new Random()

int num = ran.nextInt(list.size())

show.setText(list.get(num))

list.remove(num)

}

}

if(obj == reset){

show.setText("")

initListDate()

}

}

private JButton but

private JLabel label

private JLabel show

private JButton reset

private List<String>list = new ArrayList<String>()

}

随机函数那里肢喊携有问题了 ,修改如下

import java.awt.Button

import java.awt.Toolkit

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.ArrayList

import java.util.List

import javax.swing.*

public class Window extends JFrame {

List<历伏String>names = new ArrayList<String>()

Button but = new Button("随即点名")

JTextField jtf = new JTextField(15)

JLabel jl = new JLabel("姓名为:")

public Window(){

this.init()

this.addComponent()

this.addListener()

}

private void addListener() {

but.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

jtf.setText(names.get((int) (Math.random()*names.size())))

}

})

}

private void addComponent() {

JPanel p = new JPanel()

p.add(jl)

p.add(jtf)

p.add(but)

this.add(p)

}

private void init() {

this.setSize(400,400)

center(this)

this.setTitle("随即点名器"渗陵)

names.add("a")

names.add("b")

names.add("c")

names.add("d")

names.add("e")

}

public static void center(Window win) {

int width = win.getWidth()

int height = win.getHeight()

Toolkit tool = Toolkit.getDefaultToolkit()

int screenWidth = tool.getScreenSize().width

int screenHeight = tool.getScreenSize().height

win.setLocation((screenWidth - width) / 2, (screenHeight - height) / 2)

}

public static void main(String [] args){

JFrame jf = new Window()

jf.setVisible(true)

jf.setDefaultCloseOperation(EXIT_ON_CLOSE)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12354635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存