我試圖在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);
}
}
後@ 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);
}
...
}
你的建議是非常有幫助的。添加您的建議後,我接近解決我的問題。只有一件事我不明白。爲什麼中心板現在寬闊平坦,南面板更大。我想要南面板是寬闊的,中心面板要大一些,主圖表 – GarryMoveOut
@GarryMoveOut我認爲它和以前一樣是一樣的問題,你仍然在'contentPane'中添加'jpCenter',所以它會改變大小以適應向左走。然後你在contentPane SOUTH部分添加'jpSouth' - 再次垂直擴展。嘗試將** jfcPanelCenter **放入** NORTH **位置,並將** jfcPanelSouth **放入**'jpCenter的BorderLayout ** **的** SOUTH **位置。 –
我錯誤的理解你之前。我建議你修改代碼。它幾乎不錯。但是一些'jfcPanelSouth'在'jfcPalenCenter'後面# – GarryMoveOut