2012-07-31 13 views
1

我正在嘗試將維度傳遞給Java Swing JPanel創建者類。到目前爲止沒有運氣。 我甚至把contentPane.setPreferredSize(new Dimension(intWidth, intHeight));放在try/catch塊中,我沒有得到任何系統輸出。我傳遞了一個相當可觀的價值,即frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));,但該應用程序仍然與其周圍的組件一樣小。可以做些什麼來解決它?謝謝。如何將維度傳遞給Swing JPanel創建者類?

import java.awt.Container; 
import java.awt.Dimension; 
import javax.swing.JPanel; 

public final class ContentPaneCreator { 

    public static Container createContentPane(String color, int intWidth, int intHeight) { 

     JPanel contentPane = new JPanel(); 
     // Pass the colour 
     contentPane.setBackground(ColorFactory.getInstance().getColor(color)); 
     // Pass the dimensions 
     try { 
     contentPane.setPreferredSize(new Dimension(intWidth, intHeight)); 
     } 
     catch (Exception ex) { 
      System.out.println(ex); 
     } 
     return contentPane; 
    } 
} 

StickyNotes()

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class StickyNotes extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public static String appName; 
    public static String fileConfirmSave; 
    public static String txtConfirmChange; 


    private void createStickyNotesGUI() { 

     ConfigProperties config = new ConfigProperties(); 
     // ConfigProperties config2 = new ConfigProperties().getFileIn(); 

     appName = config.getConfigProperties("appName").toUpperCase(); 
     fileConfirmSave = config.getConfigProperties("fileConfirmSave"); 
     txtConfirmChange = config.getConfigProperties("txtConfirmChange"); 

     // Create and set up the window. 
     JFrame frame = new JFrame(); 
     frame.setTitle(appName); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     /* BEGIN ROOT Pane: */ 

     /* BEGIN Layered Pane: */ 
     // Add Main Menu 
     MainMenuBar mainMenu = new MainMenuBar(); 
     frame.setJMenuBar(mainMenu.createMainMenuBar(frame)); 

     /* BEGIN CONTENT Pane(s): */ 

     // Add JPanel Content // can I pass a layout object? 
     frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255)); 

     // Add Tool Bar /* needs to be run AFTER we .setContentPane() in order for it to be layerd on top */ 
     ToolBarCreator toolBar = new ToolBarCreator(); 
     frame.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH); 


     // TODO 
     /* 
     * Pass dynamic color values to LabelCreator() once you get 
     * newStickyNote() working 
     */ 
     frame.getContentPane().add(
       LabelCreator.createLabel(frame, "Java Swing", "purple"), 
       BorderLayout.NORTH); 

     // Display the window here with JFrame// 
     //frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     // TODO 
     /* set configuration to remember frame size */ 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void doExit(JFrame frame) { 
     boolean fDirty = true; 
     if (fDirty) 
      switch (JOptionPane.showConfirmDialog(StickyNotes.this, 
        fileConfirmSave, txtConfirmChange, 
        JOptionPane.YES_NO_OPTION)) { 
      case JOptionPane.YES_OPTION: 
       // if (doSave()) 
       frame.dispose(); 
       break; 
      case JOptionPane.NO_OPTION: 
       frame.dispose(); 
      } 
     else 
      frame.dispose(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new StickyNotes().createStickyNotesGUI(); 
      } 
     }); 
    } 
} 
+2

是喲你在'Window'上調用'pack'? – Jeffrey 2012-07-31 00:30:43

+3

1)**爲什麼**要爲內容窗格設置大小? 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-07-31 00:35:07

+0

@Jeffrey是的,我使用frame.pack(); – JoJo 2012-07-31 00:43:11

回答

1

有在你的代碼中有一些問題:

首先,如果使用frame.setLayout(new FlowLayout(FlowLayout.LEFT));的FlowLayout)那麼這裏frame.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH);你爲什麼要指定BorderLayout.NORTH它沒有效果

再次frame.getContentPane().add(LabelCreator.createLabel(frame, "Java Swing", "purple"),BorderLayout.NORTH);

編輯案例:
爲了您contentpane問題,首先看在下面的代碼(演示):

JFrame frame= new JFrame(); 
frame.getContentPane().setBackground(Color.red); 
frame.getContentPane().setPreferredSize(new Dimension(width, height)); 
frame.pack(); 
frame.setVisible(true); 

當你通過不同widthheight你會看到全尺寸的frame,因爲我還沒有指定setSize任何地方將得到實現,這樣做的原因是

  1. 出現在屏幕上,每GUI組件必須是包含層次結構的一部分。包容層次結構是以頂級容器作爲其根的組件樹。我們會稍微介紹一下。

  2. 每個GUI組件只能包含一次。如果一個組件已經在一個容器中,並且您嘗試將其添加到另一個容器中,則組件將從第一個容器中移除,然後添加到第二個容器中。

  3. 每個頂級容器都有一個內容窗格,一般而言,它包含(直接或間接)該頂級容器的GUI中的可見組件。

  4. 您可以選擇將菜單欄添加到頂層容器。菜單欄按照慣例定位在頂層容器內,但位於內容窗格之外。一些外觀和感覺,例如Mac OS的外觀和感覺,可讓您選擇將菜單欄放置在更適合外觀和感覺的其他位置,例如屏幕頂部。

所以結論是改變contentpane尺寸將會改變frame大小。

當你的聲明frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));正在使panelcontentpane,如此反覆改變它的大小是改變frame大小

但周圍的其他方法是:

frame.getContentPane().add(ContentPaneCreator.createContentPane("darkseagreen", 800, 255)); 

addcontainerjframe然後add組件container(你的情況的JPanel)

希望這將有助於和一件事多我也被用來編碼像你創建的每小目的methodsclasses,但有時他們提出的複雜,而不是簡單,所以我給你的建議是遵循的設計模式MVC或任何其它你喜歡

+0

謝謝!我在這裏有很多東西要消化,但是你幫助我得到整個應用程序,並開始進行更正。我想知道爲什麼我可以使用它,而且它仍然有效:frame.add(ContentPaneCreator.createStickyNote(「darkseagreen」,600,755)); – JoJo 2012-07-31 23:29:18

1

嘗試:

contentPane.setSize(intWidth, intHeight); 

我敢肯定,上面是足夠的,但你也可以試試:

contentPane.setPreferredSize(new Dimension(intWidth, intHeight)); 
contentPane.setMinimumSize(new Dimension(intWidth, intHeight)); 
contentPane.setMaximumSize(new Dimension(intWidth, intHeight)); 
+1

這是[下行](http://stackoverflow.com/q/7229226/230513)。 – trashgod 2012-07-31 00:53:23

+1

是的,也許更好的答案是避免必須使用這些方法。但他的方法簽名是'createContentPane(String color,int intWidth,int intHeight)',所以我認爲這是一個實用的解決方案,無論他/她想要做什麼。 – Zong 2012-07-31 01:00:41

相關問題