如何将维度传递给Swing JPanel创建者类?

问题描述:

我正在尝试将维度传递给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

有在你的代码中有一些问题:

首先,如果使用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

尝试:

contentPane.setSize(intWidth, intHeight); 

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

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

这是[下行](http://*.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