gpt4 book ai didi

Java JFrame 绘制多行

转载 作者:行者123 更新时间:2023-11-29 05:18:51 27 4
gpt4 key购买 nike

所以我的问题的症结在于用 Java 将多个组件绘制到一个 JFrame 中。我试图两次使用相同的组件来绘制两条不同的线,但只出现了一条。我在不同的文件中处理三个不同的类,这对我来说可能会更加困难。我已经尝试了可能的解决方案但无济于事here , here , here , here , 和其他地方。我怀疑我做错了很多事,因为我仍在努力完全理解 JFrameJPanelLayoutManager。谁能告诉我哪里出错了?

我的测试类如下:

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 的类:

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]));
}
}
}

最佳答案

问题 #1

Component 的实例只能驻留在单个 Container 中(一次)。

您需要为要添加的每个 Component 创建一个新实例。我会推荐工厂模式...

问题#2

JFrame,但默认使用 BorderLayout,它只允许一个组件驻留在它的 5 个可用布局位置中的每一个位置。

您还会遇到问题,因为您的 TransportSlabGraph 类没有覆盖它的 getPreferredSize 方法,这意味着,默认情况下,组件的实例将提供一个许多布局管理器的默认大小为 0x0

考虑将布局管理器更改为类似 GridLayout 的东西。

看看Laying Out Components Within a Container了解更多详情

关于Java JFrame 绘制多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25539229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com