import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class TimerDemo implements ActionListener {
Timer t = new Timer(1000,this);
static JLabel tf1=new JLabel();
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.setSize(300,225);
dummy.add(tf1);
dummy.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
tf1.setText("\007Being ticked " + Calendar.getInstance().getTime());
System.out.println
("\007Being ticked " + Calendar.getInstance().getTime());
}
}
}
=================================================
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." ) ;
TimerDemo t1=new TimerDemo();
}
}
2011-04-29
2011-04-22
2011/04/22
random不重複
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest1random extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN");
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;
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"};
public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
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"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);
for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
}
public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);
for (int i = 0; i < 25; i++)
{
int j=24-i;
int r1=(int) (Math.random()*(j+1));
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{
int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);
}
*/
}
}
==================================================================
輸入100內數字從其中挑25個數
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest1random extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN");
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;
public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
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"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);
for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
}
public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);
String numbers[]=new String[101];
for (int i = 0; i <101; i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < 25; i++)
{
int j=24-i;
String m1=tf1.getText();
int m2=Integer.parseInt(m1);
int r1=(int) 1+(int)(Math.random()*m2);
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{
int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);
}
*/
}
}
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest1random extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN");
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;
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"};
public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
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"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);
for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
}
public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);
for (int i = 0; i < 25; i++)
{
int j=24-i;
int r1=(int) (Math.random()*(j+1));
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{
int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);
}
*/
}
}
==================================================================
輸入100內數字從其中挑25個數
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest1random extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN");
static JTextField tf1=new JTextField("1");
static JLabel lb[]=new JLabel[25];
static JButton bt[]=new JButton[25];
String tmp;
public static void main(String args[])
{
swingtest1random fm=new swingtest1random();
GridLayout gl= new GridLayout(5,5);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
JPanel p3 = new JPanel(gl);
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"};
JButton buttons[]=new JButton[25];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
fm.add(p3);
for (int i = 0; i < 25; i++)
{
bt[i] = new JButton(numbers[i]);
p1.add(bt[i], gl);
}
fm.setVisible(true);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
lb[i] = new JLabel();
p3.add(lb[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
}
public void actionPerformed(ActionEvent e)
{
//String s1;
//s1=tf1.getText();
//int i1;
//i1 = Integer.parseInt(s1);
String numbers[]=new String[101];
for (int i = 0; i <101; i++)
{
numbers[i]=String.valueOf(i);
}
for (int i = 0; i < 25; i++)
{
int j=24-i;
String m1=tf1.getText();
int m2=Integer.parseInt(m1);
int r1=(int) 1+(int)(Math.random()*m2);
tmp=numbers[j];
numbers[j]=numbers[r1];
numbers[r1]=tmp;
}
for (int i = 0; i < 25; i++)
{
System.out.println(numbers[i]);
bt[i].setLabel(numbers[i]);
}
/*
for (int i = 0; i < 9; i++)
{
int r1=(int) (Math.random()*9);
//int r2=(int) (Math.random()*9);
System.out.println(r1);
String s1=Integer.toString(r1);
bt[i].setText(s1);
//lb[r1].setText(s1);
}
*/
}
}
2011-04-01
2011/04/01
取代
註解掉很多
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN"); // 建立按鈕物件
static JTextField tf1=new JTextField("1");
/*
static JTextField tf1=new JTextField("1");
static JTextField tf2=new JTextField("2");
static JTextField tf3=new JTextField("3");
static JTextField tf4=new JTextField("4");
static JTextField tf5=new JTextField("5");
static JTextField tf6=new JTextField("6");
static JTextField tf7=new JTextField("7");
*/
public static void main(String args[])
{
swingtest fm=new swingtest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
GridLayout gl= new GridLayout(3,3);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
/*
fm.add(bt1); // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
*/
}
public void actionPerformed(ActionEvent e)
{
String s1;
s1=tf1.getText();
System.out.println(s1);
}
/*
public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}
*/
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN"); // 建立按鈕物件
static JTextField tf1=new JTextField("1");
/*
static JTextField tf1=new JTextField("1");
static JTextField tf2=new JTextField("2");
static JTextField tf3=new JTextField("3");
static JTextField tf4=new JTextField("4");
static JTextField tf5=new JTextField("5");
static JTextField tf6=new JTextField("6");
static JTextField tf7=new JTextField("7");
*/
public static void main(String args[])
{
swingtest fm=new swingtest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
GridLayout gl= new GridLayout(3,3);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
/*
fm.add(bt1); // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
*/
}
public void actionPerformed(ActionEvent e)
{
String s1;
s1=tf1.getText();
System.out.println(s1);
}
/*
public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}
*/
}
執行圖
訂閱:
文章 (Atom)