2013-04-01 4 views
0

안녕하세요. 기본적인 질문입니다. 내 코드에서는 생성자에서 GUI를 만들고 버튼 변경을 처리하기 위해 ActionListener 클래스를 중첩합니다. 이 코드는 gui를 생성하고 action listener는 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

관련 문제