2016-04-03 40 views
1

我有一個非常基本的GUI,組織了一個GridBagLayout。最複雜的部分是West用ScrollPane填充的底部,右邊是帶有CardLayout的面板,該面板有多個ChartPanel,因此我可以在幾張圖之間切換。將ChartPanel添加到CardLayout中

當我啓動程序時出現了我的問題。

enter image description here

的大小調整問題消失後,我調整在任何方向的框架。我已經確認該chartpanel是問題,因爲沒有將此添加到CardLayout面板修復它。我創建一個空白ChartPanel後來填充它採取一些行動後,但是這是我做了什麼:

public class Tester { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame("Chipmunk: Variant Data Collection Tool"); 
     JPanel hotspotPanel = new JPanel(new CardLayout()); 
     ChartPanel subHotspotPanel = new ChartPanel(null); 
     JPanel indelHotspotPanel = new JPanel(new BorderLayout()); 
     JTextPane resultPane = new JTextPane(); 
     JPanel main = new JPanel(new GridBagLayout()); 
     JPanel header = new JPanel(new BorderLayout()); 

     header.setBackground(Color.WHITE); 
     frame.setLayout(new BorderLayout()); 
     frame.setMinimumSize(new Dimension(875, 600)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 
     frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2); 
     resultPane.setOpaque(false); 
     resultPane.setEditable(false); 
     GridBagConstraints c = new GridBagConstraints(); 
     DocumentFilter filter = new UppercaseDocumentFilter(); 
     JTextField geneField = new JTextField(10); 
     ((AbstractDocument) geneField.getDocument()).setDocumentFilter(filter); 
     geneField.setMinimumSize(geneField.getPreferredSize()); 
     JTextField proEffField = new JTextField(10); 
     proEffField.setMinimumSize(proEffField.getPreferredSize()); 
     String[] mutTypes = { "missense", "nonsense", "frameshift", "nonframeshift"}; 
     JComboBox<String> mutTypeComboBox = new JComboBox<String>(mutTypes); 
     JButton saveResultsButton = new JButton("Save to TSV"); 
     JPanel glass = (JPanel) frame.getGlassPane(); 
     JButton clearButton = new JButton("Clear"); 
     JButton cosmicButton = new JButton("To COSMIC"); 
     JButton dataButton = new JButton("Show Data"); 
     dataButton.setEnabled(false); 
     JButton goButton = new JButton("GO"); 

     c.weightx = 1.0;c.gridx = 0;c.gridy = 0;c.anchor = GridBagConstraints.EAST;c.ipadx=5;c.ipady=5; 
     main.add(new JLabel("Gene: "), c); 
     c.gridx = 1;c.gridy = 0;c.anchor = GridBagConstraints.WEST; 
     main.add(geneField, c); 
     c.gridx = 0;c.gridy = 1;c.anchor = GridBagConstraints.EAST; 
     main.add(new JLabel("Protein Effect: "), c); 
     c.gridx = 1;c.gridy = 1;c.anchor = GridBagConstraints.WEST; 
     main.add(proEffField, c); 
     c.gridx =0;c.gridy = 2;c.anchor = GridBagConstraints.EAST; 
     main.add(new JLabel("Mutation Type: "), c); 
     c.gridx =1;c.gridy = 2;c.anchor = GridBagConstraints.WEST; 
     main.add(mutTypeComboBox, c); 
     c.gridx =0;c.gridy = 3;c.anchor = GridBagConstraints.WEST; 
     main.add(saveResultsButton, c); 
     c.gridx = 0;c.gridy = 3;c.anchor = GridBagConstraints.EAST; 
     main.add(goButton, c); 
     c.gridx = 1;c.gridy = 3;c.anchor = GridBagConstraints.WEST; 
     main.add(clearButton,c); 
     c.gridx = 0;c.gridy = 3;c.anchor = GridBagConstraints.CENTER; 
     main.add(dataButton,c); 
     c.gridx = 1;c.gridy = 3;c.anchor = GridBagConstraints.EAST; 
     main.add(cosmicButton,c); 
     c.gridx = 0; c.gridy =4;c.gridwidth =1; c.weightx = 1.0;c.weighty = 1.0; c.fill = GridBagConstraints.BOTH; 
     JScrollPane scrollPane = new JScrollPane(resultPane); 
     main.add(scrollPane, c); 
     c.gridx = 1; c.gridy =4;c.gridwidth = 1; c.weightx = 1.0;c.weighty = 1.0; c.fill = GridBagConstraints.BOTH; 

     hotspotPanel.add(subHotspotPanel, "SUBPANEL"); 
     hotspotPanel.add(indelHotspotPanel, "INDELPANEL"); 
     hotspotPanel.add(new JPanel(), "BLANK"); 
     main.add(hotspotPanel, c); 
     frame.add(header, BorderLayout.NORTH); 
     frame.add(main, BorderLayout.CENTER); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

} 
+1

'大小調整問題消失後,我調整框架中的任何direction.' - 聽起來像是你將組件添加到幀後的幀已經可見。應在所有組件添加到框架後調用'setVisible(true)'語句。如果您需要更多幫助,請發佈適當的[SSCCE](http://sscce.org/)來證明問題。 – camickr

+0

確認這不是問題,與SSCCE更新後 –

+0

代碼不編譯我們無法訪問ChartPanel類。 「SSCCE」的重點在於簡化代碼來證明問題。我們不只是想要當前代碼的核心轉儲。你真的需要所有組件來證明問題嗎?當你刪除不相關的代碼時,你會發現問題的可能性。我們也沒有DocumentFilter。再次,這與佈局問題有什麼關係? – camickr

回答

1

使用該example,很明顯,一個ChartPanel工作正常在CardLayout。下面的示例覆蓋getPreferredSize(),如圖所示here,以建立ChartPanel的初始大小。在每個卡上使用GridLayout允許圖表在封閉框架調整大小時填充面板。

image

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.util.Random; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.general.DefaultPieDataset; 

/** 
* @see https://stackoverflow.com/a/36392696/230513 
* @see https://stackoverflow.com/a/36243395/230513 
*/ 
public class CardPanel extends JPanel { 

    private static final Random r = new Random(); 
    private static final JPanel cards = new JPanel(new CardLayout()); 
    private final String name; 

    public CardPanel(String name) { 
     super(new GridLayout()); 
     this.name = name; 
     DefaultPieDataset pieDataset = new DefaultPieDataset(); 
     pieDataset.setValue("One", r.nextInt(10) + 10); 
     pieDataset.setValue("Two", r.nextInt(20) + 10); 
     pieDataset.setValue("Three", r.nextInt(30) + 10); 
     JFreeChart chart = ChartFactory.createPieChart3D(
      "3D Pie Chart", pieDataset, true, true, true); 
     chart.setTitle(name); 
     this.add(new ChartPanel(chart) { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(500, (int)(500 * 0.62)); 
      } 
     }); 
    } 

    @Override 
    public String toString() { 
     return name; 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       create(); 
      } 
     }); 
    } 

    private static void create() { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     for (int i = 1; i < 9; i++) { 
      CardPanel p = new CardPanel("Chart " + String.valueOf(i)); 
      cards.add(p, p.toString()); 
     } 
     JPanel control = new JPanel(); 
     control.add(new JButton(new AbstractAction("\u22b2Prev") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       CardLayout cl = (CardLayout) cards.getLayout(); 
       cl.previous(cards); 
      } 
     })); 
     control.add(new JButton(new AbstractAction("Next\u22b3") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       CardLayout cl = (CardLayout) cards.getLayout(); 
       cl.next(cards); 
      } 
     })); 
     f.add(cards, BorderLayout.CENTER); 
     f.add(control, BorderLayout.SOUTH); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
}