我用4个jbuttons做了一个jframe, 一旦我键入一个特定的键,它就会删除jbutton和其他按钮。
然而,当我删除jbutton时,它不会直接消失, 但如果我重新调整或最小化jframe窗口,它就会这样做。
我的代码有问题吗? 或者我的电脑有问题吗?
public class View extends JFrame {
GamePanel _gamePanel;
DataModel _dm;
GameTile _tile1, _tile2, _tile3, _tile4;
ArrayList<GameTile> _gameTileList;
public View(DataModel dm) {
this._dm = dm;
_gamePanel = new GamePanel();
_tile1 = new GameTile('a');
_tile1.addKeyListener(new PressKeyHandler(this));
_tile2 = new GameTile('b');
_tile2.addKeyListener(new PressKeyHandler(this));
_tile3 = new GameTile('c');
_tile3.addKeyListener(new PressKeyHandler(this));
_tile4 = new GameTile('d');
_tile4.addKeyListener(new PressKeyHandler(this));
_gameTileList = new ArrayList<GameTile>();
this.setSize(300, 500);
this.setVisible(true);
_gamePanel.add(_tile1);
_gamePanel.add(_tile2);
_gamePanel.add(_tile3);
_gamePanel.add(_tile4);
// this.addKeyListener(new PressKeyHandler(this));
this.add(_gamePanel);
// this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_dm.setView(this);
}
public GamePanel get_gamePanel() {
return _gamePanel;
}
public GameTile get_gameTile(char letter) {
GameTile tileToReturn = new GameTile(' ');
switch (letter) {
case 'a':
tileToReturn = _tile1;
break;
case 'b':
tileToReturn = _tile2;
break;
case 'c':
tileToReturn = _tile3;
break;
case 'd':
tileToReturn = _tile4;
break;
}
return tileToReturn;
}
public ArrayList<GameTile> get_gameTileList() {
return _gameTileList;
}
public void set_gameTileList(ArrayList<GameTile> _gameTileList) {
this._gameTileList = _gameTileList;
}
public void removeTile(JPanel panel) {
}
public void update() {
_gamePanel.remove(_gameTileList.get(0));
// _gameTileList.get(0).setText(" ");
// _gameTileList.get(0).setBackground(Color.BLACK);
emptyTile(_gameTileList.get(0));
_gamePanel.add(_gameTileList.get(0), 0);
_gameTileList.remove(0);
}
public void emptyTile(GameTile tile) {
tile.setText(" ");
tile.setBorder(BorderFactory.createEmptyBorder());
tile.setBackground(Color.BLACK);
}
}
public class PressKeyHandler implements KeyListener {
View _view;
public PressKeyHandler(View view) {
this._view = view;
}
@Override
public void keyTyped(KeyEvent e) {
switch (e.getKeyChar()) {
case 'a':
_view.get_gameTileList().add(_view.get_gameTile('a'));
_view.update();
break;
case 'b':
_view.get_gameTileList().add(_view.get_gameTile('b'));
_view.update();
break;
case 'c':
_view.get_gameTileList().add(_view.get_gameTile('c'));
_view.update();
break;
case 'd':
_view.get_gameTileList().add(_view.get_gameTile('d'));
_view.update();
break;
default:
System.out.println("no tiles for tyeped letterl");
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
我只是一个新手,但是你试过把frame.pack();你在哪里删除按钮?几天前我遇到了类似的问题,在我放大框架之前,按钮不会出现。然后我添加了frame.pack();在那里我添加了按钮,它会自动调整大小。