如何将维度传递给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();
}
});
}
}
有在你的代码中有一些问题:
首先,如果使用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);
当你通过不同值width
和height
你会看到全尺寸的frame
,因为我还没有指定setSize
任何地方将得到实现,这样做的原因是
出现在屏幕上,每GUI组件必须是包含层次结构的一部分。包容层次结构是以*容器作为其根的组件树。我们会稍微介绍一下。
每个GUI组件只能包含一次。如果一个组件已经在一个容器中,并且您尝试将其添加到另一个容器中,则组件将从第一个容器中移除,然后添加到第二个容器中。
每个*容器都有一个内容窗格,一般而言,它包含(直接或间接)该*容器的GUI中的可见组件。
您可以选择将菜单栏添加到顶层容器。菜单栏按照惯例定位在顶层容器内,但位于内容窗格之外。一些外观和感觉,例如Mac OS的外观和感觉,可让您选择将菜单栏放置在更适合外观和感觉的其他位置,例如屏幕顶部。
所以结论是改变contentpane
尺寸将会改变frame
大小。
当你的声明frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));
正在使panel
为contentpane
,如此反复改变它的大小是改变frame
大小
但周围的其他方法是:
frame.getContentPane().add(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));
add
container
至jframe
然后add
你组件到container
(你的情况的JPanel)
希望这将有助于和一件事多我也被用来编码像你创建的每小目的methods
和classes
,但有时他们提出的复杂,而不是简单,所以我给你的建议是遵循的设计模式像MVC
或任何其它你喜欢
谢谢!我在这里有很多东西要消化,但是你帮助我得到整个应用程序,并开始进行更正。我想知道为什么我可以使用它,而且它仍然有效: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));
是哟你在'Window'上调用'pack'? – Jeffrey 2012-07-31 00:30:43
1)**为什么**要为内容窗格设置大小? 2)为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2012-07-31 00:35:07
@Jeffrey是的,我使用frame.pack(); – JoJo 2012-07-31 00:43:11