我的 Java 包含 JFreeChart 的 Swing GUI 无法关闭

My Java Swing GUI which contains a JFreeChart won't close

我遇到的问题是当我按下“后退”按钮时,程序打开了正确的 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);  
        });
}  
} 

你的程序有很多问题:

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

    extends JFrame
    
  2. 你正在使用null-layouts,这会给你带来比解决方案更多的麻烦,使用setBoundsnull-layouts似乎是最简单的方法一开始构建复杂的 GUI,但实际上并非如此,因为 Swing 必须处理不同的 OS、PLAF、屏幕尺寸和分辨率。参见 Why is it frowned upon to use a null layout in Swing? and this for an example of what could happen if you insist on using null-layout instead of proper Layout Managers

    panel.setLayout(null);
    
  3. the program opens up the correct GUI but doesn't get rid of the "LiftAnalysis" GUI

    上面这行让我觉得您使用了多个 JFrames,这不是一个好的做法。请参阅使用两者的 The Use of Multiple JFrames: Good or Bad Practice? (Bad), you're going to keep popping windows in different places for the user or have multiple instances of your program running or showing up in the task bar, instead you could be using CardLayout or use Dialogs to display information to the user, here's an example

    如果你坚持保留倍数 JFrames 你必须调用 dispose(); 而不是 panel.setVisible(false);