2013-10-11 19 views
0

我有兩個屏幕,兩個屏幕都擴展爲GameScreen,而這又擴展了JPanel。我的NavigationFrame類正確執行,但我的InternalSensorsFrame正在通過我的按鈕繪製灰色背景矩形,以便按鈕隱藏,並且只在您將鼠標懸停在按鈕上時才短暫閃爍。這是我的代碼。任何人都可以告訴我爲什麼這個例子正在工作,另一個不是,我該如何解決它?被paint隱藏的JButton組件

public class NavigationFrame extends GameScreen { 

... 
    public NavigationFrame() { 
     //super(parent); 
     addKeyListener(new TAdapter()); 
     //background 
     img = new ImageIcon(Home.resources.getClass().getResource(imageName)); 
     bg = new BufferedImage(
     img.getIconWidth(), 
     img.getIconHeight(), 
     BufferedImage.TYPE_4BYTE_ABGR); 
     Graphics gg = bg.createGraphics(); 
     img.paintIcon(null, gg, 0,0); 
     gg.dispose(); 
     RenderingHints rh = 
       new RenderingHints(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 

    gameFont = new Font("Helvetica", Font.BOLD, 14); 

    //Set Viewscreen to blockmapview 
    if (viewScreen == null){ 
     setViewScreen(new ScrollShipScreen(this, 0)); 
    } 
    viewScreen.setBounds(150, 50, 500, 500); 
    add(viewScreen); 

    //Buttons 
    btnMainComputer = new JButton("Main Computer"); 
    btnMainComputer.setBounds(140, 560, 120, 23); 
    btnMainComputer.addActionListener(this); 
    add(btnMainComputer); 

    btnInternalSensors = new JButton("Internal Sensors"); 
    btnInternalSensors.setBounds(330, 560, 130, 23); 
    btnInternalSensors.addActionListener(this); 
    add(btnInternalSensors); 

    btnNavigation = new JButton("Navigation"); 
    btnNavigation.setBounds(550, 560, 110, 23); 
    btnNavigation.addActionListener(this); 
    add(btnNavigation); 


    } 

    @Override 
    public void paintComponent(Graphics g) { 
     //background 
     g.setColor(Color.decode("#666665")); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.fillRect(0, 0, 800, 600); 
     g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this); 

     //text setup 
     g.setColor(Color.white); 
     g.setFont(gameFont); 


     //Write the strings 
     //left bar 
     g.drawString(location, 10,60); 
.... 

    } 



    //Button Presses 
    public void actionPerformed(ActionEvent ae) { 
     JButton o = (JButton) ae.getSource(); 
.... 
     else if(o == btnMainComputer){ 
      Container parent = getParent(); 
      parent.remove(Home.getCurrentScreen()); 
      Home.setCurrentScreen(new MainComputerFrame()); 
      parent.add(Home.getCurrentScreen()); 
      //System.out.println("Main Comp"); 
     } 
     else if(o == btnInternalSensors){ 
      Container parent = getParent(); 
      parent.remove(Home.getCurrentScreen()); 
      Home.setCurrentScreen(new InternalSensorsFrame()); 
      parent.add(Home.getCurrentScreen()); 
      //System.out.println("Sensors"); 

     } 
     else if(o == btnNavigation){ 
      //Does nothing on a navigation Frame 
//   remove(Home.getCurrentScreen()); 
//   Home.setCurrentScreen(new NavigationFrame()); 
//   Home.frame.add(Home.getCurrentScreen()); 
     } 

     this.requestFocus(); 
    } 

} 

這裏是非工人階級:

public class InternalSensorsFrame extends GameScreen { 
private String imageName = "textures/interface/View Screen Frame Overlay.png"; 



public InternalSensorsFrame(){ 
    //super(parent); 
    addKeyListener(new TAdapter()); 

    //background 
    img = new ImageIcon(Home.resources.getClass().getResource(imageName)); 
    bg = new BufferedImage(
    img.getIconWidth(), 
    img.getIconHeight(), 
    BufferedImage.TYPE_4BYTE_ABGR); 
    Graphics gg = bg.createGraphics(); 
    img.paintIcon(null, gg, 0,0); 
    gg.dispose(); 
    RenderingHints rh = 
      new RenderingHints(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
    rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY); 

    //Add buttons 
    btnMainComputer = new JButton("Main Computer"); 
    btnMainComputer.setBounds(140, 560, 120, 23); 
    btnMainComputer.addActionListener(this); 
    add(btnMainComputer); 

    btnInternalSensors = new JButton("Internal Sensors"); 
    btnInternalSensors.setBounds(330, 560, 130, 23); 
    btnInternalSensors.addActionListener(this); 
    add(btnInternalSensors); 

    btnNavigation = new JButton("Navigation"); 
    btnNavigation.setBounds(550, 560, 110, 23); 
    btnNavigation.addActionListener(this); 
    add(btnNavigation); 

} 
@Override 
public void paintComponent(Graphics g) { 
    //super.paint(g); 
    g.setColor(Color.decode("#666665")); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.fillRect(0, 0, 800, 600); 
    g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this); 
    g2d.translate(150, 50); 
    Home.ship.shipLayout.paintComponent(g2d); 
//  ArrayList<CrewMan> crew = Home.ship.crew; 
//  for (int i=0; i<crew.size(); i++){ 
//   crew.get(i).paint(g2d); 
//  } 
     g.setColor(Color.white); 
     g.drawString("teststring", 10,60); 
     //super.paint(g); 

     //Toolkit.getDefaultToolkit().sync(); 
     // g.dispose(); 
} 

     //Button Presses 
     public void actionPerformed(ActionEvent ae) { 

      JButton o = (JButton) ae.getSource(); 

      if(o == btnMainComputer){ 
       Container parent = getParent(); 
       parent.remove(Home.getCurrentScreen()); 
       Home.setCurrentScreen(new MainComputerFrame()); 
       parent.add(Home.getCurrentScreen()); 
       //System.out.println("Main Comp"); 
      } 
      else if(o == btnInternalSensors){ 
       Container parent = getParent(); 
       parent.remove(Home.getCurrentScreen()); 
       Home.setCurrentScreen(new InternalSensorsFrame()); 
       parent.add(Home.getCurrentScreen()); 
       //System.out.println("Sensors"); 

      } 
      else if(o == btnNavigation){ 
       Container parent = getParent(); 
       parent.remove(Home.getCurrentScreen()); 
       Home.setCurrentScreen(new NavigationFrame()); 
       parent.add(Home.getCurrentScreen()); 
      } 

      this.requestFocus(); 
     } 
} 
+1

'super.paintComponent()'。 – trashgod

+1

調用'super.paintComponent(g)'也不要在'JPanel'中使用'requestFocus',並且不要調用Frame。因爲它是混淆的。作爲一個設計視圖,不要使用太多的繼承,你可以對組合進行相同的操作,只使用匿名類進行子類化。 – nachokk

+1

請勿使用setBounds()。 Swing旨在與佈局經理一起使用。不要調用另一個類的paintComponent()。 Swing會根據需要重新繪製組件。 – camickr

回答

4

也許這建議不解決您的問題,但可能的幫助。

1)

呼叫super.paintComponent(g)當你重寫此方法。

@Override 
    public void paintComponent(Graphics g) { 
     //background 
     super.paintComponent(g); 
     g.setColor(Color.decode("#666665")); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.fillRect(0, 0, 800, 600); 
     g2d.drawImage(bg, 0, ... 

2)requestFocus()

注意,使用這種方法是不鼓勵,因爲它的行爲 依賴於平臺。相反,我們推薦使用 requestFocusInWindow()。

3)爲您的課程使用專有名稱,您說您的班級實際上是一個JPanel的班級。

4)作爲個人建議不要使用太多的具體繼承,因爲它是危險的並且很難維護,並且您的繼承樹級別太高,您可以使用類似的東西。

實施例:

public class NavigationComponent { 

private GameScreen gameScreen; 

public NavigationComponent(){ 
     gameScreen = new GameScreen(){ 
      @override 
      public paintComponent(Graphics g){ 
       // code here 
      } 

     }; 
} 

} 

5)你正在添加的KeyListener到JPanel。爲了得到這個工作,你必須讓你的物體可以聚焦並且集中。而不是keyListener,擺動方式是使用KeyBinding。 Swing旨在與keyBindings一起使用。 How to use keybindings

0

我在發帖後簡單解決了我的問題: 添加super.paintComponents(g);沒有工作/沒有區別。最後是因爲我正在處理圖形。這裏是工作代碼:

@Override 
public void paintComponent(Graphics g) { 
    // super.paintComponents(g); 
    g.setColor(Color.decode("#666665")); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.fillRect(0, 0, 800, 600); 
    g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this); 



    Toolkit.getDefaultToolkit().sync(); 
// g.dispose(); //commenting this line out made it work 
    } 

不過,我喜歡這裏給出的答案/評論,並通過他們的工作,以提高代碼的其餘部分/將在意見作出迴應。謝謝!