Getting java to display more than 1 .add()
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);
c开发者_如何转开发on.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);
}
}
}
- The default layout for a content pane is
BorderLayout
. - Add a component to a border layout with no constraint and it ends up in the
CENTER
. - 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);
}
}
}
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);
}
}
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
精彩评论