2015年5月22日 星期五

第九次作業



import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){

Random random = new Random();
JPanel jjj = new JPanel();
int number = 0;;
int array[] = new int[9];
boolean b;
for(int i=0;i<9;i++)
{
b = true;
while(b) {
b = false;
number = random.nextInt(9)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}


JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[9];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));
for (int i = 0; i < 9; i++){
//button[i]=new JButton(array[9]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
if(array[i]==9)
{
button[i].setText("");
System.out.println(i);
}
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(array[0]==1&&array[3]==4&&array[4]==5&&array[7]==8)
{JOptionPane.showMessageDialog(null,"dddddddddd");
}
//JOptionPane.showMessageDialog(null,"ddasdasdasdsads");
int a=0,c=0;
for (int i = 0; i < 9; i++)
{
if(array[i]==9)
{
a=i;
}
}
if(a>2&&a<9)
{
if(e.getSource() == button[a-3])
{
//按下按鈕後事件
button[a-3].setText("");
button[a].setText(Integer.toString(array[a-3]));
c=array[a];
array[a]=array[a-3];
array[a-3]=c;
}
}
if(a<6&&a>-3)
{
if(e.getSource() == button[a+3])
{ //按下按鈕後事件
button[a+3].setText("");
button[a].setText(Integer.toString(array[a+3]));
c=array[a];
array[a]=array[a+3];
array[a+3]=c;
}
}
if(a>-1&&a<8)
{
if(e.getSource() == button[a+1])
{
//按下按鈕後事件
button[a+1].setText("");
button[a].setText(Integer.toString(array[a+1]));
c=array[a];
array[a]=array[a+1];
array[a+1]=c;
}
}
if(a<9&&a>0)
{
if(e.getSource() == button[a-1])
{
//按下按鈕後事件
button[a-1].setText("");
button[a].setText(Integer.toString(array[a-1]));
c=array[a];
array[a]=array[a-1];
array[a-1]=c;



}
}


}
}
);
jplPanel.add(button[i]);
}
/*public void win()
{
for(int i=0;i<9;i++)
{
if(array[i]=i)
{
System.out.println("WIN!!!");
}
}
}*/
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(400,400);
window.setVisible(true);
}
}

2015年5月15日 星期五

第八次作業


import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){



Random random = new Random();
JPanel jjj = new JPanel();
int number = 0;;
int array[] = new int[16];
boolean b;

for(int i=0;i<16;i++)
{
b = true;
while(b) {
b = false;
number = random.nextInt(16)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}




JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[16];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));

for (int i = 0; i < 16; i++){
//button[i]=new JButton(array[16]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
if(array[i]==16)
{
button[i].setText("");
System.out.println(i);
}
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(array[0]==1&&array[3]==4&&array[4]==5&&array[7]==8&&array[12]==13&&array[6]==7&&array[15]==16&&array[13]==14&&array[14]==15&&array[9]==10){JOptionPane.showMessageDialog(null,"dddddddddd");
}
//JOptionPane.showMessageDialog(null,"ddasdasdasdsads");

int a=0,c=0;
for (int i = 0; i < 16; i++)
{
if(array[i]==16)
{
a=i;
}
}

if(a>3&&a<16)
{
if(e.getSource() == button[a-4])
{

//按下按鈕後事件
button[a-4].setText("");
button[a].setText(Integer.toString(array[a-4]));
c=array[a];
array[a]=array[a-4];
array[a-4]=c;
}
}

if(a<12&&a>-4)
{
if(e.getSource() == button[a+4])
{ //按下按鈕後事件
button[a+4].setText("");
button[a].setText(Integer.toString(array[a+4]));
c=array[a];
array[a]=array[a+4];
array[a+4]=c;
}
}
if(a>-1&&a<15)
{
if(e.getSource() == button[a+1])
{
//按下按鈕後事件
button[a+1].setText("");
button[a].setText(Integer.toString(array[a+1]));
c=array[a];
array[a]=array[a+1];
array[a+1]=c;
}
}
if(a<16&&a>0)
{
if(e.getSource() == button[a-1])
{
//按下按鈕後事件
button[a-1].setText("");
button[a].setText(Integer.toString(array[a-1]));
c=array[a];
array[a]=array[a-1];
array[a-1]=c;






}
}




}
}
);
jplPanel.add(button[i]);
}
/*public void win()
{
for(int i=0;i<16;i++)
{
if(array[i]=i)
{
System.out.println("WIN!!!");
}
}
}*/
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(400,400);
window.setVisible(true);
}

}

2015年5月8日 星期五

第七次作業 亂數不重複


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class Test
{
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame("作業GridLayout)");
GridLayout f1=new GridLayout(4, 13);

Container c=frame.getContentPane();

c.setLayout(f1);

int [] num = new int[9]; //宣告一個數字陣列,用來記錄產生過的亂數
Arrays.fill(num,-1); //將陣列內容全都設為-1
int i=1;
do{
int j=(int)(Math.random()*9);
if(num[j] == 0) continue; //判斷是否亂數取出的數字是否出現過
num[j] = 0;//將出現過的數字索引值的陣列內容設為0,代表出現過
c.add(new JButton(j+""));
i++;
}while(i<=9);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}

2015年4月9日 星期四

第五次作業

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{


public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton  jbnButton1 = new JButton("Button 1");
JButton  jbnButton2 = new JButton("Button 2");
JButton  jbnButton3 = new JButton("Button 3");
JButton  jbnButton4 = new JButton("Button 4");
JButton  jbnButton5 = new JButton("Button 5");
JButton  jbnButton6 = new JButton("Button 6");
JButton  jbnButton7 = new JButton("Button 7");
JButton  jbnButton8 = new JButton("Button 8");
JButton  jbnButton9 = new JButton("Button 9");
jbnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("1!");
}
});
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("2!");
}
});
jbnButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("3!");
}
});
jbnButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("4!");
}
});
jbnButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("5!");
}
});
jbnButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("6!");
}
});
jbnButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("7!");
}
});
jbnButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("8!");
}
});
jbnButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("9!");
}
});


JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("chih-yu hsu");
}
}

2015年3月27日 星期五

第四次作業


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton jbnButton1=new JButton("Button 1");
JButton jbnButton2 = new JButton("Button 2");
JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("i");
}
}