1.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(4,4));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[17];
for (int i = 1; i <= 16; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
button[10].setActionCommand("d10");
button[11].setActionCommand("d11");
button[12].setActionCommand("d12");
button[13].setActionCommand("d13");
button[14].setActionCommand("d14");
button[15].setActionCommand("d15");
button[16].setActionCommand("d16");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
String cmd10 = e.getActionCommand();
String cmd11 = e.getActionCommand();
String cmd12 = e.getActionCommand();
String cmd13 = e.getActionCommand();
String cmd14 = e.getActionCommand();
String cmd15 = e.getActionCommand();
String cmd16 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
if (cmd10 == "d10") {
System.out.println("10");
}
if (cmd11 == "d11") {
System.out.println("11");
}
if (cmd12 == "d12") {
System.out.println("12");
}
if (cmd13 == "d13") {
System.out.println("13");
}
if (cmd14 == "d14") {
System.out.println("14");
}
if (cmd15 == "d15") {
System.out.println("15");
}
if (cmd16 == "d16") {
System.out.println("16");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.完成圖
關於我
就讀逢甲大學
正在修java
2015年4月24日 星期五
2015年4月19日 星期日
數字推盤
1.程式碼
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Piece extends JFrame
{
//放數字九官格
private String [][] box = new String[][]{{"1","2","3"},
{"4","5","6"},
{"7","8"," "}};
private keyLis kl = new keyLis();//鍵盤事件
private ButtonHandler bh = new ButtonHandler();//功能表單事件
private final int LEFT = 37,UP = 38, RIGHT = 39, DOWN = 40;
private JLabel [] jl = new JLabel [9];
private JMenuBar jmb;
private JMenu game = new JMenu("遊戲");;
private JMenuItem [] gm = new JMenuItem[3];
private boolean isStart = false; //遊戲開始
//初始化遊戲變數值
private String[][] init()
{
final int ROW = 3, COL = 3;
String [][] boxTemp = new String[][]{{"1","2","3"},
{"4","5","6"},
{"7","8"," "}};
//用亂數打亂數字的排列
for(int count = 0; count < 200; count++)
{
String temp = " ";
int [] zero = null;
int ranNum = (int)(Math.random()*4)+LEFT;
zero = getZero(boxTemp); //取得0的位置
switch(ranNum)
{
case LEFT: //左移
if(zero[1]+1<boxTemp[zero[0]].length)
{
temp = boxTemp[zero[0]][zero[1]+1];
boxTemp[zero[0]][zero[1]+1] = " ";
}
break;
case UP: //上移
if(zero[0]+1<boxTemp.length)
{
temp = boxTemp[zero[0]+1][zero[1]];
boxTemp[zero[0]+1][zero[1]] = " ";
}
break;
case RIGHT: //右移
if(zero[1]-1 >= 0)
{
temp = boxTemp[zero[0]][zero[1]-1];
boxTemp[zero[0]][zero[1]-1] = " ";
}
break;
case DOWN: //下移
if(zero[0]-1 >= 0)
{
temp = boxTemp[zero[0]-1][zero[1]];
boxTemp[zero[0]-1][zero[1]] = " ";
}
break;
default:
}
boxTemp[zero[0]][zero[1]] = temp;
}
return boxTemp;
}
//取得0的位置
private int [] getZero(String [][] boxArr)
{
int location[] = new int[2];
for(int r = 0; r < boxArr.length; r++)
{
for(int c = 0; c < boxArr[r].length; c++)
{
//取得數字0的位置
if(boxArr[r][c].equals(" "))
{
location[0] = r; //" "的x位置
location[1] = c; //" "的y位置
}
}
}
return location;
}
public Piece()
{
super("數字拼圖");
Container c = getContentPane();
c = this.getContentPane();
jmb = new JMenuBar();
this.setJMenuBar(jmb); //加入工具列
//遊戲的選擇項目
jmb.add(game);
gm[0] = new JMenuItem("新局");
gm[1] = new JMenuItem("放棄");
gm[2] = new JMenuItem("結束遊戲");
game.add(gm[0]);
game.add(gm[1]);
game.addSeparator();
game.add(gm[2]);
gm[1].setEnabled(false);
//數字的排為3*3
c.setLayout(new GridLayout(3,3));
for(int i = 0; i < jl.length; i++)
{
jl[i] = new JLabel(" "+box[i/box.length][i%box.length]);
jl[i].setFont(new Font("Serif",Font.BOLD,40));
c.add(jl[i]);
}
//設定視窗
setSize(200,200);
setLocation(300,250);
setResizable(false);//視窗放大按鈕無效
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//註冊功能表單傾聽者
for(int m=0;m<gm.length;m++)
gm[m].addActionListener(bh);
//註冊鍵盤傾聽者
this.addKeyListener(kl);
}
//鍵盤事件處理
private class keyLis implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(isStart == true)
{
String temp = " ";
int [] zero = null;
zero = getZero(box); //取得" "的位置
switch(e.getKeyCode())
{
case LEFT: //按左鍵
if(zero[1]+1<box[zero[0]].length)
{
temp = box[zero[0]][zero[1]+1];
box[zero[0]][zero[1]+1] = " ";
}
break;
case UP: //按上鍵
if(zero[0]+1<box.length)
{
temp = box[zero[0]+1][zero[1]];
box[zero[0]+1][zero[1]] = " ";
}
break;
case RIGHT: //按右鍵
if(zero[1]-1 >= 0)
{
temp = box[zero[0]][zero[1]-1];
box[zero[0]][zero[1]-1] = " ";
}
break;
case DOWN: //按下鍵
if(zero[0]-1 >= 0)
{
temp = box[zero[0]-1][zero[1]];
box[zero[0]-1][zero[1]] = " ";
}
break;
default:
}
box[zero[0]][zero[1]] = temp;
//更新數字顯示的位置
for(int i=0; i< jl.length; i++)
jl[i].setText(" "+box[i/box.length][i%box.length]);
//判斷是否過關
if(box[0][0].equals("1") && box[0][1].equals("2") &&
box[0][2].equals("3") && box[1][0].equals("4") &&
box[1][1].equals("5") && box[1][2].equals("6") &&
box[2][0].equals("7") && box[2][1].equals("8") &&
box[2][2].equals(" ")
)
{
JOptionPane.showMessageDialog(null, "過關了!!", "訊息",
JOptionPane.INFORMATION_MESSAGE);
//判斷是否再玩一次
if(JOptionPane.showConfirmDialog(null, "再玩一次?", "訊息",
JOptionPane.YES_NO_OPTION) == 0
)
{
box = init();
for(int i=0; i< jl.length; i++)//更新數字顯示的位置
jl[i].setText(" "+box[i/box.length][i%box.length]);
}
else System.exit(0); //結束遊戲
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//功能表單事件處理
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == gm[0]) //新局
{
box = init();
for(int i=0; i< jl.length; i++)//更新數字顯示的位置
jl[i].setText(" "+box[i/box.length][i%box.length]);
isStart = true;
gm[1].setEnabled(true);
}
else if(ae.getSource() == gm[1]) //放棄
{
isStart = false;
for(int i=0; i< jl.length; i++)//初始化數字顯示的位置
{
box[i/box.length][i%box.length] = Integer.toString(i+1);
if(box[i/box.length][i%box.length].equals("9")) box[i/box.length][i%box.length] = " ";
jl[i].setText(" "+box[i/box.length][i%box.length]);
}
}
else if(ae.getSource() == gm[2]) //結束遊戲
{
System.exit(0);
}
}
}
public static void main(String [] args)
{
new Piece();
}
}
2.完成圖
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Piece extends JFrame
{
//放數字九官格
private String [][] box = new String[][]{{"1","2","3"},
{"4","5","6"},
{"7","8"," "}};
private keyLis kl = new keyLis();//鍵盤事件
private ButtonHandler bh = new ButtonHandler();//功能表單事件
private final int LEFT = 37,UP = 38, RIGHT = 39, DOWN = 40;
private JLabel [] jl = new JLabel [9];
private JMenuBar jmb;
private JMenu game = new JMenu("遊戲");;
private JMenuItem [] gm = new JMenuItem[3];
private boolean isStart = false; //遊戲開始
//初始化遊戲變數值
private String[][] init()
{
final int ROW = 3, COL = 3;
String [][] boxTemp = new String[][]{{"1","2","3"},
{"4","5","6"},
{"7","8"," "}};
//用亂數打亂數字的排列
for(int count = 0; count < 200; count++)
{
String temp = " ";
int [] zero = null;
int ranNum = (int)(Math.random()*4)+LEFT;
zero = getZero(boxTemp); //取得0的位置
switch(ranNum)
{
case LEFT: //左移
if(zero[1]+1<boxTemp[zero[0]].length)
{
temp = boxTemp[zero[0]][zero[1]+1];
boxTemp[zero[0]][zero[1]+1] = " ";
}
break;
case UP: //上移
if(zero[0]+1<boxTemp.length)
{
temp = boxTemp[zero[0]+1][zero[1]];
boxTemp[zero[0]+1][zero[1]] = " ";
}
break;
case RIGHT: //右移
if(zero[1]-1 >= 0)
{
temp = boxTemp[zero[0]][zero[1]-1];
boxTemp[zero[0]][zero[1]-1] = " ";
}
break;
case DOWN: //下移
if(zero[0]-1 >= 0)
{
temp = boxTemp[zero[0]-1][zero[1]];
boxTemp[zero[0]-1][zero[1]] = " ";
}
break;
default:
}
boxTemp[zero[0]][zero[1]] = temp;
}
return boxTemp;
}
//取得0的位置
private int [] getZero(String [][] boxArr)
{
int location[] = new int[2];
for(int r = 0; r < boxArr.length; r++)
{
for(int c = 0; c < boxArr[r].length; c++)
{
//取得數字0的位置
if(boxArr[r][c].equals(" "))
{
location[0] = r; //" "的x位置
location[1] = c; //" "的y位置
}
}
}
return location;
}
public Piece()
{
super("數字拼圖");
Container c = getContentPane();
c = this.getContentPane();
jmb = new JMenuBar();
this.setJMenuBar(jmb); //加入工具列
//遊戲的選擇項目
jmb.add(game);
gm[0] = new JMenuItem("新局");
gm[1] = new JMenuItem("放棄");
gm[2] = new JMenuItem("結束遊戲");
game.add(gm[0]);
game.add(gm[1]);
game.addSeparator();
game.add(gm[2]);
gm[1].setEnabled(false);
//數字的排為3*3
c.setLayout(new GridLayout(3,3));
for(int i = 0; i < jl.length; i++)
{
jl[i] = new JLabel(" "+box[i/box.length][i%box.length]);
jl[i].setFont(new Font("Serif",Font.BOLD,40));
c.add(jl[i]);
}
//設定視窗
setSize(200,200);
setLocation(300,250);
setResizable(false);//視窗放大按鈕無效
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//註冊功能表單傾聽者
for(int m=0;m<gm.length;m++)
gm[m].addActionListener(bh);
//註冊鍵盤傾聽者
this.addKeyListener(kl);
}
//鍵盤事件處理
private class keyLis implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if(isStart == true)
{
String temp = " ";
int [] zero = null;
zero = getZero(box); //取得" "的位置
switch(e.getKeyCode())
{
case LEFT: //按左鍵
if(zero[1]+1<box[zero[0]].length)
{
temp = box[zero[0]][zero[1]+1];
box[zero[0]][zero[1]+1] = " ";
}
break;
case UP: //按上鍵
if(zero[0]+1<box.length)
{
temp = box[zero[0]+1][zero[1]];
box[zero[0]+1][zero[1]] = " ";
}
break;
case RIGHT: //按右鍵
if(zero[1]-1 >= 0)
{
temp = box[zero[0]][zero[1]-1];
box[zero[0]][zero[1]-1] = " ";
}
break;
case DOWN: //按下鍵
if(zero[0]-1 >= 0)
{
temp = box[zero[0]-1][zero[1]];
box[zero[0]-1][zero[1]] = " ";
}
break;
default:
}
box[zero[0]][zero[1]] = temp;
//更新數字顯示的位置
for(int i=0; i< jl.length; i++)
jl[i].setText(" "+box[i/box.length][i%box.length]);
//判斷是否過關
if(box[0][0].equals("1") && box[0][1].equals("2") &&
box[0][2].equals("3") && box[1][0].equals("4") &&
box[1][1].equals("5") && box[1][2].equals("6") &&
box[2][0].equals("7") && box[2][1].equals("8") &&
box[2][2].equals(" ")
)
{
JOptionPane.showMessageDialog(null, "過關了!!", "訊息",
JOptionPane.INFORMATION_MESSAGE);
//判斷是否再玩一次
if(JOptionPane.showConfirmDialog(null, "再玩一次?", "訊息",
JOptionPane.YES_NO_OPTION) == 0
)
{
box = init();
for(int i=0; i< jl.length; i++)//更新數字顯示的位置
jl[i].setText(" "+box[i/box.length][i%box.length]);
}
else System.exit(0); //結束遊戲
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//功能表單事件處理
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == gm[0]) //新局
{
box = init();
for(int i=0; i< jl.length; i++)//更新數字顯示的位置
jl[i].setText(" "+box[i/box.length][i%box.length]);
isStart = true;
gm[1].setEnabled(true);
}
else if(ae.getSource() == gm[1]) //放棄
{
isStart = false;
for(int i=0; i< jl.length; i++)//初始化數字顯示的位置
{
box[i/box.length][i%box.length] = Integer.toString(i+1);
if(box[i/box.length][i%box.length].equals("9")) box[i/box.length][i%box.length] = " ";
jl[i].setText(" "+box[i/box.length][i%box.length]);
}
}
else if(ae.getSource() == gm[2]) //結束遊戲
{
System.exit(0);
}
}
}
public static void main(String [] args)
{
new Piece();
}
}
2.完成圖
按鈕改成圖片
1.程式碼
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
public Test() {
super("Test");
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel grid_panel = new JPanel(new GridLayout(3, 3));
JButton[] buttons = new JButton[9];
ImageIcon image = new ImageIcon("C360_2013-06-23-15-43-36-777.jpg");
Dimension dimension = new Dimension(image.getIconWidth(), image.getIconHeight());
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(image);
buttons[i].setPreferredSize(dimension);
buttons[i].setMaximumSize(dimension);
buttons[i].setMinimumSize(dimension);
grid_panel.add(buttons[i]);
}
container.add(grid_panel);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
Test app = new Test();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.完成圖
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
public Test() {
super("Test");
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel grid_panel = new JPanel(new GridLayout(3, 3));
JButton[] buttons = new JButton[9];
ImageIcon image = new ImageIcon("C360_2013-06-23-15-43-36-777.jpg");
Dimension dimension = new Dimension(image.getIconWidth(), image.getIconHeight());
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(image);
buttons[i].setPreferredSize(dimension);
buttons[i].setMaximumSize(dimension);
buttons[i].setMinimumSize(dimension);
grid_panel.add(buttons[i]);
}
container.add(grid_panel);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
Test app = new Test();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.完成圖
按鈕照數字走
1.程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
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];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.完成圖
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
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];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].addActionListener(this);
jplPanel.add(button[i]);
}
button[1].setActionCommand("d1");
button[2].setActionCommand("d2");
button[3].setActionCommand("d3");
button[4].setActionCommand("d4");
button[5].setActionCommand("d5");
button[6].setActionCommand("d6");
button[7].setActionCommand("d7");
button[8].setActionCommand("d8");
button[9].setActionCommand("d9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String cmd2 = e.getActionCommand();
String cmd3 = e.getActionCommand();
String cmd4 = e.getActionCommand();
String cmd5 = e.getActionCommand();
String cmd6 = e.getActionCommand();
String cmd7 = e.getActionCommand();
String cmd8 = e.getActionCommand();
String cmd9 = e.getActionCommand();
if (cmd == "d1") {
System.out.println("1");
}
if (cmd2 == "d2") {
System.out.println("2");
}
if (cmd3 == "d3") {
System.out.println("3");
}
if (cmd4 == "d4") {
System.out.println("4");
}
if (cmd5 == "d5") {
System.out.println("5");
}
if (cmd6 == "d6") {
System.out.println("6");
}
if (cmd7 == "d7") {
System.out.println("7");
}
if (cmd8 == "d8") {
System.out.println("8");
}
if (cmd9 == "d9") {
System.out.println("9");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.完成圖
2015年4月18日 星期六
踩地雷
1.程式碼
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game extends JFrame implements ActionListener{
static int mapHeight = 9, mapWidth = 9, bombNum = 10;
static int frameWidth = 400, frameHeight = 400;
private boolean[][] isTurned;
private boolean gameOver;
private boolean gameRunning;
private int[][] aroundBombNum;
private boolean[][] map;//炸彈or空地
private boolean[][] isPressed;
private JButton[][] buttons;
private JPanel centerButtonPanel;
private JLabel gameMessage;
public Game()
{
super("踩地雷");
setSize(frameWidth, frameHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
gameMessage = new JLabel("遊戲進行中");//訊息顯示
add(gameMessage, BorderLayout.NORTH);
centerButtonPanel = new JPanel();
centerButtonPanel.setLayout(new GridLayout(mapHeight, mapWidth));
buttons = new JButton[mapHeight][mapWidth];
//按鈕
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.WHITE);
buttons[i][j].setActionCommand(i+" "+j);
centerButtonPanel.add(buttons[i][j]);
}
add(centerButtonPanel, BorderLayout.CENTER);
JButton re = new JButton("重玩");
re.setActionCommand("regame");
re.addActionListener(this);
add(re, BorderLayout.SOUTH);
gameRunning = false;
}
public void resetGame(int sx,int sy)//清空地圖、隨機地圖、重設按鈕文字
{
gameOver = false;
aroundBombNum = new int[mapHeight][mapWidth];//清空地圖
map = new boolean[mapHeight][mapWidth];
isPressed = new boolean[mapHeight][mapWidth];
isTurned = new boolean[mapHeight][mapWidth];
int bombCount = 0;
while(bombCount < bombNum)
{
int x = (int)(Math.random()*mapHeight);
int y = (int)(Math.random()*mapWidth);
if(sx!=x|sy!=y)
if(!map[x][y])//沒炸彈
{
bombCount++;//放了炸彈
map[x][y] = true;
//向周圍宣布有炸彈
for(int i=x-1; i<=x+1; i++)
for(int j = y-1; j<=y+1; j++)
{
if(i>=0&&i<mapHeight&&j>=0&&j<mapWidth)
aroundBombNum[i][j]++;
}
}
}
System.out.println("地圖建置成功");
printMap();
}
private void clean()
{
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j].setText("");
buttons[i][j].setBackground(Color.WHITE);
}
}
private void printMap()
{
System.out.println("Bombs");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
if(map[i][j])
System.out.print("*");
else
System.out.print("_");
}
System.out.println();
}
System.out.println("Arounds");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
System.out.print(aroundBombNum[i][j]);
}
System.out.println();
}
}
public void freshMessage(String newMessage)
{
System.out.println(newMessage);
gameMessage.setText(newMessage);
}
public void click(int x, int y)//炸彈: 爆炸| 土地 0擴散 1~8不擴散
{
if(map[x][y])//炸彈
{
freshMessage("踩到地雷! 爆炸!");
buttons[x][y].setBackground(Color.RED);
gameOver = true;
return ;
}
else
if(aroundBombNum[x][y]!=0)
{
turn(x,y);
}
else
{
int[][] direct = {{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};//上 左 下 右 左上 左下 右上 右下
int[] queue_x = new int[mapHeight*mapWidth];
int[] queue_y = new int[mapHeight*mapWidth];
int pop = 0, push = 0;
queue_x[push] = x;//填入第一個
queue_y[push] = y;
push++;
while(pop < push)
{
int tx = queue_x[pop];
int ty = queue_y[pop];
//如果 此格是0就繼續擴散
if(aroundBombNum[tx][ty]==0)
for(int s=0; s<8; s++)
{
int fx = tx+direct[s][0];
int fy = ty+direct[s][1];
boolean spread = false;
if(inRange(fx,fy)&&!isTurned[fx][fy])
{
isTurned[fx][fy] = true;
queue_x[push] = fx;
queue_y[push] = fy;
push++;
}
}
pop++;
/*System.out.println("Pop"+pop);
for(int i=0; i<push; i++)
{
System.out.print(" "+"["+queue_x[i]+"]"+ "["+queue_y[i]+"]");
}
System.out.println();*/
}
for(int i=0; i<push; i++)
turn(queue_x[i],queue_y[i]);
}
}
private static boolean inRange(int x, int y)
{
return (x>=0&&x<mapHeight&&y>=0&&y<mapWidth);
}
public void turn(int x, int y)//翻開
{
buttons[x][y].setBackground(Color.LIGHT_GRAY);
if(aroundBombNum[x][y]>0)
buttons[x][y].setText(Integer.toString(aroundBombNum[x][y]));
isTurned[x][y] = true;
isPressed[x][y] = true;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command =e.getActionCommand();
if(command.equals("regame"))
{
clean();
gameRunning = false;
freshMessage("遊戲進行中");
}
else
{
String[] press = command.split(" ");
int x = Integer.parseInt(press[0]);
int y = Integer.parseInt(press[1]);
if(!gameRunning)
{
resetGame(x,y);
gameRunning = true;
}
if(!isPressed[x][y]&&!gameOver)
{
System.out.println("Invoke click "+x+" "+y);
click(x,y);
isPressed[x][y] = true;
}
}
}
public static void main(String[] args)
{
Game frame = new Game();
frame.setVisible(true);
}
}
2.
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Game extends JFrame implements ActionListener{
static int mapHeight = 9, mapWidth = 9, bombNum = 10;
static int frameWidth = 400, frameHeight = 400;
private boolean[][] isTurned;
private boolean gameOver;
private boolean gameRunning;
private int[][] aroundBombNum;
private boolean[][] map;//炸彈or空地
private boolean[][] isPressed;
private JButton[][] buttons;
private JPanel centerButtonPanel;
private JLabel gameMessage;
public Game()
{
super("踩地雷");
setSize(frameWidth, frameHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
gameMessage = new JLabel("遊戲進行中");//訊息顯示
add(gameMessage, BorderLayout.NORTH);
centerButtonPanel = new JPanel();
centerButtonPanel.setLayout(new GridLayout(mapHeight, mapWidth));
buttons = new JButton[mapHeight][mapWidth];
//按鈕
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.WHITE);
buttons[i][j].setActionCommand(i+" "+j);
centerButtonPanel.add(buttons[i][j]);
}
add(centerButtonPanel, BorderLayout.CENTER);
JButton re = new JButton("重玩");
re.setActionCommand("regame");
re.addActionListener(this);
add(re, BorderLayout.SOUTH);
gameRunning = false;
}
public void resetGame(int sx,int sy)//清空地圖、隨機地圖、重設按鈕文字
{
gameOver = false;
aroundBombNum = new int[mapHeight][mapWidth];//清空地圖
map = new boolean[mapHeight][mapWidth];
isPressed = new boolean[mapHeight][mapWidth];
isTurned = new boolean[mapHeight][mapWidth];
int bombCount = 0;
while(bombCount < bombNum)
{
int x = (int)(Math.random()*mapHeight);
int y = (int)(Math.random()*mapWidth);
if(sx!=x|sy!=y)
if(!map[x][y])//沒炸彈
{
bombCount++;//放了炸彈
map[x][y] = true;
//向周圍宣布有炸彈
for(int i=x-1; i<=x+1; i++)
for(int j = y-1; j<=y+1; j++)
{
if(i>=0&&i<mapHeight&&j>=0&&j<mapWidth)
aroundBombNum[i][j]++;
}
}
}
System.out.println("地圖建置成功");
printMap();
}
private void clean()
{
for(int i=0; i<mapHeight; i++)
for(int j=0; j<mapWidth; j++)
{
buttons[i][j].setText("");
buttons[i][j].setBackground(Color.WHITE);
}
}
private void printMap()
{
System.out.println("Bombs");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
if(map[i][j])
System.out.print("*");
else
System.out.print("_");
}
System.out.println();
}
System.out.println("Arounds");
for(int i=0; i<mapHeight; i++)
{
for(int j=0; j<mapWidth; j++)
{
System.out.print(aroundBombNum[i][j]);
}
System.out.println();
}
}
public void freshMessage(String newMessage)
{
System.out.println(newMessage);
gameMessage.setText(newMessage);
}
public void click(int x, int y)//炸彈: 爆炸| 土地 0擴散 1~8不擴散
{
if(map[x][y])//炸彈
{
freshMessage("踩到地雷! 爆炸!");
buttons[x][y].setBackground(Color.RED);
gameOver = true;
return ;
}
else
if(aroundBombNum[x][y]!=0)
{
turn(x,y);
}
else
{
int[][] direct = {{-1,0},{0,-1},{1,0},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};//上 左 下 右 左上 左下 右上 右下
int[] queue_x = new int[mapHeight*mapWidth];
int[] queue_y = new int[mapHeight*mapWidth];
int pop = 0, push = 0;
queue_x[push] = x;//填入第一個
queue_y[push] = y;
push++;
while(pop < push)
{
int tx = queue_x[pop];
int ty = queue_y[pop];
//如果 此格是0就繼續擴散
if(aroundBombNum[tx][ty]==0)
for(int s=0; s<8; s++)
{
int fx = tx+direct[s][0];
int fy = ty+direct[s][1];
boolean spread = false;
if(inRange(fx,fy)&&!isTurned[fx][fy])
{
isTurned[fx][fy] = true;
queue_x[push] = fx;
queue_y[push] = fy;
push++;
}
}
pop++;
/*System.out.println("Pop"+pop);
for(int i=0; i<push; i++)
{
System.out.print(" "+"["+queue_x[i]+"]"+ "["+queue_y[i]+"]");
}
System.out.println();*/
}
for(int i=0; i<push; i++)
turn(queue_x[i],queue_y[i]);
}
}
private static boolean inRange(int x, int y)
{
return (x>=0&&x<mapHeight&&y>=0&&y<mapWidth);
}
public void turn(int x, int y)//翻開
{
buttons[x][y].setBackground(Color.LIGHT_GRAY);
if(aroundBombNum[x][y]>0)
buttons[x][y].setText(Integer.toString(aroundBombNum[x][y]));
isTurned[x][y] = true;
isPressed[x][y] = true;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command =e.getActionCommand();
if(command.equals("regame"))
{
clean();
gameRunning = false;
freshMessage("遊戲進行中");
}
else
{
String[] press = command.split(" ");
int x = Integer.parseInt(press[0]);
int y = Integer.parseInt(press[1]);
if(!gameRunning)
{
resetGame(x,y);
gameRunning = true;
}
if(!isPressed[x][y]&&!gameOver)
{
System.out.println("Invoke click "+x+" "+y);
click(x,y);
isPressed[x][y] = true;
}
}
}
public static void main(String[] args)
{
Game frame = new Game();
frame.setVisible(true);
}
}
2.
2015年4月17日 星期五
單個陣列
1.程式碼
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame{ public static void main(String[]args){ JFrame window=new JFrame("Calculator"); JPanel jplPanel = new JPanel(); JButton button[]=new JButton[10]; for(int i=0;i<button.length;i++){ button[i] = new JButton(); button[i].setSize(50,50); button[i].setText(Integer.toString(i+1)); jplPanel.add(button[i]); } window.getContentPane().add(jplPanel, BorderLayout.CENTER); window.setSize(500,500); window.setVisible(true); }}
2.完成圖
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame{ public static void main(String[]args){ JFrame window=new JFrame("Calculator"); JPanel jplPanel = new JPanel(); JButton button[]=new JButton[10]; for(int i=0;i<button.length;i++){ button[i] = new JButton(); button[i].setSize(50,50); button[i].setText(Integer.toString(i+1)); jplPanel.add(button[i]); } window.getContentPane().add(jplPanel, BorderLayout.CENTER); window.setSize(500,500); window.setVisible(true); }}
2.完成圖
按鈕 YES
1.程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
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];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].setActionCommand("d1");
button[i].addActionListener(this);
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();
if (cmd == "d1") {
System.out.println("Yes....");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator2 implements ActionListener {
public static void main(String[] args) {
new Calculator2();
}
public Calculator2() {
JFrame window=new JFrame("Calculator2");
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];
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].setActionCommand("d1");
button[i].addActionListener(this);
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();
if (cmd == "d1") {
System.out.println("Yes....");
}
}
}
class AdapterDemo extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
2.
2015年4月10日 星期五
小算盤
1.程式碼
import java.awt.*;
import java.awt.event.*;
public class calculating
{
private static Frame frm=new Frame("小算盤");
private static Panel pn1=new Panel(new GridLayout(4,3));
private static Panel pn2=new Panel(new GridLayout(4,1));
private static Label lab=new Label("0",Label.RIGHT);
//依序為清除、加、減、乘、除、等於
private static Button cn,ad,sub,mul,div,amo;
//0~9的數字
private static Button digits[]=new Button[10];
private static long num;//存放結果
private static byte op;//代表運算子
public static void main(String args[])
{
frm.setLayout(null);
frm.setBounds(450,250,160,180);
frm.setResizable(false);
lab.setBounds(20,30,120,20);
lab.setBackground(new Color(240,220,190));
pn1.setBounds(20,60,90,105);
pn2.setBounds(110,60,30,105);
//0~9數字鈕
for(int i=9;i>=0;i--){
digits[i]=new Button(Integer.toString(i));
pn1.add(digits[i]);
digits[i].addActionListener(new ActLis());
}
//清除鈕
cn=new Button("C");
pn1.add(cn);
cn.addActionListener(new ActLis());
//等於鈕
amo=new Button("=");
pn1.add(amo);
amo.addActionListener(new ActLis());
//加鈕
ad=new Button("+");
pn2.add(ad);
ad.addActionListener(new ActLis());
//減鈕
sub=new Button("-");
pn2.add(sub);
sub.addActionListener(new ActLis());
//乘鈕
mul=new Button("*");
pn2.add(mul);
mul.addActionListener(new ActLis());
//除鈕
div=new Button("/");
pn2.add(div);
div.addActionListener(new ActLis());
frm.addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
frm.add(lab);
frm.add(pn1);
frm.add(pn2);
frm.setVisible(true);
}
public static class ActLis implements ActionListener
{
public void actionPerformed(ActionEvent e)throws NumberFormatException,ArithmeticException{
long result;//存放由字串轉成的數值
Button btn=(Button) e.getSource();
try{
//處理數值1-9
for(int i=0;i<=9;i++){
if(btn==digits[i]){
output_digit(digits[i]);
break;
}
}
if(btn==cn){
result=0L;//把儲存的結果歸0
num=0L;
op=0;
lab.setText(Long.toString(num));
}else if(btn==ad){//加
save_num(ad);
op=1;
}else if(btn==sub){//減
save_num(sub);
op=2;
}else if(btn==mul){//乘
save_num(mul);
op=3;
}else if(btn==div){//除
save_num(div);
op=4;
}else if(btn==amo){
result=Long.parseLong(lab.getText());
switch(op){
case 1:
num+=result;
break;
case 2:
num-=result;
break;
case 3:
num*=result;
break;
case 4:
num/=result;
break;
default:
}
result=0L;
//輸出運算後的結果到顯示器
lab.setText(Long.toString(num));
}
}catch(NumberFormatException ne){
//捕捉例外
}catch(ArithmeticException ae){
//捕捉被除數是零的例外
}
}
//輸出數值到顯示器
private void output_digit(Button btn){
lab.setText(Long.toString(Long.parseLong(lab.getText()+btn.getLabel())));
}
//把第一組數值儲存起來
private void save_num(Button oper){
num=Long.parseLong(lab.getText());
lab.setText(Long.toString(0L));
}
}
}
2.
import java.awt.*;
import java.awt.event.*;
public class calculating
{
private static Frame frm=new Frame("小算盤");
private static Panel pn1=new Panel(new GridLayout(4,3));
private static Panel pn2=new Panel(new GridLayout(4,1));
private static Label lab=new Label("0",Label.RIGHT);
//依序為清除、加、減、乘、除、等於
private static Button cn,ad,sub,mul,div,amo;
//0~9的數字
private static Button digits[]=new Button[10];
private static long num;//存放結果
private static byte op;//代表運算子
public static void main(String args[])
{
frm.setLayout(null);
frm.setBounds(450,250,160,180);
frm.setResizable(false);
lab.setBounds(20,30,120,20);
lab.setBackground(new Color(240,220,190));
pn1.setBounds(20,60,90,105);
pn2.setBounds(110,60,30,105);
//0~9數字鈕
for(int i=9;i>=0;i--){
digits[i]=new Button(Integer.toString(i));
pn1.add(digits[i]);
digits[i].addActionListener(new ActLis());
}
//清除鈕
cn=new Button("C");
pn1.add(cn);
cn.addActionListener(new ActLis());
//等於鈕
amo=new Button("=");
pn1.add(amo);
amo.addActionListener(new ActLis());
//加鈕
ad=new Button("+");
pn2.add(ad);
ad.addActionListener(new ActLis());
//減鈕
sub=new Button("-");
pn2.add(sub);
sub.addActionListener(new ActLis());
//乘鈕
mul=new Button("*");
pn2.add(mul);
mul.addActionListener(new ActLis());
//除鈕
div=new Button("/");
pn2.add(div);
div.addActionListener(new ActLis());
frm.addWindowListener(new WindowAdapter(){public void
windowClosing(WindowEvent e){System.exit(0);}});
frm.add(lab);
frm.add(pn1);
frm.add(pn2);
frm.setVisible(true);
}
public static class ActLis implements ActionListener
{
public void actionPerformed(ActionEvent e)throws NumberFormatException,ArithmeticException{
long result;//存放由字串轉成的數值
Button btn=(Button) e.getSource();
try{
//處理數值1-9
for(int i=0;i<=9;i++){
if(btn==digits[i]){
output_digit(digits[i]);
break;
}
}
if(btn==cn){
result=0L;//把儲存的結果歸0
num=0L;
op=0;
lab.setText(Long.toString(num));
}else if(btn==ad){//加
save_num(ad);
op=1;
}else if(btn==sub){//減
save_num(sub);
op=2;
}else if(btn==mul){//乘
save_num(mul);
op=3;
}else if(btn==div){//除
save_num(div);
op=4;
}else if(btn==amo){
result=Long.parseLong(lab.getText());
switch(op){
case 1:
num+=result;
break;
case 2:
num-=result;
break;
case 3:
num*=result;
break;
case 4:
num/=result;
break;
default:
}
result=0L;
//輸出運算後的結果到顯示器
lab.setText(Long.toString(num));
}
}catch(NumberFormatException ne){
//捕捉例外
}catch(ArithmeticException ae){
//捕捉被除數是零的例外
}
}
//輸出數值到顯示器
private void output_digit(Button btn){
lab.setText(Long.toString(Long.parseLong(lab.getText()+btn.getLabel())));
}
//把第一組數值儲存起來
private void save_num(Button oper){
num=Long.parseLong(lab.getText());
lab.setText(Long.toString(0L));
}
}
}
2.
陣列做按鈕
1.
import javax.swing.*;
import java.awt.*;
public class jtest {
JButton btn[][];
JFrame mainFrame;
JPanel panel;
public jtest() {
panel = new JPanel(new GridLayout(8, 8));
btn = new JButton[8][8];
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
//產生JButton實體物件
btn[x][y] = new JButton(x + "," + y);
panel.add(btn[x][y]);
}
}
mainFrame = new JFrame("JButton Array");
mainFrame.getContentPane().add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new jtest();
}
}
2.完成圖
import javax.swing.*;
import java.awt.*;
public class jtest {
JButton btn[][];
JFrame mainFrame;
JPanel panel;
public jtest() {
panel = new JPanel(new GridLayout(8, 8));
btn = new JButton[8][8];
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
//產生JButton實體物件
btn[x][y] = new JButton(x + "," + y);
panel.add(btn[x][y]);
}
}
mainFrame = new JFrame("JButton Array");
mainFrame.getContentPane().add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new jtest();
}
}
2.完成圖
按鈕*9
1.程式碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonDemo2 {
JFrame jtfMainFrame;
JButton jbnButton1, jbnButton2, jbnButton3, jbnButton4, jbnButton5, jbnButton6, jbnButton7, jbnButton8, jbnButton9;
JTextField jtfInput;
JPanel jplPanel;
public JButtonDemo2() {
jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(50, 50);
jbnButton1 = new JButton("Button 1");
jbnButton2 = new JButton("Button 2");
jbnButton3 = new JButton("Button 3");
jbnButton4 = new JButton("Button 4");
jbnButton5 = new JButton("Button 5");
jbnButton6 = new JButton("Button 6");
jbnButton7 = new JButton("Button 7");
jbnButton8 = new JButton("Button 8");
jbnButton9 = new JButton("Button 9");
jtfInput = new JTextField(20);
jplPanel = new JPanel();
jbnButton1.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
jbnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 1!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});
jbnButton3.setMnemonic(KeyEvent.VK_I);
jbnButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 3!");
}
});
jbnButton4.setMnemonic(KeyEvent.VK_I);
jbnButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 4!");
}
});
jbnButton5.setMnemonic(KeyEvent.VK_I);
jbnButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 5!");
}
});
jbnButton6.setMnemonic(KeyEvent.VK_I);
jbnButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 6!");
}
});
jbnButton7.setMnemonic(KeyEvent.VK_I);
jbnButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 7!");
}
});
jbnButton8.setMnemonic(KeyEvent.VK_I);
jbnButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 8!");
}
});
jbnButton9.setMnemonic(KeyEvent.VK_I);
jbnButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 9!");
}
});
jplPanel.setLayout(new FlowLayout());
jplPanel.add(jtfInput);
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtfMainFrame.pack();
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
JButtonDemo2 application = new JButtonDemo2();
}
}
2.完成圖
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButtonDemo2 {
JFrame jtfMainFrame;
JButton jbnButton1, jbnButton2, jbnButton3, jbnButton4, jbnButton5, jbnButton6, jbnButton7, jbnButton8, jbnButton9;
JTextField jtfInput;
JPanel jplPanel;
public JButtonDemo2() {
jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(50, 50);
jbnButton1 = new JButton("Button 1");
jbnButton2 = new JButton("Button 2");
jbnButton3 = new JButton("Button 3");
jbnButton4 = new JButton("Button 4");
jbnButton5 = new JButton("Button 5");
jbnButton6 = new JButton("Button 6");
jbnButton7 = new JButton("Button 7");
jbnButton8 = new JButton("Button 8");
jbnButton9 = new JButton("Button 9");
jtfInput = new JTextField(20);
jplPanel = new JPanel();
jbnButton1.setMnemonic(KeyEvent.VK_I); //Set ShortCut Keys
jbnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 1!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});
jbnButton2.setMnemonic(KeyEvent.VK_I);
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 2!");
}
});
jbnButton3.setMnemonic(KeyEvent.VK_I);
jbnButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 3!");
}
});
jbnButton4.setMnemonic(KeyEvent.VK_I);
jbnButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 4!");
}
});
jbnButton5.setMnemonic(KeyEvent.VK_I);
jbnButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 5!");
}
});
jbnButton6.setMnemonic(KeyEvent.VK_I);
jbnButton6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 6!");
}
});
jbnButton7.setMnemonic(KeyEvent.VK_I);
jbnButton7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 7!");
}
});
jbnButton8.setMnemonic(KeyEvent.VK_I);
jbnButton8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 8!");
}
});
jbnButton9.setMnemonic(KeyEvent.VK_I);
jbnButton9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtfInput.setText("Button 9!");
}
});
jplPanel.setLayout(new FlowLayout());
jplPanel.add(jtfInput);
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtfMainFrame.pack();
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
JButtonDemo2 application = new JButtonDemo2();
}
}
2.完成圖
訂閱:
文章 (Atom)