JFreeChart躲在其他人后面

时间:2015-06-12 23:14:54

标签: java swing intellij-idea jfreechart

我试图在IntelliJ中构建UI但是很难,所以我尝试在Eclipse中构建它。现在UI看起来好一点但仍然很差。其中一个图表(cener one)应该是biger,而第二个图表应该是较小但是扁平和长。我尝试使用优先大小或验证或重绘但仍然没有。如何将它们固定在边框上,并避免自我掩盖?

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

1 个答案:

答案 0 :(得分:2)

由于您的代码不是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区域水平拉伸,以适应内部组件,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。