gpt4 book ai didi

java - 如何将尺寸传递给 Swing JPanel 创建者类?

转载 作者:行者123 更新时间:2023-11-30 04:43:16 25 4
gpt4 key购买 nike

我正在尝试将尺寸传递给 Java Swing JPanel 创建者类。到目前为止还没有运气。我什至将 contentPane.setPreferredSize(new Dimension(intWidth, intHeight)); 放入 try/catch block 中,但没有得到任何系统输出。我传递了一个相当大的值,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();
}
});
}
}

最佳答案

您的代码存在一些问题:

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

当您为宽度高度传递不同值时,您将看到框架的整个尺寸 将会生效,因为我没有在任何地方指定 setSize ,原因是

  1. 要出现在屏幕上,每个 GUI 组件都必须是包含层次结构的一部分。包含层次结构是一个以顶级容器为根的组件树。我们稍后会向您展示一个。

  2. 每个 GUI 组件只能包含一次。如果一个组件已经在一个容器中,并且您尝试将其添加到另一个容器中,则该组件将从第一个容器中删除,然后添加到第二个容器中。

  3. 每个顶级容器都有一个内容 Pane ,一般来说,它包含(直接或间接)该顶级容器的 GUI 中的可见组件。

    <
  4. 您可以选择将菜单栏添加到顶级容器。按照惯例,菜单栏位于顶级容器内,但位于内容 Pane 之外。某些外观(例如 Mac OS 外观)允许您选择将菜单栏放置在更适合外观的其他位置(例如屏幕顶部)。

因此结论是,更改contentpane大小将会更改frame大小。

正如您的语句 frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255)); 正在使 panelcontentpane,所以再次更改其大小就是更改frame 大小

但反过来是:

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

添加一个容器jframe,然后向其中添加您的组件 容器(在你的情况下为 jpanel )

希望这会有所帮助,还有一件事,我也曾经像您一样为每个小目的创建方法进行编码,但有时它们会提高复杂性而不是简单性,所以我给你的建议是遵循设计模式,例如 MVC 或任何你喜欢的

关于java - 如何将尺寸传递给 Swing JPanel 创建者类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com