我已经开始学习基本的Java,并想重写我曾用PHP编写的游戏地图生成器。我有一部分工作正常(这只是开始),但每当我想显示2件事(使用.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 :(得分:5)
BorderLayout
。 CENTER
。所以试试这个......
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);
}
}
}
答案 1 :(得分:2)
JFrame
的当前布局为BorderLayout
,您将这些组件添加到中心。尝试更改布局。
Container con = m1.getContentPane();
con.setLayout(new FlowLayout());
或
m1.add(new User(),BorderLayout.NORTH);
m1.add(new Grid(),BorderLayout.CENTER);
编辑:您必须覆盖User的setPreferredSize()方法。
class User extends JComponent {
public java.awt.Dimension getPreferredSize() {
return new java.awt.Dimension(100,100);
}
}
答案 2 :(得分:0)
AFAIR,内容窗格baqcking JFrame默认只接受单个子项。你不得不 设置并配置一些布局以拥有多个子项