gpt4 book ai didi

java - JFreeChart 隐藏在另一个图表后面

转载 作者:行者123 更新时间:2023-11-30 03:23:18 24 4
gpt4 key购买 nike

我试图在 IntelliJ 中构建 UI,但这很难,所以我尝试在 Eclipse 中构建它。现在 UI 看起来好一点了,但仍然很差。其中一张图表(中心的一张)应该较大,南边的第二张图表应该较小,但又平又长。我尝试使用首选尺寸或验证或重新绘制,但仍然一无所获。如何将它们锚定到侧边框,并避免自身覆盖它们?

public class MainWindow extends JFrame{

private JPanel contentPane;

private XYSeries daneXYSciezki;
private XYSeries daneXYWys;

private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;

public MainWindow(){
setTitle("ParserGPS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 880, 640);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));

JPanel panelNorth = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panelNorth, BorderLayout.NORTH);

JButton btnWczytajPlik = new JButton("Wczytaj plik");
panelNorth.add(btnWczytajPlik);

JLabel lblOdlego = new JLabel("Odległość:");
panelNorth.add(lblOdlego);

JLabel lblm = new JLabel("0m");
panelNorth.add(lblm);

JLabel lblCzas = new JLabel("Czas:");
panelNorth.add(lblCzas);

JLabel label = new JLabel("00:00:00");
panelNorth.add(label);

JLabel lblPrdko = new JLabel("Prędkość:");
panelNorth.add(lblPrdko);

JLabel lblkmh = new JLabel("0km/h");
panelNorth.add(lblkmh);

JLabel lblNrSat = new JLabel("Nr sat:");
panelNorth.add(lblNrSat);

JLabel label_1 = new JLabel("0");
panelNorth.add(label_1);

JLabel lblGga = new JLabel("GGA:");
lblGga.setHorizontalAlignment(SwingConstants.CENTER);
panelNorth.add(lblGga);

JLabel label_2 = new JLabel("0/0");
panelNorth.add(label_2);

JLabel lblGsa = new JLabel("GSA:");
panelNorth.add(lblGsa);

JLabel label_3 = new JLabel("0/0");
panelNorth.add(label_3);

JLabel lblNewLabel = new JLabel("RMC:");
panelNorth.add(lblNewLabel);

JLabel label_4 = new JLabel("0/0");
panelNorth.add(label_4);

JLabel lblGll = new JLabel("GLL:");
panelNorth.add(lblGll);

JLabel label_5 = new JLabel("0/0");
panelNorth.add(label_5);

JLabel lblVtg = new JLabel("VTG:");
panelNorth.add(lblVtg);

JLabel label_6 = new JLabel("0/0");
panelNorth.add(label_6);

JPanel jpCenter = new JPanel();
contentPane.add(jpCenter, BorderLayout.CENTER);
jpCenter.setPreferredSize(new Dimension(785, 440));

JPanel jpSouth = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
flowLayout_1.setAlignment(FlowLayout.TRAILING);
contentPane.add(jpSouth, BorderLayout.SOUTH);

this.daneXYSciezki = new XYSeries("Trasa", false);
wykCenter = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykCenter = createChart(wykCenter);
final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
jpCenter.add(jfcPanelCenter,BorderLayout.CENTER);

this.daneXYSciezki = new XYSeries("Wysokość", false);
wykSouth = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykSouth = createChart(wykSouth);
final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);
repaint();
revalidate();
}

private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createXYLineChart(
"",
"Szerokość",
"Długość",
dataset);
final XYPlot plot = result.getXYPlot();
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
yAxis.setAutoRangeIncludesZero(false);
yAxis.setAutoRange(true);
customizeChart(result);
return result;
}

private void customizeChart(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer;
renderer = new XYLineAndShapeRenderer(true, true);
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesStroke(0, new BasicStroke(1.0f));
plot.setRenderer(renderer);
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
}
}

Window I created

经过 @m.cekiera 的建议

public class MainWindow extends JFrame{
private JPanel contentPane;

private XYSeries daneXYSciezki;
private XYSeries daneXYWys;

private final XYSeriesCollection wykCenter;
private final XYSeriesCollection wykSouth;

public MainWindow(){
setTitle("ParserGPS");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 880, 640);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));

JPanel panelNorth = new JPanel();
FlowLayout flowLayout = (FlowLayout) panelNorth.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panelNorth,BorderLayout.NORTH);

JButton btnWczytajPlik = new JButton("Wczytaj plik");
panelNorth.add(btnWczytajPlik);

JLabel lblOdlego = new JLabel("Odległość:");
panelNorth.add(lblOdlego);

JLabel lblm = new JLabel("0m");
panelNorth.add(lblm);

JLabel lblCzas = new JLabel("Czas:");
panelNorth.add(lblCzas);

JLabel label = new JLabel("00:00:00");
panelNorth.add(label);

JLabel lblPrdko = new JLabel("Prędkość:");
panelNorth.add(lblPrdko);

JLabel lblkmh = new JLabel("0km/h");
panelNorth.add(lblkmh);

JLabel lblNrSat = new JLabel("Nr sat:");
panelNorth.add(lblNrSat);

JLabel label_1 = new JLabel("0");
panelNorth.add(label_1);

JLabel lblGga = new JLabel("GGA:");
//lblGga.setHorizontalAlignment(SwingConstants.CENTER);
panelNorth.add(lblGga);

JLabel label_2 = new JLabel("0/0");
panelNorth.add(label_2);

JLabel lblGsa = new JLabel("GSA:");
panelNorth.add(lblGsa);

JLabel label_3 = new JLabel("0/0");
panelNorth.add(label_3);

JLabel lblNewLabel = new JLabel("RMC:");
panelNorth.add(lblNewLabel);

JLabel label_4 = new JLabel("0/0");
panelNorth.add(label_4);

JLabel lblGll = new JLabel("GLL:");
panelNorth.add(lblGll);

JLabel label_5 = new JLabel("0/0");
panelNorth.add(label_5);

JLabel lblVtg = new JLabel("VTG:");
panelNorth.add(lblVtg);

JLabel label_6 = new JLabel("0/0");
panelNorth.add(label_6);

JPanel jpCenter = new JPanel(new BorderLayout());
contentPane.add(jpCenter, BorderLayout.CENTER);
//jpCenter.setPreferredSize(new Dimension(785, 440));

JPanel jpSouth = new JPanel(new BorderLayout());
//FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
// flowLayout_1.setAlignment(FlowLayout.TRAILING);
contentPane.add(jpSouth, BorderLayout.SOUTH);

this.daneXYSciezki = new XYSeries("Trasa", false);
wykCenter = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykCenter = createChart(wykCenter);
final ChartPanel jfcPanelCenter = new ChartPanel(jfcWykCenter);
jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);

this.daneXYSciezki = new XYSeries("Wysokość", false);
wykSouth = new XYSeriesCollection(this.daneXYSciezki);
final JFreeChart jfcWykSouth = createChart(wykSouth);
final ChartPanel jfcPanelSouth = new ChartPanel(jfcWykSouth);
jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH);
}

...
}

after 3rd correct

最佳答案

因为您的代码不是 MCVE我无法通过更改来运行它,但我认为您的问题是布局设置错误。例如,您创建一个新的 JPanel:

JPanel jpSouth = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) jpSouth.getLayout();
flowLayout_1.setAlignment(FlowLayout.TRAILING);

您知道它默认使用 FlowLayout,稍后您将使用:

jpSouth.add(jfcPanelSouth,BorderLayout.CENTER);

但你永远不会将布局更改为BorderLayout。您不能混合一个特定容器的布局。但这不是直接导致问题的原因。

您的 BorderLayout.SOUTH 区域水平拉伸(stretch),以适应内部组件,并且 BorderLayout.CENTER 保留(或不保留)所有规范。这就是 BorderLayout 的这些部分的工作原理。因此,您可以更改放置特定组件的位置,或者根本更改布局。

我建议尝试:

JPanel jpCenter = new JPanel(new BorderLayout());
...
jpCenter.add(jfcPanelCenter,BorderLayout.NORTH);
...
jpCenter.add(jfcPanelSouth,BorderLayout.SOUTH); //not to contentPane!

这将改变安排。但我不确定,这是你想要的吗?如果没有,请尝试使用不同的布局,例如 BoxLayout。另外,我宁愿使用 pack() 并设置组件的大小,而不是在计数器上使用 setBounds

如果您想创建 GUI 但不关心如何创建,您还可以使用 GUI 构建器,例如 Netbeans IDE GUI Builder。

关于java - JFreeChart 隐藏在另一个图表后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813480/

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