2015年5月22日 星期五

5/22 按0可對調

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Calculator extends JFrame implements
ActionListener{
int space;
JButton button[]=new JButton[10];

public static void main(String[]args){

new Calculator();
}


public Calculator()
{
String s;
int myrand;
int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] aresult;
int temp;
Random ran = new Random();
/*
for (int i = 0; i < 9; i++)
{
 System.out.println(a[i]);
}
*/

for (int i = 0; i < 9; i++)
{
 myrand=ran.nextInt(9-i);
temp=a[8-i];
a[8-i]=a[myrand];
a[myrand]=temp;
}

for (int i = 0; i < 9; i++)
{
 System.out.println(a[i]);
}

JFrame window=new JFrame("Calculator");

window.setDefaultLookAndFeelDecorated(true);


window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLayout(new GridLayout(3,3));

JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));


//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";

for (int i = 0; i < 9; i++)
{
button[i] = new JButton();


button[i].setActionCommand(s);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(a[i]));

jplPanel.add(button[i]);
if ( Integer.valueOf(button[i].getText())== 0)  
  {
                space=i;
                System.out.println(i)     ;
  }

}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);


}




public void actionPerformed(ActionEvent e) {
 for(int i=0; i <=8; i++)
{

if(e.getSource() == button[i]){
 //if( Integer.valueOf(button[i].getText())== 0)
String tempsting;
if (i==space-1)
{
tempsting=button[i].getText();
button[i].setText(button[i+1].getText());
button[i+1].setText(tempsting);
System.out.println(i+button[i].getText());
}
}


}

}

}



2015年5月8日 星期五

2015/5/8 隨機亂數陣列

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

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




=============================================================

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{


public static void main(String[]args){

new Calculator();
}


public Calculator()
{
String s;
JFrame window=new JFrame("Calculator");

window.setDefaultLookAndFeelDecorated(true);


window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLayout(new GridLayout(3,3));

JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));

JButton button[]=new JButton[10];
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";

for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();

button[i].setActionCommand(s);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));

jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}




public void actionPerformed(ActionEvent e) {



        String cmd = e.getActionCommand();
        System.out.println(cmd);
        if (cmd == "1") {
          
            System.out.println("Yes....");
        }

    }

}


2015年4月9日 星期四

4/10 JAVA作按鈕

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");/*9個按鈕*/
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.print("button1"); /*按了會在CMD顯示*/
}
});
jbnButton2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button2");
}
});
jbnButton3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button3");
}
});
jbnButton4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button4");
}
});
jbnButton5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button5");
}
});
jbnButton6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button6");
}
});
jbnButton7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button7");
}
});
jbnButton8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button8");
}
});
jbnButton9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.print("button9");
}
});


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("press ctrl+c to reset!");
}
}


2015年3月19日 星期四

3/20 作迴圈 VB+JAVA


JAVA 手動控制列出1到10

class Test
{
public static void main(String[] args)
{
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("8");
System.out.println("9");
System.out.println("10");
}
}

************

JAVA迴圈(10到1)

class Test
{
public static void main(String[] args)
{
for (int i = 10; i > 0; i--) {
    System.out.println(i);
}
}
}



************

VB迴圈

Private Sub CommandButton1_Click()
For i = 1 To 9
Cells(1, i) = i
Next i
End Sub



2015年3月5日 星期四

2015/3/6 JAVA程式作業 更改環境變數+CMD執行JAVA

程式設計工藝大師
http://tccnchsu.blogspot.tw/2011/02/2011-java.html
1.為何why要選修這門課?(動機)
興趣
2.希忘從這門課獲得那些知識?(目標)
學好JAVA
3.我要如何修習這一門課?(態度與方法)
認真上課

更改環境變數
可讓CMD不需變更路徑就執行JAVAC


利用CMD執行JAVA
cd為移動資料夾
javac 為編譯JAVA
java 為執行

class Test
{
public static void main(String[] args)
{
System.out.println("_____*_____");
System.out.println("____***____");
System.out.println("___*****___");
System.out.println("__*******__");
System.out.println("_____*_____");
System.out.println("_____*_____");
}
}


2014年12月18日 星期四

12/19 生成4*4亂數按鈕

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[17, 17]; //定義按鈕
        int[] array = new int[17]; //定義按鈕代表數字
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Instantiating all the buttons in the array



            for (int i = 0; i < 16; i++)
            {
                array[i] = i;  //每個按鈕的值分別為0到15
            }


            /*
            for (int j = 1; j < 10; j++)
            {
                buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50 * j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }
            */

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    buttons[i, j] = new Button(); //這行會生成4*4個按鈕
                 
                    buttons[i, j].Location = new Point(j * 50, i * 50); // (i,j)按鈕位置以 (j*50,i*50) 分布
                    buttons[i, j].Text = j.ToString(); //按鈕Text只能顯示文字 所以iToString
                    buttons[i, j].Size = new Size(50, 50); //每個按鈕大小為50*50
                    this.Controls.Add(buttons[i, j]);
                    buttons[i, j].Click += new EventHandler(Button1_Click); //把生成的按鈕設成Button1
                }
            }

         

        }
        private void Button1_Click(object sender, EventArgs e)  //這段是控制程式生成的按鈕Button1
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (sender == buttons[i, j])
                    { MessageBox.Show("i=" + i + ",j=" + j + "," + buttons[i, j].Text); }
                       //點某個按鈕就會寫出它 i,j (位置) 跟按鈕的值
                }
            }
        }


        private void button1_Click(object sender, EventArgs e) //這段是控制自己放的按鈕 button1
        {
            int d1, tmp, k;

            Random irand = new Random();
            d1 = irand.Next(1, 17); //把d1隨機亂數為1到16的值
            label1.Text = d1.ToString();


            for (int j = 1; j < 17; j++)
            {
                k = 16 - j + 1;
                tmp = array[d1];
                array[d1] = array[k];
                array[k] = tmp;
            }


            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Text = array[i*4+j+1].ToString();
                    this.Controls.Add(buttons[i, j]);
                }
            }






            //buttons[1, 1].Text = array[1].ToString();

            /*
            for (int j = 1; j < 10; j++)
            {
                //    buttons[1, j] = new Button();
                buttons[1, j].Location = new Point(50 * j, 50);
                buttons[1, j].Text = array[j].ToString();
                this.Controls.Add(buttons[1, j]);
            }
             */


            label2.Text = array[d1].ToString();
         
        }
    }
}







2014年12月4日 星期四

12/5 VB不重複隨機變數+C#變數未完

Private Sub CommandButton1_Click()

For i = 1 To 9
Cells(1, i).Font.ColorIndex = 0
Cells(1, i) = i
Next
'生成9個格子

For i = 1 To 9
j = 10 - i

rnd1 = (Fix(Rnd * j) + 1)
'設變數rnd1
front1 = Cells(1, rnd1)
Cells(1, rnd1) = Cells(1, j)
'用rnd取代j
Cells(1, j) = front1
'Cells變成變數

'Cells(3, 1) = rnd1
'Cells(1, rnd1).Font.ColorIndex = 3
Next



End Sub




以下為C# 還沒做到隨機變數

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Button[,] buttons = new Button[4, 4];
        int[] array1 = { 0,1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15 };
        int x;
        int[] seat; //宣告按鈕 矩陣 X 跟 變數seat
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(i * 50, j * 50);
                    buttons[i, j].Size = new Size(50, 50);
                 
                    this.Controls.Add(buttons[i, j]);
                    x=array1[i*4+j];
                    //對調 i j可變換行列
                    buttons[i, j].Text = Convert.ToString(x);

                 
                    Random rnd = new Random();
            label1.Text = "" + rnd.Next(17).ToString();
                   }

              }
         
           }

        private void button1_Click(object sender, EventArgs e)
        {
         
           // buttons[i, j]=seat[];
         

        }

        }
    }