推盤製作
交換
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Calculator extends JFrame implements
ActionListener{
int space;
JButton button[]=new JButton[10];
public static void main(String[]args){
new Calculator();
}
public Calculator()
{
String s;
int myrand;
int[] a = { 0,1, 2, 3, 4, 5, 6, 7, 8 };
int[] aresult;
int temp;
Random ran = new Random();
/*
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
*/
for (int i = 0; i < 9; i++)
{
myrand=ran.nextInt(9-i);
temp=a[8-i];
a[8-i]=a[myrand];
a[myrand]=temp;
}
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
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));
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";
for (int i = 0; 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(a[i]));
jplPanel.add(button[i]);
if(Integer.valueOf(button[i].getText())==0)
{
space=i;
System.out.println(i);
}
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for(int i=0; i <= 9; i++)
{
if(e.getSource() == button[i]){
String tempsting;
if(i==space-1)
{
tempsting=button[i].getText();
button[i].setText(button[i+1].getText());
button[i+1].setText(tempsting);
System.out.println(i+button[i].getText());
}
}
}
}
}
2015年5月22日 星期五
推盤遊戲
JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[16];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));
}
}
}
}
);
jplPanel.add(button[i]);
}
/*public void win()
{
for(int i=0;i<16;i++)
{
if(array[i]=i)
{
System.out.println("WIN!!!");
}
}
}*/
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(400,400);
window.setVisible(true);
}
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){
Random random = new Random();
JPanel jjj = new JPanel();
int number = 0;;
int array[] = new int[16];
boolean b;
JPanel jjj = new JPanel();
int number = 0;;
int array[] = new int[16];
boolean b;
for(int i=0;i<16;i++)
{
b = true;
while(b) {
b = false;
number = random.nextInt(16)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}
{
b = true;
while(b) {
b = false;
number = random.nextInt(16)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}
JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[16];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));
for (int i = 0; i < 16; i++){
//button[i]=new JButton(array[16]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
if(array[i]==16)
{
button[i].setText("");
System.out.println(i);
}
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(array[0]==1&&array[3]==4&&array[4]==5&&array[7]==8&&array[12]==13&&array[6]==7&&array[15]==16&&array[13]==14&&array[14]==15&&array[9]==10){JOptionPane.showMessageDialog(null,"dddddddddd");
}
//JOptionPane.showMessageDialog(null,"ddasdasdasdsads");
//button[i]=new JButton(array[16]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
if(array[i]==16)
{
button[i].setText("");
System.out.println(i);
}
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(array[0]==1&&array[3]==4&&array[4]==5&&array[7]==8&&array[12]==13&&array[6]==7&&array[15]==16&&array[13]==14&&array[14]==15&&array[9]==10){JOptionPane.showMessageDialog(null,"dddddddddd");
}
//JOptionPane.showMessageDialog(null,"ddasdasdasdsads");
int a=0,c=0;
for (int i = 0; i < 16; i++)
{
if(array[i]==16)
{
a=i;
}
}
for (int i = 0; i < 16; i++)
{
if(array[i]==16)
{
a=i;
}
}
if(a>3&&a<16)
{
if(e.getSource() == button[a-4])
{
{
if(e.getSource() == button[a-4])
{
//按下按鈕後事件
button[a-4].setText("");
button[a].setText(Integer.toString(array[a-4]));
c=array[a];
array[a]=array[a-4];
array[a-4]=c;
}
}
button[a-4].setText("");
button[a].setText(Integer.toString(array[a-4]));
c=array[a];
array[a]=array[a-4];
array[a-4]=c;
}
}
if(a<12&&a>-4)
{
if(e.getSource() == button[a+4])
{ //按下按鈕後事件
button[a+4].setText("");
button[a].setText(Integer.toString(array[a+4]));
c=array[a];
array[a]=array[a+4];
array[a+4]=c;
}
}
if(a>-1&&a<15)
{
if(e.getSource() == button[a+1])
{
//按下按鈕後事件
button[a+1].setText("");
button[a].setText(Integer.toString(array[a+1]));
c=array[a];
array[a]=array[a+1];
array[a+1]=c;
}
}
if(a<16&&a>0)
{
if(e.getSource() == button[a-1])
{
//按下按鈕後事件
button[a-1].setText("");
button[a].setText(Integer.toString(array[a-1]));
c=array[a];
array[a]=array[a-1];
array[a-1]=c;
{
if(e.getSource() == button[a+4])
{ //按下按鈕後事件
button[a+4].setText("");
button[a].setText(Integer.toString(array[a+4]));
c=array[a];
array[a]=array[a+4];
array[a+4]=c;
}
}
if(a>-1&&a<15)
{
if(e.getSource() == button[a+1])
{
//按下按鈕後事件
button[a+1].setText("");
button[a].setText(Integer.toString(array[a+1]));
c=array[a];
array[a]=array[a+1];
array[a+1]=c;
}
}
if(a<16&&a>0)
{
if(e.getSource() == button[a-1])
{
//按下按鈕後事件
button[a-1].setText("");
button[a].setText(Integer.toString(array[a-1]));
c=array[a];
array[a]=array[a-1];
array[a-1]=c;
}
}
}
}
);
jplPanel.add(button[i]);
}
/*public void win()
{
for(int i=0;i<16;i++)
{
if(array[i]=i)
{
System.out.println("WIN!!!");
}
}
}*/
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(400,400);
window.setVisible(true);
}
}
mport java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Calculator extends JFrame implements
ActionListener{
JButton button[]=new JButton[10];
public static void main(String[]args){
new Calculator();
}
public Calculator()
{
String s;
int myrand;
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] aresult;
int temp;
Random ran = new Random();
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
for (int i = 0; i < 9; i++)
{
myrand=ran.nextInt(9-i);
temp=a[8-i];
a[8-i]=a[myrand];
a[myrand]=temp;
}
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
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));
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";
for (int i = 0; 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(a[i]));
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for(int i=1; i <= 9; i++)
{
if(e.getSource() == button[i]){
System.out.println(i);
}
}
}
}
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Calculator extends JFrame implements
ActionListener{
JButton button[]=new JButton[10];
public static void main(String[]args){
new Calculator();
}
public Calculator()
{
String s;
int myrand;
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] aresult;
int temp;
Random ran = new Random();
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
for (int i = 0; i < 9; i++)
{
myrand=ran.nextInt(9-i);
temp=a[8-i];
a[8-i]=a[myrand];
a[myrand]=temp;
}
for (int i = 0; i < 9; i++)
{
System.out.println(a[i]);
}
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));
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";
for (int i = 0; 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(a[i]));
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for(int i=1; i <= 9; i++)
{
if(e.getSource() == button[i]){
System.out.println(i);
}
}
}
}
2015年5月8日 星期五
3*3 9宮格顯示對的數字
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JButton button[]=new JButton[10];
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));
//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) {
for(int i=1; i<button.length; i++){
if(e.getSource() == button[i]){
System.out.println(i);
}
}
}
}
=======================================================================
亂數 4*4 不重複
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){
Random random = new Random();
int number = 0;;
int array[] = new int[16];
boolean b;
for(int i=0;i<16;i++)
{
b = true;
while(b) {
b = false;
number = random.nextInt(16)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}
JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[16];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));
for (int i = 0; i < 16; i++){
//button[i]=new JButton(""+array[16]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
button[i].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int j = 0; j < 16; j++) {
if (e.getSource() == button[j]) {
System.out.println(j+1);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JButton button[]=new JButton[10];
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));
//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) {
for(int i=1; i<button.length; i++){
if(e.getSource() == button[i]){
System.out.println(i);
}
}
}
}
=======================================================================
亂數 4*4 不重複
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame{
public static void main(String[]args){
Random random = new Random();
int number = 0;;
int array[] = new int[16];
boolean b;
for(int i=0;i<16;i++)
{
b = true;
while(b) {
b = false;
number = random.nextInt(16)+1;
for(int j=0;j<array.length;j++) {
if(array[j]==number) {
b = true;
}
}
}
array[i] = number;
System.out.printf("array[%d]=%d\n",i,number);
}
JFrame window=new JFrame("myButton");
window.setSize(450, 150);
JButton button[]=new JButton[16];
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4,8,8));
for (int i = 0; i < 16; i++){
//button[i]=new JButton(""+array[16]);
button[i] = new JButton();
button[i].setSize(40,40);
button[i].setText(Integer.toString(array[i]));
button[i].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int j = 0; j < 16; j++) {
if (e.getSource() == button[j]) {
System.out.println(j+1);
}
}
}
訂閱:
文章 (Atom)