让java显示超过1个.add()

发布于 2024-12-10 07:26:19 字数 2876 浏览 0 评论 0原文

我已经开始学习基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开柳相依 2024-12-17 07:26:19
  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);
        }
    }
}
  1. The default layout for a content pane is BorderLayout.
  2. Add a component to a border layout with no constraint and it ends up in the CENTER.
  3. Only one component can appear in each part of a border layout.

So try this instead..

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

Part of the problem is that custom components have a default preferred size of 0x0. Try this variant.

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);
        }
    }
}
—━☆沉默づ 2024-12-17 07:26:19

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);
  }
}

The current layout of JFrame is BorderLayout and you are adding both these components at center. Try to change the layout.

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

or

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

EDIT: You have to override the setPreferredSize() method for User.

class User extends JComponent {
  public java.awt.Dimension getPreferredSize() { 
    return new java.awt.Dimension(100,100);
  }
}
穿透光 2024-12-17 07:26:19

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文