2011年4月28日 星期四

2011/04/29 public 建構子

Java 系列電子書




===================================================================

// 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 JTextField tbx1=new JTextField(2); // 建立1文字方塊物件

static JButton buttons[]=new JButton[10];

static JLabel labels[]=new JLabel [10];

public SwingEightPuzzleEvent()

{







}










public static void main(String args[])

{

SwingEightPuzzleEvent myfrm=new SwingEightPuzzleEvent();

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 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]);

}

}
=================================================================

整理好後
=================================================================
// 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 JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel  labels[]=new JLabel [10];
static 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);
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 JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1

public SwingEightPuzzleEvent()
{
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
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(p3); // 在視窗myfrm 內加入 panel 3
btn1.addActionListener(myfrm);
myfrm.add(p2); // 在視窗myfrm 內加入 panel 2
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]);
}
}

=================================================================

class Object1 {
public static void main(String args[]) {
Person1 p1, p2;
p1 = new Person1("大雄", 50);
p2 = new Person1("胖虎", 80);
p1.checkWeight();
p2.checkWeight();
p2.weight = 68;
p1.checkWeight();
p2.checkWeight();
}
}






class Person1 {
String name;
int weight;

Person1(String pName, int pWeight) {
name = pName;
weight = pWeight;
}

void checkWeight() {
System.out.print(name+"體重 "+weight+" 公斤,");
if (weight < 70)
System.out.println("很苗條!");
else
System.out.println("很穩重!");
}
}



class Person1 {
String name;
int weight;

Person1(String pName, int pWeight) {
name = pName;
weight = pWeight;
}

void checkWeight() {
System.out.print(name+"體重 "+weight+" 公斤,");
if (weight < 70)
System.out.println("很苗條!");
else
System.out.println("很穩重!");
}
}
=================================================================

2011年4月22日 星期五

2011/04/22 java button / setLabel /String.valueOf()

===================================================================
// 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);

}
}
===================================================================
9個隨機亂數button(未解決重複)
===================================================================
public void actionPerformed(ActionEvent e)
{
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
int rndno;
String stringValue;
rndno = (int) (Math.random()*9);
for (int i = 0; i <9; i++)
{
rndno = (int) (Math.random()*9);
System.out.println(rndno);
numbers[i]=String.valueOf (rndno);
buttons[i].setLabel(numbers[i]);
}
===================================================================

3*3已解決
===================================================================
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTestEvent2 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[])
{
SwingTestEvent2 myfrm=new SwingTestEvent2();
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 numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
int rndno;
String stringValue;
String tmp;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
/*
for (int i = 0; i <9; i++)
{
//rndno = (int) (Math.random()*(10-i));
rndno = (int) (Math.random()*9);
//Collections.shuffle(rndno);
System.out.println(rndno);
numbers[i]=String.valueOf (rndno);
buttons[i].setLabel(numbers[i]);
}
*/
/*
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[8];
numbers[8]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[7];
numbers[7]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[6];
numbers[6]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[5];
numbers[5]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[4];
numbers[4]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[3];
numbers[3]=numbers[rndno];
numbers[rndno]=tmp;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[2];
numbers[2]=numbers[rndno];
numbers[rndno]=tmp;
*/
/*
for (int i = 0; i <9; i++)
{
//int j=8-i;
rndno = (int) (Math.random()*9);
System.out.println(rndno);
tmp=numbers[i];
numbers[i]=numbers[rndno];
numbers[rndno]=tmp;
}
*/
for (int i = 0; i < 9; i++)
{
int j=8-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 < 9; 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]);
}
}
===================================================================

5*5隨機不重複
===================================================================
 // with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.lang.*;
//import java.util.Collections;

public class SwingTestEvent3 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[])
{
SwingTestEvent3 myfrm=new SwingTestEvent3();
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(4,5);
GridLayout grid33= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(650,650);
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[]  = {"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"};
int rndno;
String tmp;
String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);

/*
for (int i = 0; i <25; i++)
{
//rndno = (int) (Math.random()*(10-i));
rndno = (int) (Math.random()*25);
//Collections.shuffle(qList);
System.out.println(rndno);
numbers[i]=String.valueOf (rndno);
buttons[i].setLabel(numbers[i]);
}
*/
for (int i = 0; i < 25; i++)
{
tbx1.setText("");
int 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]);
}

//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[i]);
}
}
===================================================================
100隨機產生25個
public void actionPerformed(ActionEvent e)
{
//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"};
String numbers[]=new String[101];
for (int i = 0; i <101; i++)
{
numbers[i]=String.valueOf(i);
}
int rndno;
String tmp;
String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);

for (int i = 0; i < 101; i++)
{
tbx1.setText(numbers[i]);
int j=100-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]);
}

//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[i]);
}

Use google protocol buffers + QT +CMAKE

Protocol Buffers https://developers.google.com/protocol-buffers/ Serialize and ParseFrom https://www.jianshu....