简体   繁体   English

Netbeans GUI预览与运行时视图不同

[英]Netbeans GUI preview is different from Runtime view

I'm developing a simple Java application using NetBeans and its GUI editor. 我正在使用NetBeans及其GUI编辑器开发一个简单的Java应用程序。 I'm stuck on creating a simple dialog: runtime it looks different from what I designed and from what is previewed in the editor. 我一直在创建一个简单的对话框:运行时它与我设计的内容和编辑器中预览的内容不同。 Basically, clicking on a button make my dialog appears. 基本上,单击按钮会显示我的对话框。

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
       PanelDialogNuovoCliente pan = new PanelDialogNuovoCliente();
       JDialog jd=new JDialog();
       jd.setTitle("Nuovo Cliente");
       jd.setMinimumSize(new Dimension(500, 400));
       jd.setLocationRelativeTo(null);
       jd.add(pan);
       jd.setModal(true);
       jd.setVisible(true);   
    }

The problem is that when the dialog appears it has a different look, the dialog window seems smaller and not all the components fit in it. 问题是当对话框出现时它具有不同的外观,对话框窗口看起来更小,而不是所有组件都适合它。

PanelDialogNuovoCliente is just a JPanel with some labels and JTextField . PanelDialogNuovoCliente只是一个带有一些标签和JTextFieldJPanel

Maximum, Minimum and Preferred sizes are all set to (500,400) from the JPanel properties in the editor. 最大,最小和首选大小都从编辑器中的JPanel属性设置为(500,400)。

JDialog minimum size is set to (500,400) from the code that I snipped. JDialog最小大小设置为(500,400)我剪切的代码。

Unfortunately I can't post a screenshot because I need at least 10 reputation but when I run the application Dialog's window is smaller compared to the one that I can see from the preview button in NetBean's GUI editor. 不幸的是我无法发布截图,因为我需要至少10个声誉,但是当我运行应用程序时,对话框的窗口比我从NetBean的GUI编辑器中的预览按钮看到的窗口要小。

EDIT: Here's the screenshot. 编辑:这是截图。 Runtime JDialog is on the left, while preview of it in netbeans is on the right. 运行时JDialog位于左侧,而netbeans中的预览位于右侧。 I tried to call JDialog#pack() just before setVisible(true) without success. 我试图在setVisible(true)之前调用JDialog#pack()但没有成功。 I set nimbus look and feel for my app. 我为我的应用设置了nimbus的外观和感觉。 Anyways if i try to preview the design from ide with nimbus l&f it looks perfect so i don't think that this one is the real problem 无论如何,如果我尝试使用nimbus l&f预览ide的设计,它看起来很完美,所以我不认为这个是真正的问题 在此输入图像描述

Perhaps what you're experiencing is similar to this post: 也许你所经历的与这篇文章类似:

NetBeans (Java Swing): Set window size NetBeans(Java Swing):设置窗口大小

I remember experiencing something similar and shared my experiences in a post there. 我记得经历过类似的事情并在那里分享我的经历。

Edit: (28/05/2015) 编辑:(28/05/2015)

Just to clarify/elaborate, here are steps I've got to replicate (and resolve) the issue I encountered, which might be what you've faced. 只是为了澄清/阐述,这里是我必须复制(并解决)我遇到的问题的步骤,这可能是你所面临的问题。

Problem Replication Steps 问题复制步骤

  1. Create a new project with " File >> New Project... " 使用“ File >> New Project ... ”创建一个新项目
  2. Choose " Java >> Java Desktop Application " 选择“ Java >> Java桌面应用程序
  3. Click ' Next ' button 单击“ 下一步 ”按钮
  4. Project Name: " TestApp ", then " Finish " button - You then get two tabs opening up in the Matisse editor, "TestView.java" (the app's main window) and "TestAboutBox.java". 项目名称:“ TestApp ”,然后“ 完成 ”按钮 - 然后在Matisse编辑器中打开两个选项卡,“TestView.java”(应用程序的主窗口)和“TestAboutBox.java”。
  5. In the Matisse editor, I re-size the window to be a large size . 在Matisse编辑器中,我将窗口重新调整为大尺寸
  6. I then press Ctrl+F5 to run it 然后按Ctrl + F5运行它
  7. It runs and the window is the same size as in the ide. 它运行,窗口大小与ide相同。
  8. Upon closing the app, it writes data to a " ~/.TestApp/mainFrame.session.xml " file on my linux system (I think this equates to " %APPDATA%\\CompanyName\\TestApp\\mainFrame.session.xml " on a windows system) 关闭应用程序后,它会将数据写入我的linux系统上的“ 〜/ .TestApp / mainFrame.session.xml ”文件(我认为这相当于“ %APPDATA%\\ CompanyName \\ TestApp \\ mainFrame.session.xml ” windows系统)
  9. Taking a look inside this " mainFrame.session.xml " file, I see there's a " mainFrame " node that contains the x , y , width and height of the window. 看一下这个“ mainFrame.session.xml ”文件,我看到有一个“ mainFrame ”节点,它包含窗口的xy宽度高度
  10. Back in the mattisse editor, I resize the window to be smaller. 回到mattisse编辑器中,我将窗口调整为更小。
  11. I then press Ctrl+F5 to run it again 然后我按Ctrl + F5再次运行它
  12. The app's window then appears at the larger size (ie, it didn't abide by the smaller size specified in the IDE) 应用程序的窗口然后以更大的尺寸显示(即,它不遵守IDE中指定的较小尺寸)

Workaround 解决方法

I tried the workaround suggested in Tomas Pavek's post here: 我尝试了Tomas Pavek在帖子中提出的解决方法:

http://forums.netbeans.org/ptopic28011.html http://forums.netbeans.org/ptopic28011.html

Basically, these steps: 基本上,这些步骤:

  1. Delete this " mainFrame.session.xml " file (or the folder that contains it) 删除此“ mainFrame.session.xml ”文件(或包含它的文件夹)
  2. then do CTRL+F5 to run the app again 然后按CTRL + F5再次运行应用程序

...and hey presto! ......嘿嘿! It appears at the correct size that the IDE had specified. 它显示为IDE指定的正确大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM