将参数从JPanel传递到JFrame

时间:2012-09-13 19:29:02

标签: java swing jpanel paintcomponent

我有一个问题,我没弄明白如何解决它。我正在阅读(JAVA如何编程第9版)。我的应用程序(第9章,第390页)需要创建具有不同颜色的随机数量的不同形状,然后布局(JFrame)包含JPanel(具有形状)和JLabel(具有这些不同形状的数量)。 我用随机颜色创建了随机形状,但是形状的数量无法正确传递给main方法。它总是显示0。

  

RandomShapes.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class RandomShapes extends JPanel{

public int counter;
private Random random = new Random();

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);

    drawLines(g);
}

public void drawLines(Graphics g){
    for(int i = 0 ; i < random.nextInt(20) ; i++){
        g.setColor(generateColor());
        g.drawLine(random.nextInt(getWidth()), random.nextInt(getHeight()), random.nextInt(getWidth()), random.nextInt(getHeight()));
        counter++;
    }

}

private Color generateColor(){
    Color color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));

    return color;
}

@Override
public String toString(){
    return String.format("%d",counter);
}
}
  

RandomShapesTest.java

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class RandomShapesTest{
public static void main(String[] args){
    RandomShapes shapes = new RandomShapes();
    JFrame application = new JFrame();

    JLabel status = new JLabel(shapes.toString());


    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(shapes);
    application.setSize(600,600);
    application.add(status, BorderLayout.NORTH);
    application.setVisible(true);
}   
}

顺便说一句,一个小技巧可能比完整答案更有帮助:)

此致,,,

2 个答案:

答案 0 :(得分:1)

每次重绘时counter值都会递增。这意味着无论何时调整窗口大小或执行其他触发重绘的操作,计数器都会增加并显示错误的值。

但是,JLabel当前显示0,因为在您使用toString()方法读取计数值后发生了第一次重绘。

我建议您构建要在RandomShapes构造函数调用的方法中绘制的对象。您构建了Line2D.Float个对象的列表,然后在绘制方法中绘制线条。

答案 1 :(得分:0)

谢谢你们所有有用的答案,我解决了这个问题如下:

  

CreateShapes.java

import java.awt.Color;
import java.awt.Graphics;

public class CreateShapes {

    private Graphics g;
    private Color myColor;
    private int x1, x2, x3, x4;

    public CreateShapes(int x1, int x2, int x3, int x4, Color myColor){
        this.x1 = x1;
        this.x2 = x2;
        this.x3 = x3;
        this.x4 = x4;
        this.myColor = myColor;
    }

    public void draw(Graphics g){
        g.setColor(myColor);
        g.drawLine(x1, x2, x3, x4);
    }
}
  

DrawShapes.java

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawShapes extends JPanel{

    private Random randomNumber = new Random();
    private CreateShapes[] shapes;
    private int shapeCounter;

    public DrawShapes(){

        setBackground(Color.WHITE);

        shapes = new CreateShapes[1+randomNumber.nextInt(15)];

        for(int i = 0 ; i < shapes.length ; i++){
            int x1 = randomNumber.nextInt(600);
            int x2 = randomNumber.nextInt(600);
            int x3 = randomNumber.nextInt(600);
            int x4 = randomNumber.nextInt(600);

            Color color = new Color(randomNumber.nextInt(255), randomNumber.nextInt(255), randomNumber.nextInt(255), randomNumber.nextInt(255));

            shapes[i] = new CreateShapes(x1, x2, x3, x4, color);
        }

        shapeCounter = shapes.length;
    }

    public int getShapesNumber(){
        return shapeCounter;
    }

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        for(CreateShapes shape : shapes){
            shape.draw(g);
        }
    }
}
  

RandomShapesTest.java

import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class RandomShapesTest {
    public static void main(String[] args){

        DrawShapes drawShapes = new DrawShapes();
        JFrame application = new JFrame();

        String status = String.format("Lines : %d", drawShapes.getShapesNumber());

        JLabel statusBar = new JLabel(status);

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(drawShapes);
        application.add(statusBar,BorderLayout.SOUTH);
        application.setSize(600,600);
        application.setVisible(true);
    }
}

此致,,,