我已經開始學習基本的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);
}
}
}
*「這只是部分的代碼。」 *爲了更好地幫助越早,張貼[SSCCE(http://pscode.org/sscce.html)。如果你有,我會在發佈之前**測試**我的答案。 –
我剛剛發佈了我的完整代碼。這不是一個非常大的文件。 – Oyed
這是一個經典的SSCCE。看到我的編輯進一步提示。 –