让java显示超过1个.add()
我已经开始学习基本的 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);
}
}
}
I've started learning basic Java and wanted to rewrite a Game Map Generator that I've once wrote in PHP. I've got part of it working fine (This is just the start), but whenever I want to display 2 things (using .add()), it will only display one of them. Heres my code;
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);
}
It's only part of the code, but I was wondering if it's anything I've done wrong with .add there.
Thank you in advance.
This is the full code;
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BorderLayout
。CENTER
中。所以请尝试这个。
部分问题是自定义组件的默认首选大小为 0x0。尝试这个变体。
BorderLayout
.CENTER
.So try this instead..
Part of the problem is that custom components have a default preferred size of 0x0. Try this variant.
JFrame
的当前布局是BorderLayout
,您将在中心添加这两个组件。尝试改变布局。或
编辑:您必须重写 User 的 setPreferredSize() 方法。
The current layout of
JFrame
isBorderLayout
and you are adding both these components at center. Try to change the layout.or
EDIT: You have to override the setPreferredSize() method for User.
AFAIR,默认情况下,JFrame 的内容窗格仅接受单个子项。你必须
设置并配置一些布局以拥有多个子布局
AFAIR, content pane baqcking JFrame accepts only single child by default. You will have to
set and configure some layout to have more than one child