// 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]);
}
}
2011年4月29日 星期五
Object-Oriented Programming Java. The 10th Week of Class Notes.
2011年4月22日 星期五
Object-Oriented Programming Java. The 9th Week of Class Notes.
https://sites.google.com/site/kernelnetworkprograming/javaprogramming
60個數亂取25個數排列
60個數亂取25個數排列
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
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 < 25; 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)
{
int rand;
String str[]= new String[26];
String stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
//String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
Random random = new Random();
int result[] = new int[]{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,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60};
for(int i=0; i < 25; i ++)
{
int index = random.nextInt(intValue);
//交換 i 跟index的原素
int tmp = result[index];
result[index] = result[i];
result[i] = tmp;
//System.out.println(result[i]);
//str[i] =String.valueOf(result[i]);
//buttons[i].setLabel(str[i] );
}
for(int i=0; i < result.length; i ++)
{
System.out.println("result["+i+"]=" + result[i]);
str[i] =""+result[i];
buttons[i].setLabel(str[i]);
}
//buttons[rand].setBackground(Color.blue);
//labels[rand].setText(numbers[0]);
}
}
2011年4月1日 星期五
Object-Oriented Programming Java. The 7th Week of Class Notes.
// app17_8, Button類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class app17_8 extends JFrame implements ActionListener
{
//static Frame frm=new Frame("Button class");
//static BorderLayout border=new BorderLayout();//環繞排列
static JButton buttons[]=new JButton[10];
static JTextField jtf=new JTextField();
static JButton button=new JButton();
public static void main(String args[])
{
app17_8 frm=new app17_8();
button.addActionListener(frm);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
GridLayout glt= new GridLayout(3,3);
frm.setSize(200,200);
//frm.add(btn1);
frm.setLayout(glt);
JPanel p = new JPanel(glt);
JPanel g = new JPanel(glt);
for(int i=0;i<numbers.length;i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
buttons[i] .addActionListener(frm);
p.add(buttons[i], glt);
}
g.add(jtf,glt);
g.add(button,glt);
frm.add(p);
frm.add(g);
frm.setVisible(true);
frm.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
int t1;
stringValue=jtf.getText();
System.out.println(stringValue);
t1= Integer.parseInt(stringValue);
if(t1==1)
buttons[1].setBackground(Color.RED);
else if(t1==2)
buttons[2].setBackground(Color.RED);
else if(t1==3)
buttons[3].setBackground(Color.RED);
else if(t1==4)
buttons[4].setBackground(Color.RED);
else if(t1==5)
buttons[5].setBackground(Color.RED);
else if(t1==6)
buttons[6].setBackground(Color.RED);
else if(t1==7)
buttons[7].setBackground(Color.RED);
else if(t1==8)
buttons[8].setBackground(Color.RED);
else if(t1==0)
buttons[0].setBackground(Color.RED);
}
}
執行結果 |
訂閱:
文章 (Atom)