再來一篇新文章。今天我試了很久,終於將書上教的GUI和一個ArrayList 集合物件的方法弄成一個程式。

就是上面這個。小小的一個程式。不過卻很興奮,因為書里也沒寫這個的範例。不過到是書上教的方法的應用。
不過寫的不算很好啦。有待加強…。下次有空再來弄別的。
/**
*
* @author XUN
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Lotto {
JFrame myframe=new JFrame("樂透/作者不負沒中之責任");
JButton ff=new JButton("秀出號碼");
JTextField hf=new JTextField();
public static void main(String[] args) {
// TODO code application logic here
Lotto test =new Lotto();
}
public Lotto(){
Container contentPane=myframe.getContentPane();
contentPane.add(new JLabel("java版/使用ArrayList"),BorderLayout.NORTH);
contentPane.add(ff,BorderLayout.CENTER);
contentPane.add(hf,BorderLayout.SOUTH);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setBounds(384, 250, 400, 100);
myframe.setVisible(true);
ff.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
ArrayList<Integer> num=new ArrayList<Integer>();
for(int i=1;i<50;i++)
num.add(new Integer(i));
for(int i=1;i<=1;i++)
Collections.shuffle(num);
hf.setText("六個隨機號碼:"+num.subList(0, 6)); //號碼的長度可由此修改
}
}
);
}
}