2011年4月22日 星期五

2011/04/22上課內容

開啟一範例程式如下:
// 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 stringValue;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.blue);
labels[intValue].setText(stringValue);
}
}
--------------------------------------------------------
此程式碼的執行結果為
在文字方塊輸入一數字
並按下旁邊的按鈕
會在左邊九個按鈕中
將所對應數字的按鈕變成藍色

接下來將程式碼改成如下圖:
此執行結果為
按按鈕時在文字方塊產生一亂數並且在對應的按鈕變成藍色
此為反覆按完的結果

接下來將程式碼改成如下圖:
執行結果為按按鈕之後以亂數在第一個按鈕變更標籤
接下來按按鈕之後以亂數將3乘3按鈕變更標籤並且按鈕的數字不重複
作業:將3乘3改成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[25];
static JLabel  labels[]=new JLabel [25];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
String numbers[]  = {"1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(700,700);
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[]  = {"1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
String stringValue,tmp;
int j,rndno;
for (int i = 0; i < 25; i++)
{
j=24-i;
rndno=(int)(Math.random()*(j+1));
//System.out.println(rndno);
tmp=numbers[j];
numbers[j]=numbers[rndno];
numbers[rndno]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
buttons[i].setLabel(numbers[i]);
}
//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[0]);
//buttons[intValue].setBackground(Color.blue);
//labels[intValue].setText(stringValue);
}
}

沒有留言:

張貼留言