package LottoProject; import java.awt.*; import javax.swing.*; class LottoEx { public static void main(String[] args) { /* * NumberArr 객체생성. * 각 액션리스너에서 동일한 배열값을 사용하기 위한 작업. */ NumberArr arr=new NumberArr(); //label배열생성 JLabel lottoExframe_label[] = new JLabel[10]; for(int i=0;i<10;i++){ lottoExframe_label[i] = new JLabel(); } //lotto버튼 눌렀을때 사용하는 label JLabel lottoExframe_lotto_label[] = new JLabel[2]; lottoExframe_lotto_label[0]=new JLabel(); lottoExframe_lotto_label[1]=new JLabel(); /* * A~E 5개의 Checkbox 프레임 생성 * Checkbox 생성하면, makeCheckbox()메소드 실행 → 창 생성 * 창은 이미 생성되었으나 setVisible(false)로 되어있어서 보이지 않음 */ Checkbox A = new Checkbox("A",0,arr,lottoExframe_label); Checkbox B = new Checkbox("B",1,arr,lottoExframe_label); Checkbox C = new Checkbox("C",2,arr,lottoExframe_label); Checkbox D = new Checkbox("D",3,arr,lottoExframe_label); Checkbox E = new Checkbox("E",4,arr,lottoExframe_label); // 메인 프레임 구성 JFrame lottoExframe = new JFrame("로또 실행 프로그램"); lottoExframe.setPreferredSize(new Dimension(400, 220)); lottoExframe.setLocation(710, 200); //contentPane생성 Container contentPane = lottoExframe.getContentPane(); // panel1, panel1_2생성 JPanel lottoExframe_panel1 = new JPanel(); JPanel lottoExframe_panel1_2 = new JPanel(); // 폰트에 색을 입히는 예제 //lottoExframe_label1.setText("<html><font color=red><b>Lotto</b></font>Number</html>"); // A~E버튼, Lotto버튼 생성 JButton lottoExframe_button1 = new JButton("A"); JButton lottoExframe_button2 = new JButton("B"); JButton lottoExframe_button3 = new JButton("C"); JButton lottoExframe_button4 = new JButton("D"); JButton lottoExframe_button5 = new JButton("E"); JButton lottoExframe_buttonfinal = new JButton("Lotto"); JButton lottoExframe_buttonclear = new JButton("Clear"); // panel1에 생성한 버튼 추가 lottoExframe_panel1.add(lottoExframe_button1); lottoExframe_panel1.add(lottoExframe_button2); lottoExframe_panel1.add(lottoExframe_button3); lottoExframe_panel1.add(lottoExframe_button4); lottoExframe_panel1.add(lottoExframe_button5); lottoExframe_panel1.add(lottoExframe_buttonfinal); lottoExframe_panel1.add(lottoExframe_buttonclear); // contentPane에 panel 추가. panel1은 NORTH, panel1_2는 CENTER. contentPane.add(lottoExframe_panel1,BorderLayout.NORTH); contentPane.add(lottoExframe_panel1_2,BorderLayout.CENTER); // panel1_2 레이아웃 지정-GridLayout lottoExframe_panel1_2.setLayout(new GridLayout(6, 2)); // panel1_2 label[i] 추가 for(int i=0;i<10;i++){ lottoExframe_panel1_2.add(lottoExframe_label[i]); } //lottoExframe_panel1_2.add(lottoExframe_label1); //각 버튼에 대한 ActionListener추가 lottoExframe_button1.addActionListener(new SelectActionListener(A)); lottoExframe_button2.addActionListener(new SelectActionListener(B)); lottoExframe_button3.addActionListener(new SelectActionListener(C)); lottoExframe_button4.addActionListener(new SelectActionListener(D)); lottoExframe_button5.addActionListener(new SelectActionListener(E)); lottoExframe_buttonfinal.addActionListener(new LottoActionListener(lottoExframe_panel1_2,lottoExframe_label,lottoExframe_lotto_label,arr)); lottoExframe_buttonclear.addActionListener(new ClearActionListener(lottoExframe_label,lottoExframe_lotto_label,arr)); // 프레임 보여주기 부분 lottoExframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lottoExframe.pack(); lottoExframe.setVisible(true); } }
package LottoProject; class LottoP { String numtoString(int arr[]) { String str=""; for (int i = 0; i < arr.length; i++) { str += arr[i] + " "; } return str; } // 버블정렬 메소드 void bubbleSort(int arr[]){ int temp = 0; for (int j=0;j<arr.length-1;j++) { for (int i=0;i<arr.length-1;i++) { if (arr[i]>arr[i+1]){ temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } } } }
package LottoProject; class NumberArr { int no[][] = new int [6][6]; /* * no[i][j] 형식으로 배열 선언 * i부분은 A B C E D Lotto를 의미 * j부분은 각각 6자리의 숫자저장공간 */ }
package LottoProject; import java.awt.*; import javax.swing.*; class Checkbox { int checkValue[] = new int[45]; //check값을 저장하는 배열 JCheckBox ck[]= new JCheckBox[45]; //checkbox 생성 String name; int x; boolean onoff=false; JLabel[] label; int close=0; // 창을 하나만 띄우기 위한 조건 int변수 NumberArr arr; // 배열이 있는 객체 생성 Checkbox(String name, int x, NumberArr arr, JLabel[] label){ this.name=name; this.x=x; this.arr=arr; this.label=label; makeCheckbox(); } void makeCheckbox() { //프레임 구성 JFrame myNumberframe = new JFrame("나눔로또("+name+")"); myNumberframe.setLocation(280+(240*x), 420); myNumberframe.setPreferredSize(new Dimension(240,360)); Container contentPane = myNumberframe.getContentPane(); JPanel myNumberframe_panel1 = new JPanel(); JPanel myNumberframe_panel2 = new JPanel(); JButton myNumberframe_button1 = new JButton("직접"); JButton myNumberframe_button2 = new JButton("자동"); JButton myNumberframe_button3 = new JButton("취소"); //체크박스 생성. 값은 checkValue[]에 저장 for (int i=0;i<45;i++){ checkValue[i]=i+1; if(i<9){ ck[i]=new JCheckBox("0"+(i+1)); }else{ ck[i]=new JCheckBox((i+1)+""); } } for (int i=0;i<45;i++){ myNumberframe_panel1.add(ck[i]); } //panel2에 버튼 3개 추가 myNumberframe_panel2.add(myNumberframe_button1); myNumberframe_panel2.add(myNumberframe_button2); myNumberframe_panel2.add(myNumberframe_button3); //각 버튼 액션 myNumberframe_button1.addActionListener(new InsertActionListener(myNumberframe,this,arr,x,label)); myNumberframe_button2.addActionListener(new RandomActionListener(arr,label,x,this,myNumberframe)); myNumberframe_button3.addActionListener(new CloseActionListener(myNumberframe,this)); //contentPane에 panel 추가(레이아웃지정) contentPane.add(myNumberframe_panel1,BorderLayout.CENTER); contentPane.add(myNumberframe_panel2,BorderLayout.SOUTH); //myNumberframe.setDefaultCloseOperation(new CloseActionListener(myNumberframe,this)); myNumberframe.pack(); myNumberframe.setVisible(onoff); } }
package LottoProject; import java.awt.event.*; import javax.swing.*; class SelectActionListener implements ActionListener { Checkbox ch; JFrame f; //생성자로 이벤트에 사용할 모든 값을 받아온다 SelectActionListener(Checkbox ch){ this.ch=ch; } @Override //ActionListener 인터페이스를 implements할 때 오버라이딩해야하는 메소드 public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(ch.close<1){ ch.onoff=true; ch.makeCheckbox(); ch.close+=1; } else{ JOptionPane.showMessageDialog(f,"창이 이미 열려있습니다.", "에러메세지", JOptionPane.ERROR_MESSAGE); } } }
package LottoProject; import java.awt.event.*; import javax.swing.*; class InsertActionListener implements ActionListener { LottoP p = new LottoP(); Checkbox ch; JFrame f = new JFrame(); String str=""; int x; NumberArr arr; JLabel[] label; InsertActionListener(JFrame f, Checkbox ch, NumberArr arr, int x, JLabel[] label){ this.f=f; this.ch=ch; this.arr=arr; this.x=x; this.label=label; } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub for(int i=0;i<arr.no.length;i++){ arr.no[x][i]=0; } int j=0; for(int i=0;i<45;i++){ if(ch.ck[i].isSelected()==true){ //checkbox에 check가 되어있다면... try{ arr.no[x][j]=ch.checkValue[i]; //checkValue배열에 저장되어있는 값을 arr.no배열로 옮겨옴 j++; }catch (Exception e) { //check값이 6개를 초과할경우 에러메세지 출력 JOptionPane.showMessageDialog(f, "숫자는 6개만 체크해주세요", "에러메세지", JOptionPane.ERROR_MESSAGE); return; } } } if(j<6){ //check 값이 6개 미만일 경우 에러메세지 출력 JOptionPane.showMessageDialog(f, "숫자를 6개 체크해주세요", "에러메세지", JOptionPane.ERROR_MESSAGE); System.out.println("Error"); return; } p.bubbleSort(arr.no[x]);//Insert 값을 정렬 String str=p.numtoString(arr.no[x]); //num값을 String으로 저장 /* * Label에 쓰는 부분 */ if(x==0){ label[0].setText(" A Number"); label[1].setText(str); }else if(x==1){ label[2].setText(" B Number"); label[3].setText(str); }else if(x==2){ label[4].setText(" C Number"); label[5].setText(str); }else if(x==3){ label[6].setText(" D Number"); label[7].setText(str); }else if(x==4){ label[8].setText(" E Number"); label[9].setText(str); } f.setVisible(false); ch.close-=1; } }
package LottoProject; import java.awt.event.*; import java.util.*; import javax.swing.*; class RandomActionListener implements ActionListener { LottoP p = new LottoP(); NumberArr arr; Random lottoNum = new Random(); JLabel[] label; JFrame f; int x; Checkbox ch; // 생성자로 이벤트에 사용할 모든 값을 받아온다 RandomActionListener(NumberArr arr, JLabel[] label, int x, Checkbox ch, JFrame f) { this.arr=arr; this.label=label; this.x=x; this.ch=ch; this.f=f; } @Override // ActionListener 인터페이스를 implements할 때 오버라이딩해야하는 메소드 public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub for (int i = 0; i < arr.no.length; i++) { arr.no[x][i] = lottoNum.nextInt(45) + 1;// 0~44까지 난수를 발생시켜서 1을 더함 while (true) { boolean dupl = false;// 중복을 검사하는 변수 for (int j = 0; j < i; j++) { if (arr.no[x][i] == arr.no[x][j]) { dupl = true;// 중복이 발생하면 true로 변환 } } if (dupl == true) { arr.no[x][i] = lottoNum.nextInt(45) + 1; } else { break;// 중복값이 없으면 while문을 빠져나감 } } } p.bubbleSort(arr.no[x]);//Random 값을 정렬 String str=p.numtoString(arr.no[x]); //num값을 String으로 저장 /* * Label에 쓰는 부분 */ if(x==0){ label[0].setText(" A Number"); label[1].setText(str); }else if(x==1){ label[2].setText(" B Number"); label[3].setText(str); }else if(x==2){ label[4].setText(" C Number"); label[5].setText(str); }else if(x==3){ label[6].setText(" D Number"); label[7].setText(str); }else if(x==4){ label[8].setText(" E Number"); label[9].setText(str); } f.setVisible(false); ch.close-=1; } }
package LottoProject; import java.awt.event.*; import javax.swing.*; class CloseActionListener implements ActionListener { JFrame f; Checkbox ch; //생성자로 이벤트에 사용할 모든 값을 받아온다 CloseActionListener(JFrame f,Checkbox ch){ this.f=f; this.ch=ch; } @Override //ActionListener 인터페이스를 implements할 때 오버라이딩해야하는 메소드 public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub f.setVisible(false); ch.close-=1; } }
package LottoProject; import java.awt.event.*; import javax.swing.*; class CloseActionListener implements ActionListener { JFrame f; Checkbox ch; //생성자로 이벤트에 사용할 모든 값을 받아온다 CloseActionListener(JFrame f,Checkbox ch){ this.f=f; this.ch=ch; } @Override //ActionListener 인터페이스를 implements할 때 오버라이딩해야하는 메소드 public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub f.setVisible(false); ch.close-=1; } }
package LottoProject; import java.awt.event.*; import javax.swing.*; public class ClearActionListener implements ActionListener { JLabel label[]; JLabel label2[]; NumberArr arr; ClearActionListener(JLabel label[], JLabel label2[], NumberArr arr) { this.label = label; this.label2 = label2; this.arr=arr; } public void actionPerformed(ActionEvent arg0) { /* * Label에 쓰는 부분 */ for (int i = 0; i < label.length; i++) { label[i].setText(""); } for (int i = 0; i < label2.length; i++) { label2[i].setText(""); } //배열초기화 for(int i=0;i<arr.no.length;i++){ for(int j=0;j<arr.no[0].length;j++){ arr.no[i][j]=0; } } } }
package LottoProject; import java.awt.event.*; import java.util.Random; import javax.swing.*; class LottoActionListener implements ActionListener { JLabel[] label,label2; JPanel panel1_2; String str=""; NumberArr arr; Random lottoNum = new Random(); //생성자로 이벤트에 사용할 모든 값을 받아온다 LottoActionListener(JPanel panel1_2, JLabel[] label, JLabel[] label2, NumberArr arr){ this.panel1_2=panel1_2; this.label=label; this.label2=label2; this.arr=arr; } @Override //ActionListener 인터페이스를 implements할 때 오버라이딩해야하는 메소드 public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub str = ""; for (int i = 0; i < arr.no.length; i++) { arr.no[5][i] = lottoNum.nextInt(45) + 1;// 0~44까지 난수를 발생시켜서 1을 더함 while (true) { boolean dupl = false;// 중복을 검사하는 변수 for (int j = 0; j < i; j++) { if (arr.no[5][i] == arr.no[5][j]) { dupl = true;// 중복이 발생하면 true로 변환 } } if (dupl == true) { arr.no[5][i] = lottoNum.nextInt(45) + 1; } else { break;// 중복값이 없으면 while문을 빠져나감 } } } label2[0].setText("<html><font color=red><b> Lotto</b></font>Number</html>"); panel1_2.add(label2[0]); panel1_2.add(label2[1]); //int 값 → string 값 String str2[][]=new String[6][6]; for(int i=0;i<arr.no.length;i++){ for(int j=0;j<arr.no[0].length;j++){ str2[i][j]=""+arr.no[i][j]; } } for(int i=0; i<arr.no.length; i++){ for(int j=0; j<arr.no.length-1; j++){ for(int e=0; e<arr.no[0].length; e++ ){ if(str2[5][i].equals(str2[j][e])){ //lotto번호와 같을경우 red값 지정 str2[j][e]="<font color=red><b>"+str2[j][e]+"</b></font>"; }else{ } } } } //str3[]에 초기값 지정. str3은 label에 쓰기 위한 string값 String str3[]={"","","","","",""}; for(int i=0;i<str2.length;i++){ for(int j=0;j<str2[0].length;j++){ if(str2[i][j].equals("0")){ //선택하지 않은경우(값이 없는경우) break; str3[i]=""; break; } str3[i]+=str2[i][j]+" "; //html 적용했기 때문에, 공백. } } //str3배열에 html추가 str3[0]="<html><font size=3>"+str3[0]+"</font></html>"; str3[1]="<html><font size=3>"+str3[1]+"</font></html>"; str3[2]="<html><font size=3>"+str3[2]+"</font></html>"; str3[3]="<html><font size=3>"+str3[3]+"</font></html>"; str3[4]="<html><font size=3>"+str3[4]+"</font></html>"; /* * Label에 쓰는 부분 */ for(int i=0;i<5;i++){ label[(2*i+1)].setText(str3[i]); } //lotto번호 for(int i=0;i<arr.no.length;i++){ str=str+(arr.no[5][i])+" "; label2[1].setText(str); } //다 끝내고 값초기화? for(int i=0;i<str2.length;i++){ for(int j=0;j<str2[0].length;j++){ str2[i][j]=""; } } for(int i=0;i<str3.length;i++){ str3[i]=""; } } }
'2013 > Java' 카테고리의 다른 글
[Java] 로또(Lotto) 프로그램(2) (0) | 2013.11.22 |
---|---|
[Java] 윈도우 프로그램 작성기초(1) - GUI 프로그래밍 (1) | 2013.11.14 |
[Java] 멀티스레드 프로그램(3) - Critical section 동기화 (1) | 2013.11.13 |
[Java] 멀티스레드를 이용한 말 경주 프로그램 (0) | 2013.11.12 |
[Java] 멀티스레드 프로그램(2) - 스레드간의 데이터 교환 (0) | 2013.11.12 |
[Java] 로또(Lotto) 프로그램(1) (0) | 2013.11.11 |