[英]Can't add components to a class that extends JFrame
I'm trying to write the game of life. 我正在尝试写生活游戏。 However, no matter how hard I try, I can't seem to add Cell components onto the frame with the buttons.
但是,无论我多么努力,我似乎都无法通过按钮将Cell组件添加到框架上。 They do appear if I make a new frame and add them, but even so, I can't seem to add more than one component.
如果我制作一个新框架并添加它们,它们确实会出现,但是即使如此,我似乎也不能添加多个组件。
So I how I add the components to the frame with all the stuff on it and how do I add multiple components? 因此,我如何将具有所有内容的组件添加到框架中,以及如何添加多个组件? Thanks!
谢谢!
Here's my code: 这是我的代码:
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Visuals extends JFrame implements ActionListener{
private static Board b;
private GameOfLife g;
private JButton nextGen;
private JButton random;
public Visuals(Board b){
super("The Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
nextGen = new JButton("Next Generation");
panel.add(nextGen);
nextGen.addActionListener(this);
random = new JButton("Random");
panel.add(random);
random.addActionListener(this);
this.add(panel);
this.setSize(800, 700);
this.setVisible(true);
/*for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){
Cell c= new Cell(xShift,yShift,i,j);
this.add(c);
}
}*/
Cell c= new Cell(100,100,1,1);
Cell d= new Cell(200,200,1,1);
this.add(c);
this.add(d);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 700);
frame.setVisible(true);
frame.add(c);
frame.add(d);
}
public void paint(Graphics g){
super.paint(g);
for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){
if(b.getSpot(i, j)==1){
g.setColor(Color.red);
g.fillRect(xShift, yShift, 10, 10);
}
else{
g.setColor(Color.white);
g.fillRect(xShift, yShift, 10, 10);
}
}
}
}
public static void main(String[] cnbe){
b= new Board (30,30);
Visuals v = new Visuals(b);
}
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
if(cause == nextGen){
GameOfLife g=new GameOfLife(b);
g.nextGeneration();
System.out.println(g);
this.repaint();
}
if(cause == random){
GameOfLife g=new GameOfLife(b);
g.random();
System.out.println(g);
this.repaint();
}
}
public class Cell extends JComponent{
int row;
int col;
int x;
int y;
Rectangle r;
public Cell(int x, int y, int row, int col){
r= new Rectangle(x,y,10,10);
this.row=row;
this.col=col;
this.x=x;
this.y=y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("DRAW ME",200,200);
Graphics2D g2= (Graphics2D) g;
g2.setColor(Color.green);
r= new Rectangle(x,y,100,100);
g2.fill(r);
/*if(b.getSpot(row, col)==1){
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
}
else{
g.setColor(Color.white);
g.fillRect(x, y, 10, 10);
}*/
}
class MousePressListener implements MouseListener{
public void mousePressed(MouseEvent event) {
/*int x= event.getX();
int y= event.getY();
if(x>r.getX() && x<r.getX()+10 && y>r.getY() && y<r.getY()+10){
if(b.getSpot(row, col)==1){
b.setSpot(row, col, 0);
repaint();
}
else{
b.setSpot(row, col, 1);
repaint();
}
}*/
}
public void mouseReleased(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
}
}
}
You have an issue with Layout Managers. 布局管理器存在问题。 Each time you add a component to the JFrame you overwrite what was already there.
每次将组件添加到JFrame时,都将覆盖已经存在的组件。 I'm not too familiar with Game of Life, but I don't think you want your cells to be JComponents.
我不太了解“生命游戏”,但我不认为您希望您的单元成为JComponents。 You can represent your board as an array of booleans, representing the alive and dead state, so your Visual constructor becomes:
您可以将开发板表示为布尔数组,表示存活状态和死亡状态,因此您的Visual构造函数将变为:
public Visuals(Board b) {
super("The Game of Life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane(); // 1
JPanel panel = new JPanel(new FlowLayout()); // 2
nextGen = new JButton("Next Generation");
panel.add(nextGen);
nextGen.addActionListener(this);
random = new JButton("Random");
panel.add(random);
random.addActionListener(this);
container.add(panel); // 3
setSize(800, 700);
setVisible(true);
}
In your Board class you would represent the board as an array: 在您的Board类中,您将把Board表示为一个数组:
public class Board{
private boolean[][] board;
public Board(int x, int y) {
board = new boolean[x][y];
}
}
Then in the nextGeneration
method of your GameOfLife class, perform whatever calculations you're doing and set the true/false (or alive/dead) state of the positions on the Board. 然后,在
nextGeneration
类的nextGeneration
方法中,执行您要执行的任何计算,并设置板上位置的正确/错误(或有效/无效)状态。
Your Visuals.paint method then becomes something like: 然后,您的Visuals.paint方法变为:
public void paint(Graphics g) {
super.paint(g);
for (int i = 0, yShift = 80; i < b.getRow(); i++, yShift += 20) {
for (int j = 0, xShift = 30; j < b.getCol(); j++, xShift += 20) {
if (b.isAlive(i, j)) {
g.setColor(Color.red);
g.fillRect(xShift, yShift, 10, 10);
} else {
g.setColor(Color.white);
g.fillRect(xShift, yShift, 10, 10);
}
}
}
}
iaAlive
is just returning the value of the board array at that position: iaAlive
只是返回该位置的板阵列的值:
public boolean isAlive(int i, int j) {
return board[i][j];
}
But like I say, I'm not familiar with the game. 但是就像我说的那样,我对游戏并不熟悉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.