把一個檔案分成兩個檔案
然後還是可以執行
而且可以縮減程式的篇幅
大致上如下面這個程式
是經過整理的
// 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());
}
}