在使用 GUI 构建器创建的 JPanel 中添加方法

发布于 2024-09-03 13:01:49 字数 839 浏览 4 评论 0原文

我使用 netbeans GUI 生成器创建了 GUI。我想在按下 JButton 后添加一个对象(让我们尝试使用按钮)

我在 ActionListener 中编写的非常简单的代码并且有效:

button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            panel2.add(new JButton("X"));
            panel2.validate();
        }
    });

Howewer in Gui create by netbeans this does not work:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 jPanel1.add(new JButton("X"));
 jPanel1.validate();
 System.out.print("aa"); 
 }

但它打印“aa”,因此操作侦听器有效。

它看起来与此处显示的问题类似: http://bytes.com/topic/java/answers/857720- add-icon-jpanel

但我什至看不到 JosAH 写的那个矩形。

我会很感激任何建议。

I have GUI created using netbeans GUI builder. I want to add there an object (let's try with button) after pressing a JButton

The very simple code which I wrote in ActionListener and works:

button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            panel2.add(new JButton("X"));
            panel2.validate();
        }
    });

Howewer in Gui created by netbeans this doesnt work:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 jPanel1.add(new JButton("X"));
 jPanel1.validate();
 System.out.print("aa"); 
 }

But it prints "aa" so action listener works.

It looks like a similar problem to one showed here:
http://bytes.com/topic/java/answers/857720-adding-icon-jpanel

but I can't see even that rectangle about which JosAH wrote.

I'll appreciate any suggestion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2024-09-10 13:01:49

在编辑器中创建一个占位符面板,然后将面板添加到其中。我认为问题与布局等有关,因为某些布局需要在添加组件时提供一些布局数据(即设置 add(c, ...) 中的第二个字段)。因此,使用 GUI 创建工具创建一个占位符 JPanel,为其命名以便您可以引用它,然后将该组件添加到该面板。给它一个布局,例如不需要布局数据的 FlowLayout。您可能还想删除面板上的所有插图,这样就不会出现过大的间距,您可以在 UI 编辑器中执行此操作。

Create a placeholder panel in your editor, and then add the panel to that. I think the problem is to do with layouts and such, since some layouts require some layout data to be present on adding the component (i.e., the second field in add(c, ...) is set). So, create a placeholder JPanel using the GUI creator tool, name it so you have a reference to it, and then add the component to that panel. Give it a layout such as FlowLayout which requires no layout data. You may also want to remove all the insets on the panel so you don't get massive spacing, which you can probably do in the UI editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文