Java JFrame绘制多条线

Java JFrame绘制多条线,java,swing,plot,jframe,paintcomponent,Java,Swing,Plot,Jframe,Paintcomponent,因此,我的问题的关键是在Java中将多个组件绘制到一个JFrame中。我尝试使用同一个组件两次绘制两条不同的线,但只显示一条。我在不同的文件中跨三个不同的类工作,这可能会让我更加困难。我尝试过各种可能的解决方案,但都无济于事。我怀疑我做错了很多事情,因为我仍在努力完全理解JFrame、JPanel和LayoutManager。谁能告诉我哪里出错了 我的测试仪等级如下: import javax.swing.JFrame; public class TransportSlabTester {

因此,我的问题的关键是在Java中将多个组件绘制到一个JFrame中。我尝试使用同一个组件两次绘制两条不同的线,但只显示一条。我在不同的文件中跨三个不同的类工作,这可能会让我更加困难。我尝试过各种可能的解决方案,但都无济于事。我怀疑我做错了很多事情,因为我仍在努力完全理解JFrame、JPanel和LayoutManager。谁能告诉我哪里出错了

我的测试仪等级如下:

import javax.swing.JFrame;

public class TransportSlabTester
{
    public static void main(String[] args)
    {
        System.out.println("Estimation at 100 sections: ");
        TransportSlab slab1 = new TransportSlab(10000,1,5,100);
        System.out.println();

        JFrame frame = new JFrame("Attenuated Profile");
        frame.setSize(600,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TransportSlabGraph component = new TransportSlabGraph();
        //analytical is a method from a 3rd class that returns double[]
        component.attProfileArray(slab1.analytical(),slab1.getThickness());
        frame.add(component);
        component = new TransportSlabGraph();
        //euler is a method from a 3rd class that returns double[]
        component.attProfileArray(slab1.euler(),slab1.getThickness());
        frame.add(component);
        frame.setVisible(true);
    }
}
现在,扩展JPanel的类:

问题1 组件的实例只能在单个容器中驻留一次

您需要为要添加的每个组件创建一个新实例。我推荐一种工厂模式

问题2 JFrame(默认情况下)使用BorderLayout,它只允许单个组件驻留在其5个可用布局位置中的每个位置

由于TransportSlabGraph类没有覆盖其getPreferredSize方法,因此也会出现问题,这意味着默认情况下,许多布局管理器将为组件实例提供默认大小0x0

首先考虑将布局管理器更改为类似GridLayout的内容

查看更多详细信息

问题1 组件的实例只能在单个容器中驻留一次

您需要为要添加的每个组件创建一个新实例。我推荐一种工厂模式

问题2 JFrame(默认情况下)使用BorderLayout,它只允许单个组件驻留在其5个可用布局位置中的每个位置

由于TransportSlabGraph类没有覆盖其getPreferredSize方法,因此也会出现问题,这意味着默认情况下,许多布局管理器将为组件实例提供默认大小0x0

首先考虑将布局管理器更改为类似GridLayout的内容


查看更多详细信息

组件只能添加一次到集装箱SYMBOL not found:TransportSlab为了更快地获得更好的帮助,请发布一个最小的、完整的,可验证示例。TransportSlab是我为节省空间而忽略的第三个类。组件只能添加一次到containerSymbol not found:TransportSlab为了更快地获得更好的帮助,发布一个最小、完整、可验证的示例。TransportSlab是我为节省空间而忽略的第三个类。如下所示?TransportSlabGraph组件2=新的TransportSlabGraph;组件2.attProfileArrayslab1.欧拉,slab1.葛根厚度;frame.addcomponent2;像下面这样?TransportSlabGraph组件2=新的TransportSlabGraph;组件2.attProfileArrayslab1.欧拉,slab1.葛根厚度;frame.addcomponent2;
import java.awt.*;
import java.awt.geom.Line2D;
import java.math.*;
import javax.swing.JPanel;

public class TransportSlabGraph extends JPanel
{
    double[] N, xAxes, yAxes;
    final int edge = 100; //Distance from edge of frame
    String[] xlabel = new String[11];
    String[] ylabel = new String[11];

    /**
     *
     * @param inputN Data array of type {@code double[]}
     * @param thickness Thickness set by the original constructor
     */
    public void attProfileArray(double[] inputN, double thickness)
    {
        N = new double[inputN.length];
        //Create labels for the tick marks of the x and y axis from rounded #'s
        BigDecimal bd1, bd2;
        for (int i = 0; i <= 10; i++)
        {
            bd1 = new BigDecimal((thickness/10)*i);
            MathContext mc = new MathContext(2); //Round to one decimal place
            bd2 = bd1.round(mc);
            xlabel[i] = String.valueOf(bd2.doubleValue());
            ylabel[i] = String.valueOf((inputN[0]*i)/(inputN.length-1));
        }
        //Set up data array and the axes
        for (int i = 0; i < N.length; i++)
        {
            N[i]=inputN[i];
            xAxes = new double[N.length];
            yAxes = new double[N.length];
        }
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        //Get frame dimensions to scale drawn components
        int w = getWidth();
        int h = getHeight();
        double xInc = (double)(w-2*edge)/(N.length-1);
        double scale = (double)(h-2*edge)/N[0];
        g2.draw(new Line2D.Double(edge, h-edge, w-edge, h-edge)); //draw x axis
        g2.draw(new Line2D.Double(edge, edge, edge, h-edge)); // draw y axis
        //Create evenly spaced tick marks for both axes and label them
        for (int i = 0; i <= 10; i++)
        {
            g2.draw(new Line2D.Double(edge+((w-edge-edge)/10.0)*i, h-edge-10, edge+((w-edge-edge)/10.0)*i, h-edge+10)); //x ticks
            g2.draw(new Line2D.Double(edge-10, h-edge-((h-edge-edge)/10.0)*i, edge+10, h-edge-((h-edge-edge)/10.0)*i)); //y ticks
            g2.drawString(xlabel[i],(int)(edge+((w-edge-edge)/10.0)*i),h-edge+20);
            g2.drawString(ylabel[i],edge-30,(int)(h-edge-((h-edge-edge)/10.0)*i));
        }
        //Scale data and convert to pixel coordinates
        for (int i = 0; i < N.length; i++)
        {
            xAxes[i] = edge+i*xInc;
            yAxes[i] = h-edge-scale*N[i];
        }
        //Only set the data line's color
        g2.setPaint(Color.BLUE);
        //Draw the data as a series of line segments
        for (int i = 1; i < N.length; i++)
        {
            g2.draw(new Line2D.Double(xAxes[i-1],yAxes[i-1],xAxes[i],yAxes[i]));
        }
    }
}