简体   繁体   English

用其他JPanel替换JPanel

[英]Replacing a JPanel with a different JPanel

Hi this is a bit of a basic question. 您好,这是一个基本问题。 In my code I create a gui in a constructor then nest a ActionListener class to handle button changes. 在我的代码中,我在构造函数中创建一个gui,然后嵌套一个ActionListener类以处理按钮更改。 This code will create the gui and the action listener runs through the actionPerformed method correctly. 这段代码将创建gui,并且动作侦听器会正确运行actionPerformed方法。 However, I've tried multiple ways to change the panel in the gui but I feel like the way I have the program set up it is not possible for this to work. 但是,我尝试了多种方法来更改gui中的面板,但是我觉得我设置程序的方式无法实现。 Sorry if this is a repeat but after searching for a while on SO I haven't found a good example that would help me with my problem. 很抱歉,如果这是重复的话,但是在搜索了一段时间之后,我没有找到一个很好的例子来帮助我解决问题。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.math.plot.Plot2DPanel;
import org.math.plot.plotObjects.BaseLabel;

public class GraphGui extends JFrame {

//default width and height of the GUI
private static final int WIDTH = 1200;
private static final int HEIGHT = 700;

GraphPlot gp = new GraphPlot();
Plot2DPanel plotPanel =gp.determinePlotToPlot("duration");

/**
 * This is the constructor that initializes the JFrame and the layout of the GUI.
 * The radio buttons are also created here and grouped accordingly.
 */
public GraphGui() {
    //title of GUI
    setTitle("VibeTech Graph Gui");

    //First JRadioButton for date vs duration
    JRadioButton durToDate = new JRadioButton("Duration vs. Date");
    durToDate.addActionListener(new RadioButtonListener());
    durToDate.setActionCommand("duration");
    durToDate.setSelected(true);

    //JRadioButton for weight vs date
    JRadioButton weightToDate = new JRadioButton("Weight vs. Date");
    weightToDate.addActionListener(new RadioButtonListener());
    weightToDate.setActionCommand("weight");

    //JRadioButton for plan type vs date
    JRadioButton planToDate = new JRadioButton("Plan vs. Date");
    planToDate.addActionListener(new RadioButtonListener());
    planToDate.setActionCommand("level");

    //button group of the buttons to display them as one group
    ButtonGroup group = new ButtonGroup();
    group.add(planToDate);
    group.add(weightToDate);
    group.add(durToDate);

    //create JPanel to add objects to
    JPanel jplRadio = new JPanel();
    jplRadio.setLayout(new GridLayout(0, 1));
    //add radio buttons
    jplRadio.add(planToDate);
    jplRadio.add(weightToDate);
    jplRadio.add(durToDate);

    Plot2DPanel dvt = new Plot2DPanel();
    dvt.addLinePlot("Duration over Time", gp.getDate(), gp.getDuration());
    BaseLabel title = new BaseLabel("Duration over Time", Color.RED,
            0.5, 1.1);
    title.setFont(new Font("Courier", Font.BOLD, 20));
    dvt.addPlotable(title);
    dvt.setAxisLabels("Time", "Duration");

    setLayout(new BorderLayout());
    add(jplRadio, BorderLayout.WEST);
    add(plotPanel, BorderLayout.EAST);

    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

//main method to run program
public static void main(String [ ] args)
{
    //create new GUI
    @SuppressWarnings("unused")
    GraphGui test = new GraphGui();
}

//create a radio button listener to switch graphs on button press
class RadioButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("duration")) {
            plotPanel = gp.determinePlotToPlot("duration");
        } else if (e.getActionCommand().equals("weight")) {
            plotPanel = gp.determinePlotToPlot("weight");
        } else if (e.getActionCommand().equals("level")) {
            plotPanel = gp.determinePlotToPlot("level");
        }
        //here is where I tried to do removes, adds, and validates but
        //I have trouble getting to the frame itself to remove the JPanel
        //component. I think this is a setup problem.
    }
}
}

You would need to add the panel and revalidate / repaint the JFrame for it to appear: 您需要添加面板并revalidate / repaint JFrame才能使其显示:

add(plotPanel, BorderLayout.EAST);
revalidate();
repaint();

Better to use CardLayout to manage this type of functionality. 最好使用CardLayout来管理此类功能。

Try using CardLayout for switching between panels. 尝试使用CardLayout在面板之间切换。 Here is my solution for a similar question: https://stackoverflow.com/a/9377623/544983 这是我针对类似问题的解决方案: https : //stackoverflow.com/a/9377623/544983

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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