2013-04-01 44 views
0

嗨,这是一个基本的问题。在我的代码中,我在构造函数中创建了一个gui,然后嵌套一个ActionListener类来处理按钮更改。此代码将创建gui,并且动作侦听器正确运行actionPerformed方法。然而,我已经尝试了多种方法来改变gui中的面板,但是我觉得我已经设置了程序,不可能这样做。对不起,如果这是一个重复,但在S.O.上搜索一段时间后。我还没有找到一个可以帮助我解决问题的好例子。用不同的JPanel替换JPanel

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

什么是gp.determinePlotToPlot()? –

+0

它是一个单独的类,用于确定应显示哪个新面板 – swhite

回答

2

您将需要添加面板和revalidate/repaintJFrame它出现:

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

最好使用CardLayout来管理这种类型的功能。

+0

这里是[示例](http://stackoverflow.com/questions/8248152/jframe-removing-jpanels-and-adding-a-new-jpanel?answertab =最老的#tab-top)虽然会建议看一下'CardLayout'。 – Reimeus

+0

谢谢我会看看Juvanis发布的例子,但这对我来说确实有效。不过CardLayout看起来要容易得多。谢谢 – swhite