2012-07-07 112 views
0

我创建了(使用Netbeans)一个JFrame,并在其中放入了两个JPanel(mainPanel和secondPanel)。 当我使用JFrame的菜单栏打开文件时,另一个项目类返回JPanel。 如何更新mainPanel,以便显示从项目的其他类返回的JPanel? 谢谢。如何在同一个项目中使用另一个类的另一个JPanel来更新一个JPanel?

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package nysemarketpick; 

import java.io.File; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 

/** 
* 
* @author skiabox 
*/ 
public class MainForm extends JFrame { 

/** 
* Creates new form MainForm 
*/ 
public MainForm() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    fileChooser = new javax.swing.JFileChooser(); 
    toolbar = new javax.swing.JToolBar(); 
    shortButton = new javax.swing.JButton(); 
    jSeparator1 = new javax.swing.JToolBar.Separator(); 
    midButton = new javax.swing.JButton(); 
    jSeparator2 = new javax.swing.JToolBar.Separator(); 
    longButton = new javax.swing.JButton(); 
    mainPanel = new javax.swing.JPanel(); 
    secondPanel = new javax.swing.JPanel(); 
    jLabel1 = new javax.swing.JLabel(); 
    jMenuBar1 = new javax.swing.JMenuBar(); 
    fileMenu = new javax.swing.JMenu(); 
    Open = new javax.swing.JMenuItem(); 
    Exit = new javax.swing.JMenuItem(); 
    optionsMenu = new javax.swing.JMenu(); 
    shortItem = new javax.swing.JMenuItem(); 
    midItem = new javax.swing.JMenuItem(); 
    longItem = new javax.swing.JMenuItem(); 
    toolsMenu = new javax.swing.JMenu(); 
    helpMenu = new javax.swing.JMenu(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
    setTitle("Nyse Stock Pick"); 

    toolbar.setFloatable(false); 
    toolbar.setRollover(true); 

    shortButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nysemarketpick/S-orange-icon.png"))); // NOI18N 
    shortButton.setText("Short-Term"); 
    shortButton.setFocusable(false); 
    shortButton.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT); 
    shortButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
    toolbar.add(shortButton); 
    toolbar.add(jSeparator1); 

    midButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nysemarketpick/M-lg-icon.png"))); // NOI18N 
    midButton.setText("Mid-Term"); 
    midButton.setFocusable(false); 
    midButton.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT); 
    midButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
    toolbar.add(midButton); 
    toolbar.add(jSeparator2); 

    longButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nysemarketpick/L-blue-icon.png"))); // NOI18N 
    longButton.setText("Long-Term"); 
    longButton.setFocusable(false); 
    longButton.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT); 
    longButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 
    toolbar.add(longButton); 

    org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel); 
    mainPanel.setLayout(mainPanelLayout); 
    mainPanelLayout.setHorizontalGroup(
     mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 0, Short.MAX_VALUE) 
    ); 
    mainPanelLayout.setVerticalGroup(
     mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 537, Short.MAX_VALUE) 
    ); 

    org.jdesktop.layout.GroupLayout secondPanelLayout = new org.jdesktop.layout.GroupLayout(secondPanel); 
    secondPanel.setLayout(secondPanelLayout); 
    secondPanelLayout.setHorizontalGroup(
     secondPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 240, Short.MAX_VALUE) 
    ); 
    secondPanelLayout.setVerticalGroup(
     secondPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(0, 543, Short.MAX_VALUE) 
    ); 

    jLabel1.setText("World Market Status"); 
    jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT); 

    fileMenu.setText("File"); 

    Open.setText("Open"); 
    Open.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      OpenActionPerformed(evt); 
     } 
    }); 
    fileMenu.add(Open); 

    Exit.setText(" Exit"); 
    Exit.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      ExitActionPerformed(evt); 
     } 
    }); 
    fileMenu.add(Exit); 

    jMenuBar1.add(fileMenu); 

    optionsMenu.setText("Options"); 

    shortItem.setText("Short-Term Moving Average"); 
    optionsMenu.add(shortItem); 

    midItem.setText("Mid-Term Moving Average"); 
    optionsMenu.add(midItem); 

    longItem.setText("Long-Term Moving Average"); 
    optionsMenu.add(longItem); 

    jMenuBar1.add(optionsMenu); 

    toolsMenu.setText("Tools"); 
    jMenuBar1.add(toolsMenu); 

    helpMenu.setText("Help"); 
    jMenuBar1.add(helpMenu); 

    setJMenuBar(jMenuBar1); 

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(layout.createSequentialGroup() 
      .addContainerGap() 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
       .add(layout.createSequentialGroup() 
        .add(toolbar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 762, Short.MAX_VALUE) 
        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
        .add(354, 354, 354)) 
       .add(layout.createSequentialGroup() 
        .add(6, 6, 6) 
        .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .add(18, 18, 18) 
        .add(secondPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
        .add(351, 351, 351)))) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
     .add(layout.createSequentialGroup() 
      .addContainerGap() 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 
       .add(toolbar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
      .add(12, 12, 12) 
      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
       .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(secondPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap(33, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold> 

private void ExitActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    System.exit(0); 
}          

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    int returnVal = fileChooser.showOpenDialog(this); 
    if (returnVal == JFileChooser.APPROVE_OPTION) 
    { 
     File file = fileChooser.getSelectedFile(); 


     //What to do with the file 
     //remove current chart 
     mainPanel.removeAll(); 
     //add new chart 
     PriceVolumeChart chart = new PriceVolumeChart(file.getAbsolutePath()); 

     mainPanel.add(chart.getChart()); 
     mainPanel.validate(); 
    } 
}          

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* 
    * Set the Nimbus look and feel 
    */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* 
    * If Nimbus (introduced in Java SE 6) is not available, stay with the 
    * default look and feel. For details see 
    * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* 
    * Create and display the form 
    */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      new MainForm().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JMenuItem Exit; 
private javax.swing.JMenuItem Open; 
private javax.swing.JFileChooser fileChooser; 
private javax.swing.JMenu fileMenu; 
private javax.swing.JMenu helpMenu; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JMenuBar jMenuBar1; 
private javax.swing.JToolBar.Separator jSeparator1; 
private javax.swing.JToolBar.Separator jSeparator2; 
private javax.swing.JButton longButton; 
private javax.swing.JMenuItem longItem; 
private javax.swing.JPanel mainPanel; 
private javax.swing.JButton midButton; 
private javax.swing.JMenuItem midItem; 
private javax.swing.JMenu optionsMenu; 
private javax.swing.JPanel secondPanel; 
private javax.swing.JButton shortButton; 
private javax.swing.JMenuItem shortItem; 
private javax.swing.JToolBar toolbar; 
private javax.swing.JMenu toolsMenu; 
// End of variables declaration 
} 



public class PriceVolumeChart 
{ 
static FileReader fileReader; 
static File file; 
static TimeSeries t1 = new TimeSeries("49-day moving average"); 
static String filename; 

JPanel myPanel; 

/** 
* Default constructor 
*/ 
public PriceVolumeChart(String absoluteFilePath) 
{ 
    //super(title); 
    filename = absoluteFilePath; 
    //JPanel panel = createDemoPanel(); 
    //panel.setPreferredSize(new Dimension(500, 270)); 
    //setContentPane(panel); 
    myPanel = createDemoPanel(); 
    myPanel.setPreferredSize(new Dimension(500, 270)); 
} 


//create price dataset 
//hard-coded here 
private static OHLCDataset createPriceDataset(String filename) 
{ 
    //the following data is taken from http://finance.yahoo.com/ 
    //for demo purposes... 


    OHLCSeries s1 = new OHLCSeries(filename); 

    try { 
     BufferedReader in = new BufferedReader(new FileReader(filename)); 
     DateFormat df = new SimpleDateFormat("yyyyMMdd"); 
     String inputLine; 
     in.readLine(); 
     while ((inputLine = in.readLine()) != null) { 
      StringTokenizer st = new StringTokenizer(inputLine, ","); 
      Date date  = df.parse(st.nextToken()); 
      double open  = Double.parseDouble(st.nextToken()); 
      double high  = Double.parseDouble(st.nextToken()); 
      double low  = Double.parseDouble(st.nextToken()); 
      double close = Double.parseDouble(st.nextToken()); 
      double volume = Double.parseDouble(st.nextToken()); 
      //double adjClose = Double.parseDouble(st.nextToken()); 
      s1.add(new Day(date), open, high, low, close); 
      t1.add(new Day(date), close); 
     } 
     in.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 



    OHLCSeriesCollection dataset = new OHLCSeriesCollection(); 
    dataset.addSeries(s1); 

    return dataset; 
} 


//create volume dataset 
private static IntervalXYDataset createVolumeDataset(String filename) 
{ 
    //create dataset 2... 
    TimeSeries s1 = new TimeSeries("Volume"); 

    try { 
     BufferedReader in = new BufferedReader(new FileReader(filename)); 
     DateFormat df = new SimpleDateFormat("yyyyMMdd"); 
     String inputLine; 
     in.readLine(); 
     while ((inputLine = in.readLine()) != null) { 
      StringTokenizer st = new StringTokenizer(inputLine, ","); 
      Date date = df.parse(st.nextToken()); 
      st.nextToken(); 
      st.nextToken(); 
      st.nextToken(); 
      st.nextToken(); 
      double volume = Double.parseDouble(st.nextToken()); 
      //double adjClose = Double.parseDouble(st.nextToken()); 
      s1.add(new Day(date), volume); 
     } 
     in.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 



    return new TimeSeriesCollection(s1); 
} 


private static JFreeChart createCombinedChart() 
{ 
    OHLCDataset data1 = createPriceDataset(filename); 

    XYItemRenderer renderer1 = new HighLowRenderer(); 
    renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); 
    renderer1.setSeriesPaint(0, Color.blue); 
    DateAxis domainAxis = new DateAxis("Date"); 
    NumberAxis rangeAxis = new NumberAxis("Price"); 
    rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00")); 
    rangeAxis.setAutoRange(true); 
    rangeAxis.setAutoRangeIncludesZero(false); 
    XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1); 
    plot1.setBackgroundPaint(Color.lightGray); 
    plot1.setDomainGridlinePaint(Color.white); 
    plot1.setRangeGridlinePaint(Color.white); 
    plot1.setRangePannable(true); 

    //Overlay the Long-Term Trend Indicator 
    TimeSeries dataset3 = MovingAverage.createMovingAverage(t1, "LT", 49, 49); 
    TimeSeriesCollection collection = new TimeSeriesCollection(); 
    collection.addSeries(dataset3); 
    plot1.setDataset(1, collection); 
    plot1.setRenderer(1, new StandardXYItemRenderer()); 



    //add a second dataset (volume) and renderer 
    IntervalXYDataset data2 = createVolumeDataset(filename); 
    XYBarRenderer renderer2 = new XYBarRenderer(); 
    renderer2.setDrawBarOutline(false); 
    renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00"))); 
    renderer2.setSeriesPaint(0, Color.red); 

    XYPlot plot2 = new XYPlot(data2, null, new NumberAxis("Volume"), renderer2); 
    plot2.setBackgroundPaint(Color.lightGray); 
    plot2.setDomainGridlinePaint(Color.white); 
    plot2.setRangeGridlinePaint(Color.white); 

    //middle pane oscillators 
    TimeSeries threeMA = MovingAverage.createMovingAverage(t1, "SL1", 3, 3); 
    TimeSeries tenMA = MovingAverage.createMovingAverage(t1, "SL2", 10, 10); 

    List<TimeSeriesDataItem> threeItemsList = new ArrayList<TimeSeriesDataItem>(); 
    List<TimeSeriesDataItem> tenItemsList = new ArrayList<TimeSeriesDataItem>(); 
    threeItemsList = threeMA.getItems(); 
    tenItemsList = tenMA.getItems(); 

    for (TimeSeriesDataItem item: threeItemsList) 
    { 
     System.out.println("three" + item.getValue().doubleValue() + " " + item.getPeriod().toString()); 
    } 

    for (TimeSeriesDataItem item: tenItemsList) 
    { 
     System.out.println("ten" + item.getValue().doubleValue() + " " + item.getPeriod().toString()); 
    } 

    TimeSeries slDataset = new TimeSeries("SL"); 
    int i = 0; 
    //List<TimeSeriesDataItem> slItemsList = new ArrayList<TimeSeriesDataItem>(); 
    TimeSeriesDataItem myItem; 
    Day threeDate; 

    for (TimeSeriesDataItem item: threeItemsList) 
    { 
     //RegularTimePeriod variable = item.getPeriod(); 
     if (item.getPeriod().compareTo(tenItemsList.get(0).getPeriod()) < 0) 
     { 
      continue; 
     } 
     else 
     { 
      double result = item.getValue().doubleValue() - tenItemsList.get(i).getValue().doubleValue(); 
      Day currentDay = (Day)item.getPeriod(); 
      myItem = new TimeSeriesDataItem(currentDay, result); 
      slDataset.add(myItem); 

      //slItemsList.add(myItem); 
      i++; 
     } 


    } 

    System.out.println("slDataset count : " + slDataset.getItemCount()); 



    TimeSeriesCollection collectionSL = new TimeSeriesCollection(); 
    collectionSL.addSeries(slDataset); 

    //create middle line (ML) 
    XYLineAndShapeRenderer rendererFinal = new XYLineAndShapeRenderer(true, false); 
    rendererFinal.setBaseShapesVisible(false); 
    TimeSeries ml = MovingAverage.createMovingAverage(slDataset, "ML", 16, 16); 
    TimeSeriesCollection collectionML = new TimeSeriesCollection(); 
    collectionML.addSeries(ml); 

    DateAxis domainMiddleAxis = new DateAxis(); 
    NumberAxis rangeMiddleAxis = new NumberAxis("Value"); 
    StandardXYItemRenderer rendererMiddle = new StandardXYItemRenderer(); 
    rendererMiddle.setBaseShapesVisible(false); 
    //rendererMiddle.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00"))); 
    XYPlot plot3 = new XYPlot(collectionSL, domainMiddleAxis, rangeMiddleAxis, rendererFinal); 
    plot3.setBackgroundPaint(Color.lightGray); 
    plot3.setDomainGridlinePaint(Color.white); 
    plot3.setRangeGridlinePaint(Color.white); 
    plot3.setRangePannable(true); 
    plot3.setDomainPannable(true); 



    //plot3.setDataset(collectionSL); 
    //plot3.setRenderer(new StandardXYItemRenderer()); 
    //StandardXYItemRenderer rendererML = new StandardXYItemRenderer(); 
    plot3.setDataset(1, collectionML); 
    plot3.setRenderer(1, rendererMiddle); 
    //XYLineAndShapeRenderer r = new XYLineAndShapeRenderer(); 
    //r.setBaseShapesVisible(false); 
    //plot3.setRenderer(1, r); 



    CombinedDomainXYPlot cplot = new CombinedDomainXYPlot(domainAxis); 
    cplot.add(plot1, 3); 
    cplot.add(plot3, 2); 
    cplot.add(plot2, 2); 
    cplot.setGap(8.0); 
    cplot.setDomainGridlinePaint(Color.white); 
    cplot.setDomainGridlinesVisible(true); 
    cplot.setDomainPannable(true); 


    //return the new combined chart 
    JFreeChart chart = new JFreeChart("Sun Microsystems (SUNW)", JFreeChart.DEFAULT_TITLE_FONT, cplot, true); 


    ChartUtilities.applyCurrentTheme(chart); 
    renderer2.setShadowVisible(false); 
    renderer2.setBarPainter(new StandardXYBarPainter()); 

    return chart; 
} 


//create a panel 
public static JPanel createDemoPanel() 
{ 
    JFreeChart chart = createCombinedChart(); 
    ChartPanel panel = new ChartPanel(chart); 
    panel.setMouseWheelEnabled(true); 

    return panel; 
} 

//return chart 
public JPanel getChart() 
{ 
    return myPanel; 
} 

} 
+0

你能提供一个代码示例? – Derk 2012-07-07 13:12:36

+0

好的,我会更新原文。 – skiabox 2012-07-07 13:21:27

+0

似乎在JFrame级别上没有使用布局管理器是一个缺点 – skiabox 2012-07-07 14:37:59

回答

1

看来你想使用mainPanel作为包含图表的面板。尝试在面板中添加和删除面板,而不是将面板分配给mainPanel。使其成为容器,并且不要试图通过将另一个Jpanel分配给该变量来更改它。

//What to do with the file 
PriceVolumeChart chart = new PriceVolumeChart(file.getAbsolutePath()); 
mainPanel = chart.getChart(); 

到:

//remove current chart 
mainPanel.removeAll(); //assuming you have nothing else in main panel other than a chart panel; 
//add new chart 
PriceVolumeChart chart = new PriceVolumeChart(file.getAbsolutePath()); 
mainPanel.add(chart); 
mainPanel.validate(); 

注注:我没

的方法 “私人无效OpenActionPerformed(java.awt.event.ActionEvent中EVT)” 试图改变以下

来这个我的自我。这个想法是从这里采取的:Change a JPanel dynamically based on JRadioButton

我修改它到我认为适合你的代码的地方。 我刚刚使用与原始问题相同的代码,因此也许您应该尝试在答案中提出的内容,因为它看起来像正确的方式。

编辑:

在你做你的代码的变化,你的评论,添加以下后也:

在功能

public MainForm() { 
    initComponents(); 
} 

更换主的布局管理器组件初始化后的面板。例如:

public MainForm() { 
    initComponents(); 
    mainPanel.setLayout(new FlowLayout()); 
} 
+0

似乎没有工作,我不知道我在做什么错。我会用新的代码更新原始的邮政编码。 – skiabox 2012-07-07 15:50:17

+0

@trashgod:也许你可以在这里帮助你的专业知识 – skiabox 2012-07-07 16:03:18

+0

@skiabox查看编辑 – 2012-07-07 17:57:25

0

我用下面的代码和它的工作对我来说:

JPanel tempPanel = new YourPanelClass(); 
tempPanel.setBounds(DisplayPanel.getBounds()); // DisplayPanel is a panel object with which you want to replace new Panel. 
mainPanel.removeAll(); //mainPanel is container for panels 
mainPanel.revalidate(); 
mainPanel.repaint(); 
mainPanel.add(tempPanel); 
相关问题