我正在尝试将维度传递给 Java Swing JPanel 创建者类。到目前为止没有运气。我什至把它contentPane.setPreferredSize(new Dimension(intWidth, intHeight));
放在一个 try/catch 块中,但我没有得到任何系统输出。我传递了一个相当大的价值,frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));
但应用程序仍然和它周围的组件一样小。可以做些什么来修复它?谢谢。
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;
}
}
便利贴()
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();
}
});
}
}