2013-07-24 23 views
0

我一直有一些無法理解的JPanel,JFrame的,和圖形類的這種整體結構,並延伸和壓倒一切和這樣。我似乎擁有了一切工作,直到我加入了圖形類,然後我的按鈕,並在等我的JPanel/JFrame中沒有露面了。我發現這與重寫或超級有關?但我真的需要一些澄清。非常感謝!Graphics類似乎是壓倒一切的JFrame/JPanel的

我已經縮小一下代碼,方便查看。

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.TitledBorder; 

public class windowBuild extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel contentPane; 
    private int energy = 4; 
    private JButton btnClaw = new JButton("Claw"); 
    private Image bg; 
    private boolean loaded = false; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       windowBuild frame = new windowBuild(); 
       frame.setVisible(true); 

      } 
     }); 
    } 

    private class ButtonHandler implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      String which = e.getActionCommand(); 
      if (which.equals("Claw")) { 
       energy = energy - 1; 
       System.out 
         .println("Player one's dragon clawed the opponent. Dragon's energy is now at: " 
           + energy); 
      } 

     } 

    } 

    public void loadImage() { 
     bg = new ImageIcon("C:\\res\\dragonDuelBackground.jpeg").getImage(); 
     loaded = true; 
     repaint(); 
    } 

    public windowBuild() { 
     ButtonHandler bh; 
     System.out.println("Starting frame..."); 
     bh = new ButtonHandler(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 800, 600); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new TitledBorder(null, "Dragon Duel", 
       TitledBorder.CENTER, TitledBorder.TOP, null, Color.CYAN)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     btnClaw.setBounds(273, 511, 109, 39); 
     contentPane.add(btnClaw); 
     btnClaw.addActionListener(bh); 

    } 
//******************************************************************** 
// public void paint(Graphics g) { 
//  if (loaded) { 
//   g.drawImage(bg, 400, 400, null); 
//  } 
// } 
//***************Uncomment this and the code won't work anymore********** 
} 

回答

2

取消註釋本,代碼將無法正常工作了

不要重寫paint()頂層容器(JFrame的,JDialog的......)的。定製繪畫是通過覆蓋一個JPanel(或JComponent中)的paintComponent()方法完成。然後將面板添加到框架。不要忘記調用super.paintComponent(...)。

閱讀Custom Painting教程Swing指南的詳細信息和示例。

+0

的教程非常好。謝謝。 – jdawg495

2
  1. 避免直接從JFrame延伸。如果你這樣做,你不會添加任何新功能到框架中,並且限制了UI的可重用性(因爲您不能將框架添加到其他任何東西)
  2. 請勿覆蓋頂層容器的paint 。有很多很好的理由,但主要問題是,框架容器有許多不同的層(JLayeredPane,contentPane),它們可能會覆蓋你所做的任何事情。
  3. 你必須調用super.paintXxx以確保繪畫鏈保持完整。

相反,建立一個獨立的類(從類似JPanel例如延長),並覆蓋它的paintComponent方法。或者更好。簡單使用JLabel來顯示圖像

看看Performing Custom PaintingPainting in AWT and Swing瞭解更多詳情。

另外,我沒有看到任何你在哪裏打電話loadImage,所以圖像不會被裝載

我也建議您採取通過Reading/Loading an Image使用ImageIO讀取,當它可以將拋出一個Exception 「T讀出的圖像文件,這是更爲有用然後ImageIcon它只是默默地失效

用簡單的例子更新

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.TitledBorder; 

public class TestGraphics { 

    public static void main(String[] args) { 
     new TestGraphics(); 
    } 

    public TestGraphics() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private int energy = 4; 
     private JButton btnClaw = new JButton("Claw"); 
     private BufferedImage bg; 

     public TestPane() { 
      setBackground(Color.WHITE); 
      try { 
       bg = ImageIO.read(new File("dragon.gif")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      ButtonHandler bh; 
      bh = new ButtonHandler(); 
      setBorder(new TitledBorder(null, "Dragon Duel", 
        TitledBorder.CENTER, TitledBorder.TOP, null, Color.CYAN)); 
      setLayout(new BorderLayout()); 

      JPanel buttonPane = new JPanel(); 
      buttonPane.setOpaque(false); 
      buttonPane.add(btnClaw); 

      add(buttonPane, BorderLayout.SOUTH); 
      btnClaw.addActionListener(bh); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return bg == null ? new Dimension(200, 200) : new Dimension(bg.getWidth(), bg.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (bg != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       int x = (getWidth() - bg.getWidth())/2; 
       int y = (getHeight() - bg.getHeight())/2; 
       g2d.drawImage(bg, x, y, this); 
       g2d.dispose(); 
      } 
     } 

     private class ButtonHandler implements ActionListener { 

      public void actionPerformed(ActionEvent e) { 
       String which = e.getActionCommand(); 
       if (which.equals("Claw")) { 
        energy = energy - 1; 
        System.out 
          .println("Player one's dragon clawed the opponent. Dragon's energy is now at: " 
          + energy); 
       } 

      } 
     } 
    } 
} 

PS-你最好有非常充分的理由不使用佈局管理器

+0

謝謝!它幫助了很多。 – jdawg495

+0

你可以改變代碼並顯示給我。我明白你在說什麼,但是現在我正在努力應該把什麼放在哪裏,放在主要班級還是放在哪裏? – jdawg495