import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class window extends JFrame implements Runnable{
//相应的组件和一些变量
final int MAX=6;
JButton[][] point=new JButton[MAX][MAX];
static JLabel warn=new JLabel("无效!拥有金钱数应为正整数,请重新输入");
static JLabel tip=new JLabel("绿,蓝,红分别代表三个人的起点,黄色代表终点!");
static JComboBox rf=new JComboBox();
static JComboBox cf=new JComboBox();
static JComboBox bx=new JComboBox();
static JComboBox by=new JComboBox();
static JComboBox ax=new JComboBox();
static JComboBox ay=new JComboBox();
static JComboBox ey=new JComboBox();
static JComboBox ex=new JComboBox();
static JButton sure=new JButton("确定");
static JButton help=new JButton("帮助");
static JTextField money=new JTextField(5);
static int jeffersonx=0,jeffersony=0;
static int bobx=0,boby=0;
static int alicex=0,alicey=0;
static int exitx,exity;
static int cash=0;
PipedInputStream inStream;
maze migong;
public window(){
//设计主界面
//当输入错误信息会给出提示
//页面会出现相应的提示提醒用户
super("迷宫升级版");
setBounds(500, 300, 650, 600);
setLayout(null);
migong=new maze();
JLabel labelcashbox=new JLabel("请输入拥有金钱数");
labelcashbox.setBounds(40, 20, 110, 30);
money.setBounds(150, 20, 80, 30);
this.add(money);
this.add(labelcashbox);
warn.setVisible(false);
money.addActionListener(//输入金钱的监听,如果输入的不是正整数将给出提示
new ActionListener()
{public void actionPerformed(ActionEvent e) {
warn.setVisible(false);
int j;
try{
j=Integer.parseInt(money.getText());
if(j<=0)warn.setVisible(true);
else cash=j;
}
catch (NumberFormatException e1)
{
warn.setVisible(true);
}
}
});
warn.setBounds(10, 70, 250, 30);
this.add(warn);
JLabel jef=new JLabel("请选择jefferson的起始坐标:");
jef.setBounds(0, 100, 170, 30);
this.add(jef);
JLabel jr=new JLabel("X:");
jr.setBounds(70, 130, 30, 30);
this.add(jr);
JLabel jc=new JLabel("Y:");
jc.setBounds(150, 130, 20, 30);
this.add(jc);
for(int r=0;r<MAX;r++)
{
rf.addItem(r);
}
rf.setBounds(100, 130, 50, 30);
this.add(rf);
for(int c=0;c<MAX;c++)
{
cf.addItem(c);
}
cf.setBounds(170, 130, 50, 30);
this.add(cf);
JLabel bobxy=new JLabel("请选择bob的起始坐标:");
bobxy.setBounds(0, 160, 170, 30);
this.add(bobxy);
JLabel bobx=new JLabel("X:");
bobx.setBounds(70, 190, 30, 30);
this.add(bobx);
JLabel boby=new JLabel("Y:");
boby.setBounds(150, 190, 20, 30);
this.add(boby);
for(int r=0;r<MAX;r++)
{
bx.addItem(r);
}
bx.setBounds(100, 190, 50, 30);
this.add(bx);
for(int c=0;c<MAX;c++)
{
by.addItem(c);
}
by.setBounds(170, 190, 50, 30);
this.add(by);
JLabel alicexy=new JLabel("请选择alice的起始坐标:");
alicexy.setBounds(0, 220, 170, 30);
this.add(alicexy);
JLabel alicex=new JLabel("X:");
alicex.setBounds(70, 250, 30, 30);
this.add(alicex);
JLabel alicey=new JLabel("Y:");
alicey.setBounds(150, 250, 20, 30);
this.add(alicey);
for(int r=0;r<MAX;r++)
{
ax.addItem(r);
}
ax.setBounds(100, 250, 50, 30);
this.add(ax);
for(int c=0;c<MAX;c++)
{
ay.addItem(c);
}
ay.setBounds(170, 250, 50, 30);
this.add(ay);
JLabel exitxy=new JLabel("请选择终点的坐标:");
exitxy.setBounds(0, 280, 170, 30);
this.add(exitxy);
JLabel exx=new JLabel("X:");
exx.setBounds(70, 310, 30, 30);
this.add(exx);
JLabel exy=new JLabel("Y:");
exy.setBounds(150, 310, 20, 30);
this.add(exy);
for(int r=0;r<MAX;r++)
{
ex.addItem(r);
}
ex.setBounds(100, 310, 50, 30);
this.add(ex);
for(int c=0;c<MAX;c++)
{
ey.addItem(c);
}
ey.setBounds(170, 310, 50, 30);
this.add(ey);
JButton set=new JButton("设置");
set.addActionListener(new setxy());
set.setBounds(60, 360, 80, 30);
this.add(set);
for(int i=0;i<MAX;i++){
for(int j=0;j<MAX;j++){
point[i][j]=new JButton(String.valueOf(migong.maze[i][j]));
point[i][j].setBounds(250+i*60, 20+j*60, 60, 60);
this.add(point[i][j]);
}
}
sure.setBounds(300, 500, 80, 30);
sure.addActionListener(new action());
this.add(sure);
help.setBounds(400, 500, 80, 30);
help.addActionListener(new action());
this.add(help);
tip.setVisible(false);
tip.setBounds(300, 400, 300, 30);
add(tip);
this.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
setVisible(true);
}
public class setxy implements ActionListener{//“设置”按钮监听类
//通过监听设置按钮来确定起点和终点的坐标
//并且将迷宫图中的相应点设置不同的颜色
public void actionPerformed(ActionEvent e) {
point[rf.getSelectedIndex()][cf.getSelectedIndex()].setBackground(Color.GREEN);
point[bx.getSelectedIndex()][by.getSelectedIndex()].setBackground(Color.BLUE);
point[ax.getSelectedIndex()][ay.getSelectedIndex()].setBackground(Color.RED);
point[ex.getSelectedIndex()][ey.getSelectedIndex()].setBackground(Color.ORANGE);
jeffersonx=rf.getSelectedIndex();
jeffersony=cf.getSelectedIndex();
bobx=bx.getSelectedIndex();
boby=by.getSelectedIndex();
alicex=ax.getSelectedIndex();
alicey=ay.getSelectedIndex();
exitx=ex.getSelectedIndex();
exity=ey.getSelectedIndex();
tip.setVisible(true);
}
}
public class action implements ActionListener{//操作监听类
//监听是点击的确定还是帮助按钮
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==sure)//点击确定产生寻找路径过程
{
migong.setCash(cash);
migong.inpoint(0, jeffersonx, jeffersony);
migong.inpoint(1, bobx, boby);
migong.inpoint(2, alicex, alicey);
migong.outpoint(exitx, exity);
domaze();
}
if(source==help)//点击帮助显示提示信息
{
JFrame frame1=new JFrame("帮助?");
JTextArea he=new JTextArea(20,40);
he.setLineWrap(true);
JScrollPane hel=new JScrollPane(he);
String f2="该软件为迷宫升级版:" +
"设置初始金钱数后,通过点击迷宫上的点,设置三个人的初始位置" +
",第四个点击的点为目的地的点,点击确定后刚开始设置的三个点会根据寻找的"
+"的路径而移动,最后到达目的地!拥有金钱数应为正整数,点击回车键后否则系统" +
"会根据输入请况给予提示。";
he.setText(f2);
frame1.getContentPane( ).add(hel);
frame1.setSize(300, 300);
frame1.setVisible(true);
}
}
}
public void domaze(){//显示出三个人的寻找路径的过程
PipedOutputStream outStream;
try{
outStream = new PipedOutputStream();
inStream = new PipedInputStream(outStream);
migong.write(outStream);
new Thread(this).start();
migong.run();
}catch( IOException e1 ){ }
}
public void run() {//三个人寻找路径的过程
//不同的颜色代表不同的人,通过颜色的运动来表示人走
java swing 做的迷宫问题升级版
4星 · 超过85%的资源 需积分: 9 116 浏览量
2011-07-06
11:49:11
上传
评论
收藏 15KB RAR 举报
美丽人生
- 粉丝: 61
- 资源: 72