2015年4月23日 星期四

16個按鈕程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{


public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(600, 200);
JPanel jplPanel = new JPanel();
int i;
int j=1;
int num1 , num2;
JButton jbnButton[] = new JButton[16];
JButton changeButton[] = new JButton[3];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
jbnButton[i]=new JButton("Button"+(i+1));
jplPanel.add(jbnButton[i]);
String s = Integer.toString(i+1);
jbnButton[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
/*if(j<3){
changeButton[j] = jbnButton[i];
num1 = i;
j++;
}
else if(j==3){
changeButton[j]=jbnButton[i];
jbnButton[num1] = changeButton[j];
jbnButton[i] = changeButton[j-1];
j=1;
}*/
//j++;
//JButton btn = (JButton[i])e.getSourec();
//lab.setText(btn.getLabel+".");
System.out.println("Button"+s);
}
});
}

jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
}
}

=============================================================================================
亂數不重複C
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
Random rand = new Random();
Button[,] buttons = new Button[4, 4];
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
buttons[i, j] = new Button();
buttons[i, j].Size = new Size(80, 80);
buttons[i, j].Location = new Point(i * 85, j * 85);
this.Controls.Add(buttons[i, j]);
}

}
}

private void button1_Click_1(object sender, EventArgs e)
{
int a, b, x;
int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

for (int i = 15; i > 1; i--)
{
a = rand.Next(0, i);
b = array[a];
array[a] = array[i];
array[i] = b;
}

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
x = array[j * 4 + i];
buttons[i, j].Text = Convert.ToString(x);
this.Controls.Add(buttons[i, j]);
}

}
}
}
}
What's the best practice (and why) for implementing the java.awt.event.ActionListenerinterface?
import java.awt.*;  
import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{


public static void main(String[]args){

new Calculator();
}


public Calculator()
{
String s;
JFrame window=new JFrame("Calculator");

window.setDefaultLookAndFeelDecorated(true);


window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setLayout(new GridLayout(3,3));

JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));

JButton button[]=new JButton[10];
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";

for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();

button[i].setActionCommand(s);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));

jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}




public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        System.out.println(cmd);
        if (cmd == "1") {
           
            System.out.println("Yes....");
        }
    }

}

2015年4月9日 星期四

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{


public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton  jbnButton1 = new JButton("Button 1");
JButton  jbnButton2 = new JButton("Button 2");
JButton  jbnButton3 = new JButton("Button 3");
JButton  jbnButton4 = new JButton("Button 4");
JButton  jbnButton5 = new JButton("Button 5");
JButton  jbnButton6 = new JButton("Button 6");
JButton  jbnButton7 = new JButton("Button 7");
JButton  jbnButton8 = new JButton("Button 8");
JButton  jbnButton9 = new JButton("Button 9");
jbnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 1!");
}
  });
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 2!");
}
  });
jbnButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 3!");
}
  });
jbnButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 4!");
}
  });
jbnButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 5!");
}
  });
jbnButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 6!");
}
  });
jbnButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 7!");
}
  });
jbnButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 8!");
}
  });
jbnButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Button 9!");
}
  });
JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("chih-yu hsu");
}
}


多重按鈕狗爬式