为什么我的两个JComponents实例中只有一个出现在大型机中

时间:2018-01-25 12:54:11

标签: java jframe jcomponent

我创建了一个绘制饼图的类,这个类扩展了JComponent。它完全像我想要的那样工作,除非我想要制作这个类的两个实例而不是一个。最后创建的饼图只显示出来。另一个不会。我将两个图表的边界打印到控制台,我看到未显示的那个有width = 0和height = 0,但我确实在声明中给出了一定的宽度和高度。

PieChart课程

public class PieChart extends JComponent {
    private static final long serialVersionUID = -5712412361082680298L;

    private Rectangle bounds;
    public ArrayList<PieSlice> slices = new ArrayList<PieSlice>();

    PieChart(int x, int y, int width, int height) {
        bounds = new Rectangle(x, y, width, height);
        this.setBounds(bounds);
    }

    public void paint(Graphics g) {
        drawPie((Graphics2D) g, bounds, slices);
    }

    void drawPie(Graphics2D g, Rectangle area, ArrayList<PieSlice> slices) {
        double total = 0.0D;

        for (int i = 0; i < slices.size(); i++) {
            total += slices.get(i).value;
        }

        double curValue = 0.0D;
        int startAngle = 0;
        for (int i = 0; i < slices.size(); i++) {
            startAngle = (int) (curValue * 360 / total);
            int arcAngle = (int) (slices.get(i).value * 360 / total);
            g.setColor(slices.get(i).color);
            g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
            curValue += slices.get(i).value;
        }
    }

    public void addPieSlice(double value, Color color) {
        PieSlice slice = new PieSlice(value, color);
        slices.add(slice);
    }
}

MainView类

public class MainView extends JFrame {

    private static final long serialVersionUID = 1L;
    private CarParkView carParkView;
    public PieChart occupiedPieChart;
    public PieChart subscribersPieChart;
    public JPanel buttonPane;
    public JButton resume;
    public JButton pause;
    public JButton plusHundredTicks;

    public MainView(Model model, int numberOfFloors, int numberOfRows, int numberOfPlaces) {
        this.setPreferredSize(new Dimension(1500,1000));

        carParkView = new CarParkView(model, numberOfFloors, numberOfRows, numberOfPlaces);
        occupiedPieChart = new PieChart(900, 100, 400, 400);
        subscribersPieChart = new PieChart(900, 500, 400, 400);
        buttonPane = new JPanel();

        resume = new JButton("Resume");
        pause = new JButton("Pause");
        plusHundredTicks = new JButton("+100 ticks");

        buttonPane.add(resume);
        buttonPane.add(pause);
        buttonPane.add(plusHundredTicks);

        buttonPane.setBounds(0, 20, 1000, 100);
        carParkView.setBounds(0, 100, 800, 500);

        occupiedPieChart.addPieSlice(10, Color.WHITE);
        occupiedPieChart.addPieSlice(90, Color.GREEN);

        subscribersPieChart.addPieSlice(50, Color.WHITE);
        subscribersPieChart.addPieSlice(50, Color.BLUE);

        Container contentPane = getContentPane();
        contentPane.add(buttonPane);
        contentPane.add(subscribersPieChart);
        contentPane.add(carParkView);
        contentPane.add(occupiedPieChart);

        pack();

        setVisible(true);

        updateView();
    }

    public void updateView() {
        carParkView.updateView();
        subscribersPieChart.repaint();
        occupiedPieChart.repaint();
    }

}

注意:这是我的第一个Java-eclipse项目,尽管解决方案可能很容易,但我仍然盯着这几个小时。

0 个答案:

没有答案