JPanel GUI正在自我更新

时间:2014-05-20 19:31:05

标签: java swing user-interface timer jpanel

我正在制作一个简单的点击式游戏。问题是,我的JPanel忽略了我设置为每秒更新的Swing Timer,而是自己更新每毫秒,即使我删除了计时器。除了计时器的监听器之外,不会在任何地方调用重绘,因此JPanel在没有我的指令的情况下重新绘制自己,是否有人知道为什么会发生这种情况?

    public GameGUI() {

    sectors.add(new Sector());

    add(recourseMonitor);
    recourseMonitor.setOpaque(false);
    recourseMonitor.setFocusable(false);

    add(buyHShip);
    buyHShip.setToolTipText("Extracts Hydrogren directly from the core of a star.");
    buyHShip.addActionListener(this);

    add(buyCShip);
    buyCShip.setToolTipText("Extracts Carbon from indigenous lifeforms.");
    buyCShip.addActionListener(this);
    add(buyIShip);

    add(buyUShip);
    buyUShip.setToolTipText("Extracts Uranium from nuclear waste leftovers.");
    buyUShip.addActionListener(this);

    buyIShip.setToolTipText("Strip mines nearby planets for Iron.");
    buyIShip.addActionListener(this);

    add(buyAShip);
    buyAShip.setToolTipText("Collects Antimatter from the fabric of the universe.");
    buyAShip.addActionListener(this);

    add(buyCookieShip);
    buyCookieShip.setToolTipText("Rips holes in the space-time continuum to "
            + "bring back cookies from an ancient civilization.");
    buyCookieShip.addActionListener(this);

    add(buyTShip);
    buyTShip.addActionListener(this);

    add(cashButton);
    cashButton.addActionListener(this);

    add(sellHplus);
    sellHplus.addActionListener(this);

    add(sellCplus);
    sellCplus.addActionListener(this);

    add(sellIplus);
    sellIplus.addActionListener(this);

    add(sellUplus);
    sellUplus.addActionListener(this);

    add(sellAplus);
    sellAplus.addActionListener(this);

    add(sellCookieplus);
    sellCookieplus.addActionListener(this);

    add(sellHminus);
    sellHminus.addActionListener(this);

    add(sellCminus);
    sellCminus.addActionListener(this);

    add(sellIminus);
    sellIminus.addActionListener(this);

    add(sellUminus);
    sellUminus.addActionListener(this);

    add(sellAminus);
    sellAminus.addActionListener(this);

    add(sellCookieminus);
    sellCookieminus.addActionListener(this);

    add(hBeingSold);
    add(cBeingSold);
    add(iBeingSold);
    add(uBeingSold);
    add(aBeingSold);
    add(cookiesBeingSold);

    if (debug == true) {
        add(testButton);
        testButton.addActionListener(this);
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    buyHShip.setText("[" + sectors.get(0).getStation().gethShip() + "] "
            + "Buy Hydrogen Extractor: " + hPrice);
    buyCShip.setText("[" + sectors.get(0).getStation().getcShip() + "] "
            + "Buy Carbon Miner: " + cPrice);
    buyUShip.setText("[" + sectors.get(0).getStation().getuShip() + "] "
            + "Buy Uranium Miner: " + uPrice);
    buyIShip.setText("[" + sectors.get(0).getStation().getiShip() + "] "
            + "Buy Iron Miner: " + iPrice);
    buyAShip.setText("[" + sectors.get(0).getStation().getaShip() + "] "
            + "Buy Antimatter Collector: " + aPrice);
    buyCookieShip.setText("[" + sectors.get(0).getStation().getcChip() + "] "
            + "Buy Cookie Drone: " + cookiePrice);
    buyTShip.setText("[" + sectors.get(0).getStation().gettShip() + "] "
            + "Buy Trading Ship: " + tradePrice);

    hBeingSold.setText("Hydrogen being sold: " + tradeH);
    cBeingSold.setText("Carbon being sold: " + tradeC);
    iBeingSold.setText("Iron being sold: " + tradeI);
    uBeingSold.setText("Uranium being sold: " + tradeU);
    aBeingSold.setText("Antimatter being sold: " + tradeA);
    cookiesBeingSold.setText("Cookies being sold: " + tradeCookie);

    recourseMonitor.setText("Space Cash: " + spaceCash
            + "\nEnergy: " + energy
            + "\nHydrogen: " + hydrogen
            + "\nCarbon: " + carbon
            + "\nUranium: " + uranium
            + "\nIron: " + iron
            + "\nAntimatter: " + antimatter
            + "\nCookies: " + cookies);

    drawItems(g);

    playGame();

}

public void drawItems(Graphics g) {
    super.paintComponent(g);

    recourseMonitor.setLocation(5, 0);

    buyHShip.setLocation(getWidth() - buyHShip.getWidth(), 0);
    buyCShip.setLocation(getWidth() - buyCShip.getWidth(), buyCShip.getHeight() * 2);
    buyIShip.setLocation(getWidth() - buyIShip.getWidth(), buyIShip.getHeight() * 4);
    buyUShip.setLocation(getWidth() - buyUShip.getWidth(), buyUShip.getHeight() * 6);
    buyAShip.setLocation(getWidth() - buyAShip.getWidth(), buyAShip.getHeight() * 8);
    buyCookieShip.setLocation(getWidth() - buyCookieShip.getWidth(), buyCookieShip.getHeight() * 10);
    buyTShip.setLocation(getWidth() - buyTShip.getWidth(), buyTShip.getHeight() * 12);

    cashButton.setLocation(getWidth() / 2 - cashButton.getWidth() / 2, 10);
    testButton.setLocation(getWidth() / 2 - testButton.getWidth() / 2, testButton.getHeight() + 20);

    sellHplus.setLocation(5, 200);
    hBeingSold.setLocation(sellHplus.getWidth() + 20, 200);
    sellHminus.setLocation(sellHplus.getWidth() + hBeingSold.getWidth() + 40, 200);

    sellCplus.setLocation(5, 250);
    cBeingSold.setLocation(sellCplus.getWidth() + 20, 250);
    sellCminus.setLocation(sellCplus.getWidth() + cBeingSold.getWidth() + 40, 250);

    sellIplus.setLocation(5, 300);
    iBeingSold.setLocation(sellIplus.getWidth() + 20, 300);
    sellIminus.setLocation(sellIplus.getWidth() + iBeingSold.getWidth() + 40, 300);

    sellUplus.setLocation(5, 350);
    uBeingSold.setLocation(sellUplus.getWidth() + 20, 350);
    sellUminus.setLocation(sellUplus.getWidth() + uBeingSold.getWidth() + 40, 350);

    sellAplus.setLocation(5, 400);
    aBeingSold.setLocation(sellAplus.getWidth() + 20, 400);
    sellAminus.setLocation(sellAplus.getWidth() + aBeingSold.getWidth() + 40, 400);

    sellCookieplus.setLocation(5, 450);
    cookiesBeingSold.setLocation(sellCookieplus.getWidth() + 20, 450);
    sellCookieminus.setLocation(sellCookieplus.getWidth() + cookiesBeingSold.getWidth() + 40, 450);

}

public void playGame() {

    for (int i = 0; i < sectors.size(); i++) {
        hIncome = sectors.get(i).getStation().gethShip() - tradeH;
        cIncome = sectors.get(i).getStation().getcShip() - tradeC;
        iIncome = sectors.get(i).getStation().getiShip() - tradeI;
        uIncome = sectors.get(i).getStation().getuShip() - tradeU;
        aIncome = sectors.get(i).getStation().getaShip() - tradeA;
        cookieIncome = sectors.get(i).getStation().getcChip() - tradeCookie;
        cashIncome = (tradeH * 5) + (tradeC * 20) + (tradeI * 100)
                + (tradeU * 500) + (tradeA * 1500) + (tradeCookie * 5000);
    }

    for (int i = 0; i < sectors.size(); i++) {
        spaceCash += cashIncome;
        energy += energyIncome;
        hydrogen += hIncome;
        carbon += cIncome;
        iron += iIncome;
        uranium += uIncome;
        antimatter += aIncome;
        cookies += cookieIncome;
    }



}

有代码(不包括动作监听器以及变量和按钮的初始化)。

1 个答案:

答案 0 :(得分:1)

绘画方法仅用于绘画:

  1. 不要调用setText(....)
  2. 等方法
  3. 不要调用setLocation()
  4. 等方法

    更改组件的属性将导致重新绘制组件,这可能会导致无限循环。

    不要在drawItems()方法中调用super.paintComponent()。除非在paintComponent()方法中调用super.paintComponent(),否则不应直接调用绘制方法。

    不应该从paintComponent()方法调用playGame()方法。这也是你游戏的逻辑。该代码应该从控制游戏的Timer中调用。