- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将尺寸传递给 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
,原因是
要出现在屏幕上,每个 GUI 组件都必须是包含层次结构的一部分。包含层次结构是一个以顶级容器为根的组件树。我们稍后会向您展示一个。
每个 GUI 组件只能包含一次。如果一个组件已经在一个容器中,并且您尝试将其添加到另一个容器中,则该组件将从第一个容器中删除,然后添加到第二个容器中。
每个顶级容器都有一个内容 Pane ,一般来说,它包含(直接或间接)该顶级容器的 GUI 中的可见组件。
<您可以选择将菜单栏添加到顶级容器。按照惯例,菜单栏位于顶级容器内,但位于内容 Pane 之外。某些外观(例如 Mac OS 外观)允许您选择将菜单栏放置在更适合外观的其他位置(例如屏幕顶部)。
因此结论是,更改contentpane
大小将会更改frame
大小。
正如您的语句 frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));
正在使 panel
为contentpane
,所以再次更改其大小就是更改frame
大小
但反过来是:
frame.getContentPane().add(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));
添加
一个容器
到jframe
,然后向其中添加
您的组件 容器
(在你的情况下为 jpanel )
希望这会有所帮助,还有一件事,我也曾经像您一样为每个小目的创建方法
和类
进行编码,但有时它们会提高复杂性而不是简单性,所以我给你的建议是遵循设计模式,例如 MVC
或任何你喜欢的
关于java - 如何将尺寸传递给 Swing JPanel 创建者类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11731541/
Android 中的 Activity Creator 是什么? 最佳答案 activityCreator 是用于创建 Android 项目的批处理文件和 shell 脚本。这早已被 android
我正在使用最新的 Qt SDK (2.4.0) 并且在让 Visual Studio 2008 CDB 正常工作时遇到问题。我遇到了以下两个我认为相关的问题 我有一个失败的断言。当该断言失败时,我会看
源代码 索引.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; impo
我使用 Wifimanager addNetwork() 添加三个配置,然后我想删除这些。 如何获取我的应用创建的配置。 最佳答案 如果我没看错,您需要从您的应用中删除这些添加的 WiFi。 使用 W
我一定是遗漏了一个带有标题和包含的基本概念,因为当我尝试从单独的源文件中调用最简单的函数时,我得到一个错误: main.obj:-1: error: LNK2019: unresolved exter
我想用我用 Qt creator 创建的 Qt 应用程序部署一个崩溃处理程序。经过一番摸索,谷歌的 Breakpd 似乎会完全按照我的需要做,但是关于这方面的教程很简单。因此,我有许多问题希望能得到解
我有一个 Action 创建器,我应该如何用 jest 测试它?问题是,我什至无法进入 Api 函数,这就是为什么我无法获得 LOGIN_SUCCESS,当我检查 store.getState() 时
我有两个计数器:COUNTER_1 和 COUNTER_2。所以我创建了多个 reducer 来处理它们。 export default (state = 0, action) => { switch
如何检查在 Emacs 中创建缓冲区的人的身份,然后再检查是否是同一用户访问该缓冲区?我的意思是“我是谁?”在 Unix 命令中.. 然后检查同一用户是否正在访问该文档? --> 我想要在我自己的代码
如果我有一个包含 5 行和 4 列的表。但我必须在下一行添加值。没有与数据库相关的依赖项。我有 ADD 按钮,我想像每次单击时一样对其进行编码ADD 按钮有新行的添加......我的意思是表中行的创建
我正在尝试制作一个简单的对话框来显示我的名字。看代码。 Pessoa *p = new Pessoa("Ronald Araújo", "ronald.araujo@live.com", 23); Q
我有一些模型,它们之间有 has_many 和 belongs_to 关联,如下所示: class Customer Customer instance end end end 也就是说
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Getting / setting file owner in C# 我搜索了 Internet 但没有找到
我在测试一个 Action 创建者时遇到麻烦,该 Action 创建者只是循环遍历传递给它的数组,并为该数组中的每个项目分配一个 Action 。这很简单,我似乎无法弄清楚。这是 Action 创建者
我使用 redux 和 redux-thunk 来执行异步操作。 在我的组件中,我有以下代码 class Counter extends Component { componentDidMou
假设我有一个 REST API 来编辑用户配置文件。这里涉及到两种方法 一种更新用户个人资料的方法; 获取当前配置文件的完成分数的方法。 当然,当用户更新他的个人资料时,我想获取完成分数。 这是我(天
我有一个 react 组件,我想在 redux 中设置一些状态,当设置状态时,我想在组件中做一些其他的事情。 // Inside Component onButtnClick() {
我有一个带有 Redux 和 Jest 的 create-react-app 用于测试。我正在使用 fetch 从 Action 创建者发出 API 请求。在编写测试时,我发现无论出于何种原因——无论
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
所以我有一个在 QT Creator 中设计的 GUI。 因为我必须添加一个 createWindowContainer 小部件,所以我必须通过 C++ 将一个小部件添加到我的代码中。我正在添加一个简
我是一名优秀的程序员,十分优秀!