2015-06-12 63 views
1

我試圖在IntelliJ中構建UI,但它很難,所以我試圖在Eclipse中構建它。現在UI看起來好一點,但仍然很差。圖表中的一個(cener)應該是biger,而南面的第二個應該更小但平坦且長。我試圖使用優先大小或驗證或重繪,但仍然沒有。如何將他們錨定到邊界,並避免掩蓋他們自己?JFreeChart隱藏在其他一個

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

回答

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()和設置組件的大小,而不是一個countainer上的setBounds

如果您想創建GUI但不關心如何,您還可以使用GUI構建器,如Netbeans IDE GUI Builder。

+0

你的建議是非常有幫助的。添加您的建議後,我接近解決我的問題。只有一件事我不明白。爲什麼中心板現在寬闊平坦,南面板更大。我想要南面板是寬闊的,中心面板要大一些,主圖表 – GarryMoveOut

+0

@GarryMoveOut我認爲它和以前一樣是一樣的問題,你仍然在'contentPane'中添加'jpCenter',所以它會改變大小以適應向左走。然後你在contentPane SOUTH部分添加'jpSouth' - 再次垂直擴展。嘗試將** jfcPanelCenter **放入** NORTH **位置,並將** jfcPanelSouth **放入**'jpCenter的BorderLayout ** **的** SOUTH **位置。 –

+0

我錯誤的理解你之前。我建議你修改代碼。它幾乎不錯。但是一些'jfcPanelSouth'在'jfcPalenCenter'後面# – GarryMoveOut

相關問題