簡體   English   中英

用其他JPanel替換JPanel

[英]Replacing a JPanel with a different JPanel

您好,這是一個基本問題。 在我的代碼中,我在構造函數中創建一個gui,然后嵌套一個ActionListener類以處理按鈕更改。 這段代碼將創建gui,並且動作偵聽器會正確運行actionPerformed方法。 但是,我嘗試了多種方法來更改gui中的面板,但是我覺得我設置程序的方式無法實現。 很抱歉,如果這是重復的話,但是在搜索了一段時間之后,我沒有找到一個很好的例子來幫助我解決問題。

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.
    }
}
}

您需要添加面板並revalidate / repaint JFrame才能使其顯示:

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

最好使用CardLayout來管理此類功能。

嘗試使用CardLayout在面板之間切換。 這是我針對類似問題的解決方案: https : //stackoverflow.com/a/9377623/544983

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM