2011-10-17 60 views
3

我已經開始學習基本的Java,並且想重寫我曾經用PHP寫過的遊戲地圖生成器。我有一部分工作正常(這只是一個開始),但每當我想顯示2件事情(使用.add())時,它只會顯示其中的一個。這是我的代碼讓java顯示超過1個.add()

public static void main(String[] args) { 
    JFrame m1 = new JFrame(); 
    Container con = m1.getContentPane(); 
    Color c = new Color(16, 174, 0); 
    con.setBackground(c); 
    m1.setSize(mapWidth, mapHeight); 
    m1.setTitle("ThomasMosey's Map Generator"); // Window Title 
    m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    m1.add(new User()); 
    m1.add(new Grid()); 
    m1.setVisible(true); 
} 

它只是代碼的一部分,但我想知道是否我在.add那裏做了任何錯誤。

預先感謝您。


這是完整的代碼;

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 

public class Map extends JFrame { 
    public static int mapWidth = 576; // The Map's Width 
    public static int mapHeight = 598; // The Map's Height 
    public static int userX = 1; 
    public static int userY = 1; 
    private static final long serialVersionUID = 1L; 
    public static void main(String[] args) { 
     JFrame m1 = new JFrame(); 
     Container con = m1.getContentPane(); 
     Color c = new Color(16, 174, 0); 
     con.setBackground(c); 
     m1.setSize(mapWidth, mapHeight); 
     m1.setTitle("ThomasMosey's Map Generator"); // Window Title 
     m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     con.add(new User(), BorderLayout.NORTH); 
     con.add(new Grid(), BorderLayout.CENTER); 
     m1.setVisible(true); 
    } 
} 
// The Grid system 
class Grid extends JComponent { 
    private static final long serialVersionUID = 1L; 
    public void paint(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     Color gridColor = new Color(0, 84, 12); 
     g2.setColor(gridColor); 
     int i; 
     int i2; 
     for(i = 50; i <= Map.mapWidth; i += 51) { 
      g2.drawLine(0, i, Map.mapWidth, i); 
     } 
     for(i2 = 50; i2 <= Map.mapHeight; i2 += 51) { 
      g2.drawLine(i2, 0, i2, Map.mapHeight); 
     } 
    } 
} 
// Drawing the Grid but lower down to give a significant difference to check whether or not it's actually drawing on the JFrame 
class User extends JComponent { 
    private static final long serialVersionUID = 1L; 
    public void paint(Graphics g2) { 
     Graphics2D g22 = (Graphics2D) g2; 
     g22.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     Color gridColor = new Color(0, 84, 12); 
     g22.setColor(gridColor); 
     int i22; 
     for(i22 = 50; i22 <= 6000; i22 += 53) { 
      g22.drawLine(0, i22, Map.mapWidth, i22+1); 
     } 
    } 
} 
+0

*「這只是部分的代碼。」 *爲了更好地幫助越早,張貼[SSCCE(http://pscode.org/sscce.html)。如果你有,我會在發佈之前**測試**我的答案。 –

+0

我剛剛發佈了我的完整代碼。這不是一個非常大的文件。 – Oyed

+0

這是一個經典的SSCCE。看到我的編輯進一步提示。 –

回答

5
  1. 的內容窗格的默認佈局是BorderLayout
  2. 將組件添加到不受約束的邊框佈局,並以CENTER結尾。
  3. 只有一個組件可以出現在邊框佈局的每個部分中。

那麼試試這個,而不是..這個問題的

m1.add(new User(), BorderLayout.NORTH); 
m1.add(new Grid(), BorderLayout.CENTER); 

部分原因是,自定義組件具有0x0到默認首選尺寸。試試這個變種。

import java.awt.*; 
import javax.swing.*; 

public class Map extends JFrame { 
    public static int mapWidth = 576; // The Map's Width 
    public static int mapHeight = 598; // The Map's Height 
    public static int userX = 1; 
    public static int userY = 1; 
    private static final long serialVersionUID = 1L; 
    public static void main(String[] args) { 
     JFrame m1 = new JFrame(); 
     Container con = m1.getContentPane(); 
     Color c = new Color(16, 174, 0); 
     con.setBackground(c); 
     // bad form - pack() instead 
     //m1.setSize(mapWidth, mapHeight); 
     m1.setTitle("ThomasMosey's Map Generator"); // Window Title 
     m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     User user = new User(); 
     user.setPreferredSize(new Dimension(300,300)); 
     con.add(user, BorderLayout.NORTH); 
     Grid grid = new Grid(); 
     grid.setPreferredSize(new Dimension(600,600)); 
     con.add(grid, BorderLayout.CENTER); 
     m1.pack(); 
     m1.setVisible(true); 
    } 
} 
// The Grid system 
class Grid extends JComponent { 
    private static final long serialVersionUID = 1L; 
    public void paintComponent(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     Color gridColor = new Color(0, 84, 12); 
     g2.setColor(gridColor); 
     int i; 
     int i2; 
     for(i = 50; i <= Map.mapWidth; i += 51) { 
      g2.drawLine(0, i, Map.mapWidth, i); 
     } 
     for(i2 = 50; i2 <= Map.mapHeight; i2 += 51) { 
      g2.drawLine(i2, 0, i2, Map.mapHeight); 
     } 
    } 
} 
// Drawing the Grid but lower down to give a significant difference to check whether or not it's actually drawing on the JFrame 
class User extends JComponent { 
    private static final long serialVersionUID = 1L; 
    public void paintComponent(Graphics g2) { 
     Graphics2D g22 = (Graphics2D) g2; 
     g22.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     Color gridColor = new Color(0, 84, 12); 
     g22.setColor(gridColor); 
     int i22; 
     for(i22 = 50; i22 <= 6000; i22 += 53) { 
      g22.drawLine(0, i22, Map.mapWidth, i22+1); 
     } 
    } 
} 
+0

添加BorderLayout.CENTER和BorderLayout.PAGE_START不會更改它。編輯:您的第二個修復程序無法正常工作。 – Oyed

+0

它的工作,但我想他們在彼此之上。有沒有辦法做到這一點? – Oyed

+0

什麼是'Grid'和'User'?在這裏,用戶**不會出現在'網格'的頂部。你的意思是他們應該分層嗎?該用戶是透明的,你可以看到**後面的網格**嗎? –

0

AFAIR,內容窗格baqcking默認情況下,JFrame只接受單個子項。你將不得不 組,並配置一些佈局有一個以上的孩子

+0

-1「JFrame」的內容窗格可以接受** 5 **組件而不更改佈局。 –

2

JFrame當前佈局是BorderLayout和你是在中心添加這兩種成分。嘗試改變佈局。

Container con = m1.getContentPane(); 
con.setLayout(new FlowLayout()); 

m1.add(new User(),BorderLayout.NORTH); 
m1.add(new Grid(),BorderLayout.CENTER); 

編輯:你來覆蓋用戶有必要對setPreferredSize()方法。

class User extends JComponent { 
    public java.awt.Dimension getPreferredSize() { 
    return new java.awt.Dimension(100,100); 
    } 
} 
+0

感謝您重申您的答案。我甚至在有關首選規模的建議之前準備投票。 (雖然我不同意最好重寫該方法,但這更多的是設計問題。) –