我正在为可联网的战舰类游戏编写 gui(是的,我也是)。如果我右键单击设置一个航点,所有的 mapLabels 都会消失并且我没有得到它,因为所有右键单击应该做的就是向服务器发送一些字符串,而不是以任何方式影响显示本身。如果我调用 panel.repaint(); 窗格.验证();设置航路点后,所有内容都会再次显示,但性能接近于不可能像那样慢。
我意识到,这是很多代码,但我无法想象 FieldListener 中的问题,尽管它必须是。因此,我不知道要发布什么。如果你想看别的东西,你可以要求它......
这是我们代码的一部分,很可能是造成问题的原因:
/**
* The listener for a field.
* @author Andris
*
*/
private class FieldListener implements MouseListener {
private int[] xy;
private int[] pressedPos = new int[] {-1,-1};
@Override
public void mousePressed(MouseEvent evt) {
Field field = map.getField(xy[0],xy[1]);
pressedPos = xy.clone();
switch(evt.getButton()){
case MouseEvent.BUTTON1://-------well, BUTTON1......
break;
case MouseEvent.BUTTON3:
switch(selBtn){
case 2://---------------this is the case
client.Out("some string");// this sends to the server, nothing else...
break;
}
break;
}
}
在服务器回答之后执行(在来自不同包的完全不同的类中,并且所有消失的字段都是私有的):
public class Server {
client.Out(cfg.chat_server+"you have set a waypoint at ("+x+","+y+").");
public class ChatFrame extends JPanel {
public void Out(String nextString){
text.append(" "+nextString);
text.append("\n");
JScrollBar scroll = display.getVerticalScrollBar();
if(!scroll.getValueIsAdjusting())scroll.setValue(Integer.MAX_VALUE);
}
这是自定义绘制方法(也许这是因为 ImageObserver 是错误的):
private class MapLabel extends JLabel{ //------------- in MapFrame
private Field field;
private int[] xy;
public void paint(Graphics g){
Image image = getImageNsetToolTip();
Graphics2D g2D=(Graphics2D)g;
g2D.drawImage(image, 0, 0, actImgSize, actImgSize, this);
g2D.setClip(0, 0, actImgSize, actImgSize);
super.paint(g2D);
}
/**
* gets the proper image and sets the tool-tip
* @return
*/
private Image getImageNsetToolTip(){
Image result;
String toolTip = "("+xy[0]+","+xy[1]+")";
TileType type = field.getType();
switch(type){
case harbor:
result=harborImg[0];
break;
case land:
//------------------------------etc...
this.setToolTipText(toolTip);
return result;
}
这是其余的一些:
...lots of imports...
/**
* This is the frame in which the GameMap is displayed.
* @author Andris
*
*/
@SuppressWarnings("serial")
public class MapFrame extends JPanel {
...lots of variables...
/**
* Creates the frame, but doesn't make it visible yet.
* @param window the GameWindow in which this frame will be embedded.
* @param client the client who runs this.
*/
public MapFrame(GameWindow window, Client client){
...lots of variables initialized...
panel = new JPanel();
pane = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
buttons = new JButton[nButtons];
buttonListener = new ButtonListener();
for(int i=0; i<nButtons; i++){
buttons[i] = new JButton(buttonTexts[i]);
buttons[i].setName(buttonTexts[i].replace(' ', '_'));
buttons[i].addActionListener(buttonListener);
buttonPanel.add(buttons[i]);
}