【问题标题】:My Java Swing GUI which contains a JFreeChart won't close我的包含 JFreeChart 的 Java Swing GUI 不会关闭
【发布时间】:2022-01-04 16:11:08
【问题描述】:

我遇到的问题是,当我按下“返回”按钮时,程序会打开正确的 GUI,但没有摆脱“LiftAnalysis”GUI。我几乎没有使用 JFreeChart 或使用 TimeSeriesCharts 的经验。请提出一种在按下后退按钮时隐藏当前 GUI 的方法。 代码如下

import java.awt.Color;  
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;  
import javax.swing.WindowConstants;  
import org.jfree.chart.ChartFactory;  
import org.jfree.chart.ChartPanel;  
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;  
import org.jfree.data.time.Day;  
import org.jfree.data.time.TimeSeries;  
import org.jfree.data.time.TimeSeriesCollection;  
import org.jfree.data.xy.XYDataset;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;  
  
public class LiftAnalysis extends JFrame {  
  
  private static final long serialVersionUID = 1L;  
  
  public LiftAnalysis(){  
    //super(title);  
    // Create dataset  
    XYDataset dataset = createDataset();  
    // Create chart  
    JFreeChart chart = ChartFactory.createTimeSeriesChart(  
        "Lift Analysis", // Chart  
        "Date", // X-Axis Label  
        "Number", // Y-Axis Label  
        dataset);  
  
    //Changes background color  
    //XYPlot plot = (XYPlot)chart.getPlot();  
    Plot plot = chart.getPlot();

    plot.setBackgroundPaint(new Color(255,228,196));  
    chart.setBackgroundPaint(Color.DARK_GRAY);

    ChartPanel panel = new ChartPanel(chart);  
    setContentPane(panel);  
    panel.setLayout(null);
    
    if (plot instanceof CategoryPlot) {
        setAxisFontColor(((CategoryPlot) plot).getDomainAxis(), Color.white);
        setAxisFontColor(((CategoryPlot) plot).getRangeAxis(), Color.white);
    } else if (plot instanceof XYPlot) {
        setAxisFontColor(((XYPlot) plot).getDomainAxis(), Color.white);
        setAxisFontColor(((XYPlot) plot).getRangeAxis(), Color.white);
    }
    
    ImageIcon helfIcon = new ImageIcon("C:\\Users\\joshu\\Desktop\\School\\Computer Science\\logo_small_icon_only_inverted.png");
    
    Image helfImage = helfIcon.getImage();
    Image modifiedHelfImage = helfImage.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
    helfIcon = new ImageIcon(modifiedHelfImage);
    
    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(helfIcon);
    lblNewLabel.setBounds(736, -14, 62, 69);
    
    panel.add(lblNewLabel);
    
    
    JButton btnBack = new JButton("Back");

        btnBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AnalysisPage p10 = new AnalysisPage();
                panel.setVisible(false);
                p10.run(); //Opens a different GUI
            }
        });
    btnBack.setBackground(Color.GRAY);
    btnBack.setFont(new Font("Tahoma", Font.BOLD, 18));
    btnBack.setBounds(43, 318, 120, 34);
    panel.add(btnBack);
  }  
  
  private XYDataset createDataset() {  
    TimeSeriesCollection dataset = new TimeSeriesCollection();  
  
    TimeSeries series1 = new TimeSeries("Bench");  
    series1.add(new Day(15, 12, 2021), 675);
    series1.add(new Day(18, 12, 2021), 675);
    series1.add(new Day(21, 12, 2021), 702);
    series1.add(new Day(28, 12, 2021), 720);
    series1.add(new Day(03, 01, 2022), 738);
    dataset.addSeries(series1);  
  
    TimeSeries series2 = new TimeSeries("Squat");  
    series2.add(new Day(16, 12, 2021), 1750);
    series2.add(new Day(19, 12, 2021), 1800);
    series2.add(new Day(22, 12, 2021), 1850);
    series2.add(new Day(29, 12, 2021), 1850);
    series2.add(new Day(04, 01, 2022), 1900);
    dataset.addSeries(series2);
    
    TimeSeries series3 = new TimeSeries("Deadlift");  
    series3.add(new Day(17, 12, 2021), 1425);
    series3.add(new Day(20, 12, 2021), 1455);
    series3.add(new Day(23, 12, 2021), 1500);
    series3.add(new Day(30, 12, 2021), 1575);
    series3.add(new Day(05, 01, 2022), 1650);
    dataset.addSeries(series3);
      
    
  
    return dataset;  
  }  
  private void setAxisFontColor(Axis axis, Color fontColor) {
        if (!fontColor.equals(axis.getLabelPaint()))
            axis.setLabelPaint(fontColor);
        if (!fontColor.equals(axis.getTickLabelPaint()))
            axis.setTickLabelPaint(fontColor);
    }

  
  public static void main(String[] args) {  
     
    SwingUtilities.invokeLater(() -> {  
      LiftAnalysis example = new LiftAnalysis();  
      example.setSize(800, 400);  
      example.setLocationRelativeTo(null);  
      example.setVisible(true);  
      example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
      
    });  
  }


  
public void run() {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(() -> {  
          LiftAnalysis example = new LiftAnalysis();  
          example.setSize(800, 400);  
          example.setLocationRelativeTo(null);  
          example.setVisible(true);  
          example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
        });
}  
} 

【问题讨论】:

    标签: java swing jfreechart timeserieschart


    【解决方案1】:

    你的程序有很多问题:

    1. 您没有修改JFrame 的任何属性,因此无需从它扩展,而是创建JFrame 的实例。见Extends JFrame vs. creating it inside the program

      extends JFrame
      
    2. 您正在使用null-layouts,这会给您带来比解决方案更多的麻烦,使用setBoundsnull-layouts 一开始似乎是构建复杂GUI 的最简单方法,但实际上并非如此,因为 Swing 必须处理不同的操作系统、PLAF、屏幕尺寸和分辨率。如果您坚持使用null-layout 而不是正确的Layout Managers,请参阅Why is it frowned upon to use a null layout in Swing? 和此answer 可能会发生什么。

      panel.setLayout(null);
      
    3. 程序打开了正确的 GUI,但没有摆脱“LiftAnalysis”GUI

      上面的行让我觉得你使用了多个JFrames,这不是一个好习惯。请参阅The Use of Multiple JFrames: Good or Bad Practice?(坏),您将继续在不同的位置为用户弹出窗口,或者您的程序的多个实例正在运行或显示在任务栏中,而不是您可以使用CardLayout 或使用@987654327 @ 向用户显示信息,这里是一个example,它同时使用了它们。

      如果您坚持保留多个JFrames,则必须调用dispose(); 而不是panel.setVisible(false);

    【讨论】:

    • 另外,考虑CardLayout,见here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2010-09-10
    • 2011-09-11
    • 1970-01-01
    • 2017-03-11
    • 2010-11-21
    • 1970-01-01
    相关资源
    最近更新 更多