2011年4月29日 星期五

java - 封裝

今天教的是封裝

把一個檔案分成兩個檔案
然後還是可以執行

而且可以縮減程式的篇幅


大致上如下面這個程式
是經過整理的


// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingEightPuzzleEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame


static FlowLayout flow=new FlowLayout();
static GridLayout grid12= new GridLayout(1,2);
static GridLayout grid33= new GridLayout(3,3);

static JPanel p1 = new JPanel(grid33); //實作  panel 1
static JPanel p2 = new JPanel(flow); //實作  panel 2
static JPanel p3 = new JPanel(grid33); //實作  panel 3

static JLabel  labels[]=new JLabel [10];
static JButton buttons[]=new JButton[10];
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
static String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};



public SwingEightPuzzleEvent()
{

for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}



p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕


}



public static void main(String args[])
{
SwingEightPuzzleEvent myfrm=new SwingEightPuzzleEvent();

myfrm.setLayout(grid12);
myfrm.setSize(450,450);

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

btn1.addActionListener(myfrm);

myfrm.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
//int rndno;
//String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);

/*
for (int j = 0; j < 9; j++)
{
//rndno=(int) (Math.random()*9);
//System.out.println(rndno);
//numbers[i]=String.valueOf(rndno);
//buttons[i].setLabel(numbers[i]);
}
*/

//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[0]);
}
}




作業

老師給我們兩個程式編譯&執行(程式取自http://www.rgagnon.com/javadetails/java-0144.html)

第一個

import java.util.Timer;
import java.util.TimerTask;

public class ToDo  {
  Timer timer;

  public ToDo ( int seconds )   {
    timer = new Timer (  ) ;
    timer.schedule ( new ToDoTask (  ) , seconds*1000 ) ;
  }


  class ToDoTask extends TimerTask  {
    public void run (  )   {
      System.out.println ( "OK, It's time to do something!" ) ;
      timer.cancel (  ) ; //Terminate the thread
    }
  }


  public static void main ( String args [  ]  )   {
    System.out.println ( "Schedule something to do in 5 seconds." ) ;
    new ToDo ( 5 ) ;
    System.out.println ( "Waiting." ) ;
  }
}



會出現以下結果





第二個

import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;

public class TimerDemo implements ActionListener {
  Timer t = new Timer(1000,this);

  TimerDemo() {
    t.start();
    }

  public static void main(String args[]) {
    TimerDemo td = new TimerDemo();
    // create a dummy frame to keep the JVM running
    //  (for demonstation purpose)
    java.awt.Frame dummy = new java.awt.Frame();
    dummy.setVisible(true);
    }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {
      System.out.println 
        ("\007Being ticked " + Calendar.getInstance().getTime());
      }
    }
}




會出現下面結果


會一直跑....還會有逼逼逼逼逼聲


加分題


import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class TimerDemo extends JFrame implements ActionListener
{
Timer t = new Timer(1000,this);

static JLabel  labels[]=new JLabel [10];
static JButton buttons[]=new JButton[10];
static JTextField tbx1=new JTextField("                                                                                                                                 "); // 建立1文字方塊物件

TimerDemo()
{
t.start();

}



  public static void main(String args[]) {
 TimerDemo myfrm = new TimerDemo();
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(500,500);

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JPanel p2 = new JPanel(grid33); //實作  panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3

for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
buttons[i] = new JButton(numbers[i]); // create buttons
p2.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p2); // 在視窗myfrm 內加入 panel 1
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p1 = new JPanel(flow); //實作  panel 2
myfrm.add(p1); // 在視窗myfrm 內加入 panel 2

p1.add(tbx1); // 在 panel 2內加入文字方塊

  
  


     myfrm.setVisible(true);
     java.awt.Frame dummy = new java.awt.Frame();
     dummy.setVisible(true);
    }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == t) {
      System.out.println
        ("\007Being ticked " + Calendar.getInstance().getTime());
      }
tbx1.setText("\007Being ticked " + Calendar.getInstance().getTime());
    }
}





在命令提示字元上會顯示時間
而且文字方塊一樣也會顯示時間

只是我還沒把文字方塊控制到想要的地方
還有方塊長度也是還沒掌握的






5/6改(文字方塊部分比上述的程式好一點)



import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class TimerDemo extends JFrame implements ActionListener
{
Timer t = new Timer(1000,this);

static JLabel  labels[]=new JLabel [10];
static JButton buttons[]=new JButton[10];
static JTextField tbx1=new JTextField(""); // 建立1文字方塊物件
static JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
TimerDemo()
{
t.start();

}


 
public static void main(String args[]) {
TimerDemo myfrm = new TimerDemo();
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(1000,1000);

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JPanel p1 = new JPanel(grid33); //實作  panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3

for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
btn1.addActionListener(myfrm);

//p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
myfrm.add(tbx1);  
   


     myfrm.setVisible(true);
     java.awt.Frame dummy = new java.awt.Frame();
     dummy.setVisible(true);
    }

  public void actionPerformed(ActionEvent e)
{

String numbers[]=new String[101];
for (int i = 0; i <100; i++)
{
numbers[i]=String.valueOf(i);
}


    if (e.getSource() == t) {
      System.out.println
        ("\007Being ticked " + Calendar.getInstance().getTime());
      }
tbx1.setText("\007Being ticked " + Calendar.getInstance().getTime());


    }
}

2011年4月22日 星期五

java - 按鈕不重複

錯誤版

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);


int rm=(int)Math.random()*9;
System.out.println(rm);
numbers[0]=String.valueOf(rm);
tbx1.setText(numbers[0]);
buttons[rm].setBackground(Color.blue);
labels[rm].setText(numbers[0]);

}
}



正確版

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);


int rm=(int)(Math.random()*9);
System.out.println(rm);
numbers[0]=String.valueOf(rm);
tbx1.setText(numbers[0]);
buttons[rm].setBackground(Color.blue);
labels[rm].setText(numbers[0]);

}
}



按鈕會重複


// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
  String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
String stringValue;
int rm;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);

for (int i = 0; i < 9; i++)
{
rm=(int)(Math.random()*9);
System.out.println(rm);
numbers[i]=String.valueOf(rm);
buttons[i].setLabel(numbers[i]);
}



//tbx1.setText(numbers[0]);
//buttons[rm].setBackground(Color.blue);
//labels[rm].setText(numbers[0]);

}
}




按鈕不會重複



// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
  String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
String stringValue;
int rm;
String tmp;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);

for (int i = 0; i < 9; i++)
{
int j=8-i;
rm=(int)(Math.random()*(9-i));
System.out.println(rm);
tmp=numbers[j];
numbers[j]=numbers[rm];
numbers[rm]=tmp;
buttons[j].setLabel(numbers[j]);
System.out.println(numbers[j]);
}



//tbx1.setText(numbers[0]);
//buttons[rm].setBackground(Color.blue);
//labels[rm].setText(numbers[0]);

}
}




作業 (5*5)


// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[26];
static JLabel  labels[]=new JLabel [26];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9", "10", "11", "12", "13", "14", "15", "16", "17", "18","19", "20", "21", "22", "23", "24"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9", "10", "11", "12", "13", "14", "15", "16", "17", "18","19", "20", "21", "22", "23", "24"};
String stringValue;
int rm;
String tmp;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);

for (int i = 0; i < 25; i++)
{
int j=24-i;
rm=(int)(Math.random()*(25-i));
System.out.println(rm);
tmp=numbers[j];
numbers[j]=numbers[rm];
numbers[rm]=tmp;
}


for (int i = 0; i < 25; i++)
{


buttons[j].setLabel(numbers[j]);
System.out.println(numbers[j]);
}




//tbx1.setText(numbers[0]);
//buttons[rm].setBackground(Color.blue);
//labels[rm].setText(numbers[0]);

}
}





加分題
輸入100
25個按鈕會從1-100隨機選25個



// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[26];
static JLabel  labels[]=new JLabel [26];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();

String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9", "10", "11", "12", "13", "14", "15", "16", "17", "18","19", "20", "21", "22", "23", "24"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作  panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作  panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作  panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]=new String[101];
for (int i = 0; i <100; i++)
{
numbers[i]=String.valueOf(i);
}

String stringValue;
int rm;
String tmp;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);

for (int i = 0; i < 25; i++)
{
int j=24-i;
rm=(int)(Math.random()*intValue);
System.out.println(rm);

tmp=numbers[j];
numbers[j]=numbers[rm];
numbers[rm]=tmp;

}

for (int i = 0; i < 25; i++)
{
buttons[i].setLabel(numbers[i]);
System.out.println(numbers[i]);

}




//buttons[rm].setBackground(Color.blue);
//labels[rm].setText(numbers[0]);

}
}




2011年4月1日 星期五

java - 按鈕排列(升級版)

將Frame改成 JFrame


//Swing, Button Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("Button class"); // Java Class JFrame
static Button btn1=new Button("Button 1"); // 建立1按鈕物件
static Button btn2=new Button("Button 2"); // 建立2按鈕物件
static TextField tbx1=new TextField("TextField  1"); // 建立1文字方塊物件

public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}


再來將Button也改成JButton
將TextField也改成JTextField


//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField  1"); // 建立1文字方塊物件

public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}

改變按鈕的排列
加入GridLayout


//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField  1"); // 建立1文字方塊物件

public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(0,2);
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border); // 在視窗內加入按鈕1
myfrm.add(btn2, border); // 在視窗內加入按鈕2
myfrm.add(tbx1, border); // 在視窗內加入按鈕2

/*
String[] numbers = {"0", "1", "2", "3", 4", "5", "6", "7", "8", "9"}; // goes on to 26
JButtons[] buttons = new JButton(numbers.length)
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
}
*/




myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}

再打上另外的指令
更方便也更簡單的增加按鈕


//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField  1"); // 建立1文字方塊物件

public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(0,2);

String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8", "9"}; // goes on to 26
JButton buttons[]=new JButton[10];

myfrm.setLayout(border);
myfrm.setSize(250,150);

myfrm.add(btn1, border); // 在視窗內加入按鈕1
myfrm.add(btn2, border); // 在視窗內加入按鈕2
myfrm.add(tbx1, border); // 在視窗內加入按鈕2


// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}




myfrm.setVisible(true); 
}
public void actionPerformed(ActionEvent e) 
int rn;
rn=(int) (Math.random()*49) ; 
System.out.println(rn );
}
}

改成3*3矩陣


//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame


public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(3,3);


String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];


myfrm.setLayout(border);
myfrm.setSize(250,150);






// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}








myfrm.setVisible(true); 
}
public void actionPerformed(ActionEvent e) 

int rn;
rn=(int) (Math.random()*49) ; 
System.out.println(rn );
}
}
增加panel



//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame

public static void main(String args[])
{

//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(3,3);
JPanel p = new JPanel(border); //PREFERRED!
myfrm.add(p);

String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];

myfrm.setLayout(border);
myfrm.setSize(250,150);



// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}




myfrm.setVisible(true); 
}
public void actionPerformed(ActionEvent e) 
int rn;
rn=(int) (Math.random()*49) ; 
System.out.println(rn );
}
}


------------------------------------------------------------------------------------------------------------------------------------



// with event
//AWT, JButton類別 有ActionListener
//AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class AwtTestEvent extends JFrame implements ActionListener
{
//static JJFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JTextField tbx1=new JTextField("          "); // 建立1文字方塊物件


public static void main(String args[])
{
String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];




AwtTestEvent myfrm=new AwtTestEvent();
GridLayout border1=new GridLayout(3,3);
GridLayout border2=new GridLayout(2,1);




JPanel p1 = new JPanel(border1); //PREFERRED!
JPanel p2 = new JPanel(border2); //PREFERRED!


// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], border1); // 在視窗內加入按鈕1
}
myfrm.add(p1);


myfrm.add(p2);






myfrm.setLayout(border1);
myfrm.setLayout(border2);


myfrm.setSize(250,150);
btn1.addActionListener(myfrm);




p2.add(btn1, border2); // 在視窗內加入按鈕1
p2.add(tbx1, border2); // 在視窗內加入文字方塊1; 


myfrm.setVisible(true); 


}
public void actionPerformed(ActionEvent e) 

String sr;
sr=tbx1.getText();
System.out.println(sr);
}
}