public Random() {
JFrame frame = new JFrame( "产生随机数窗口 ");
Container c = frame.getContentPane();
c.setLayout(new FlowLayout());
l1 = new JLabel( "请输入一个整数: ");
l1.setForeground(Color.green);
t1 = new JTextField(10);
b1 = new JButton( "随机数 ");
l2 = new JLabel( "结果为: ");
l2.setForeground(Color.red);
b1.setFont(new Font("my",0,20));
l1.setFont(new Font("my",0,20));
l2.setFont(new Font("my",0,20));
t2 = new JTextField(5);
t2.setForeground(Color.blue);
t2.setFont(new Font("my",0,20));
t1.setFont(new Font("my",0,20));
c.add(l1);
c.add(t1);
c.add(b1);
c.add(l2);
c.add(t2);
b1.addActionListener(new getRandom());
frame.setSize(800,80);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private class getRandom implements ActionListener {
public void actionPerformed(ActionEvent e) {
t2.setText(String.valueOf((int)(Math.random()*Integer.parseInt(t1.getText()))));
}
}
public static void main(String[] args) {
Random r = new Random();
}
public Random() {
JFrame frame = new JFrame("产生随机数窗口");
Container c = frame.getContentPane();
c.setLayout(new FlowLayout());
l1 = new JLabel("请输入一个整数:");
t1 = new JTextField(10);
b1 = new JButton("随机数");
l2 = new JLabel("结果为:");
t2 = new JTextField(5);
c.add(l1);
c.add(t1);
c.add(b1);
c.add(l2);
c.add(t2);
b1.addActionListener(new getRandom());
frame.setSize(500,80);
frame.setVisible(true);
}
private class getRandom implements ActionListener {
public void actionPerformed(ActionEvent e) {
t2.setText(String.valueOf((int)(Math.random()*Integer.parseInt(t1.getText()))));
}
}
public static void main(String[] args) {
Random r = new Random();
}