我正在嘗試將維度傳遞給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();
}
});
}
}
是喲你在'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