简体   繁体   中英

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

The problem I have is when I press the "back" button, the program opens up the correct GUI but doesn't get rid of the "LiftAnalysis" GUI. I have almost no experience with JFreeChart or using TimeSeriesCharts. Please suggest a method to hide the current GUI when the back button is pressed. Code is below

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);  
        });
}  
} 

There are a lot of issues on your program:

  1. You're not modifying any of the properties of a JFrame , so there's no need to extend from it, rather create an instance of a JFrame . See Extends JFrame vs. creating it inside the program

    extends JFrame
  2. You're making use of null-layouts , this will give you more troubles than solutions, using setBounds and null-layouts might seem like the easiest approach to build complex GUIs at first, but it really isn't, because Swing has to deal with different OS, PLAFs, screen sizes and resolutions. See Why is it frowned upon to use a null layout in Swing? and this answer 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

    The above line, makes me think that you're using multiple JFrames , which isn't a good practice. See 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 that uses both of them.

    If you insist on keeping the multiple JFrames you have to call dispose(); instead of panel.setVisible(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM