2011年4月29日 星期五

Object-Oriented Programming Java. The 10th Week of Class Notes.

// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class SwingEightPuzzleEvent extends JFrame implements ActionListener
{
Timer t = new Timer(1000,this);
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
static  JTextArea JtaR = new JTextArea();
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
static FlowLayout flow=new FlowLayout();
static GridLayout grid12= new GridLayout(1,2);
static GridLayout grid33= new GridLayout(3,3);
JPanel p1 = new JPanel(grid33); 
JPanel p2 = new JPanel(); 
JPanel p3 = new JPanel(grid33); 

public SwingEightPuzzleEvent()
{

p2.setLayout(new BorderLayout());
add(JtaR, BorderLayout.CENTER);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
setLayout(grid12);
setSize(450,450);
add(p1); // 在視窗myfrm 內加入 panel 1
add(p3); // 在視窗myfrm 內加入 panel 3
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(this);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕
add(p2); // 在視窗myfrm 內加入 panel 2
t.start();
setVisible(true);
}


public static void main(String args[])
{
//new TimerDemo();
new SwingEightPuzzleEvent();
}

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

      }
/*
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]);
}
}

沒有留言:

張貼留言