在我“重新启动”我的地图后,我无法让 actionlistener 在某些按钮上工作。问题是 btns[1] 到 btns[9] 根本不起作用!我的变量可能有问题,我不确定。我什么都试过了。这些按钮在我启动新地图后根本不起作用(在我按下按钮 btns[0] .. New Map 之后)。这是我的代码,希望你们帮助我。New Board(null) - 如果构造函数中有空值,它应该创建一揽子地图,我已经在 Board 类的构造函数中编码(我想这并不重要,因为它在我第一次启动新游戏时工作(无效的) )。
如果您发现无法找出它为什么不起作用,我将我的整个游戏上传到 sendspace = http://www.sendspace.com/file/pvwtoo - Jar 形式, http://www.sendspace.com/file /l18khb - BlueJ 形式 - 如有必要,可以更好地协调。非常感谢您的所有帮助。卢克
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Game extends JFrame implements ActionListener
{
Board b;
Menu m;
Container c = getContentPane();
JPanel pnl;
ImageIcon ii;
JLabel jl;
JTextArea jt;
private JButton [] btns = new JButton[10];
String selectMore = "Select more = false";
int posx,posy;
public Game(Map m) {
createGui(m);
}
public void createGui(Map mm)
{
b = new Board(mm);
//m = new Menu();
c.add(b);
pnl = new JPanel();
ii = new ImageIcon(this.getClass().getResource("menu.png"));
pnl.setLayout(new GridLayout(16,10));
pnl.setSize(100,608);
//pnl.add(m);
pnl.setBackground(Color.BLACK);
c.add(pnl,BorderLayout.LINE_END);
setTitle("Strgame");
c.setBackground(Color.BLACK);
//this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 608);
setLocationRelativeTo(null);
setResizable(false);
//setUndecorated(true);
//setExtendedState(Frame.MAXIMIZED_BOTH);
setVisible(true);
manageButtons();
infoPanel();
pnl.requestFocusInWindow();
requestFocusInWindow();
}
public void manageButtons()
{
if(pnl!=null){pnl.removeAll();}
btns[0] = new JButton("New Map");
btns[1] = new JButton("Change XY to House 1");
btns[2] = new JButton("Change XY to House 2");
btns[3] = new JButton("Change XY to Road 1");
btns[4] = new JButton("Change XY to Road 2");
btns[5] = new JButton("Change XY to Road 3");
btns[6] = new JButton("Change XY to Grass");
btns[7] = new JButton("Get info of selected");
btns[9] = new JButton(selectMore);
for(int i = 0; i < 10; i++)
{
if(btns[i] != null)
{
btns[i].addActionListener(this);
btns[i].setPreferredSize(new Dimension(213,10));
btns[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
btns[i].setBackground(Color.lightGray);
pnl.add(btns[i]);
}
}
}
public void infoPanel()
{
jl = new JLabel("Info");
jl.setForeground(Color.lightGray);
jl.setHorizontalAlignment(SwingConstants.CENTER);
pnl.add(jl);
jt = new JTextArea();
//jt.setPreferredSize(new Dimension(211,10));
//jt.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
jt.setBackground(Color.lightGray);
//jt.setHorizontalAlignment(SwingConstants.CENTER);
jt.setMargin(new Insets(2,2,2,2));
jt.setEditable(false);
jt.setText(" Not selected");
pnl.add(jt);
}
public int posx()
{
int [] pom = b.lastPosSelected();
posx = pom[0];
return posx;
}
public int posy()
{
int [] pom = b.lastPosSelected();
posy = pom[1];
return posy;
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if(action.equals("New Map"))
{
createGui(null);
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to House 1"))
{
b.changexy("house");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to House 2"))
{
b.changexy("house1");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to Road 1"))
{
b.changexy("road");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to Road 2"))
{
b.changexy("road1");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to Road 3"))
{
b.changexy("road2");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Change XY to Grass"))
{
b.changexy("grass");
b.revalidate();
b.repaint();
c.repaint();
}
else if(action.equals("Select more = false"))
{
b.selMore();
b.repaint();
selectMore = "Select more = true";
btns[9].setText(selectMore);
}
else if(action.equals("Select more = true"))
{
b.selMore();
b.repaint();
selectMore = "Select more = false";
btns[9].setText(selectMore);
}
else if(action.equals("Get info of selected"))
{
jt.setText(" Name: "+b.getSelected()+"\n Costs: "+b.getSelectedCost());
}
}
public static void main(String[] args) {
new Game(null);
}
}