Java在WindowBuilder构建的GUI上添加组件

Java在WindowBuilder构建的GUI上添加组件,java,swing,user-interface,windowbuilder,Java,Swing,User Interface,Windowbuilder,我在Eclipse中使用WindowBuilder构建了一个JavaSwingGUI。但是当我尝试使用.add()和.revalidate()添加新组件时,什么都没有发生 如果有人能帮我解决这个问题,我真的很感激 package Frame; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; impor

我在Eclipse中使用WindowBuilder构建了一个JavaSwingGUI。但是当我尝试使用.add()和.revalidate()添加新组件时,什么都没有发生

如果有人能帮我解决这个问题,我真的很感激

package Frame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

public class TestFrame {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame window = new TestFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JButton btnSampleButton = new JButton("Sample Button");
        btnSampleButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.add(new JButton("BTN"));
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.getContentPane().setLayout(null);
        btnSampleButton.setBounds(110, 126, 185, 112);
        frame.getContentPane().add(btnSampleButton);
    }
}
请尝试以下操作:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFrame {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TestFrame window = new TestFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JButton btnSampleButton = new JButton("Sample Button");
        btnSampleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JButton btn = new JButton("BTN");
                btn.setSize(btn.getPreferredSize());
                btn.setLocation(new Point(1, 1));
                frame.add(btn);
                frame.revalidate();
                frame.repaint();
            }
        });
        frame.getContentPane().setLayout(null);
        btnSampleButton.setBounds(110, 126, 185, 112);
        frame.getContentPane().add(btnSampleButton);
    }
}

努力学习。当您使用适当的布局管理器时,您不应该设置组件的大小/位置。

请以的形式发布代码,这样我们就可以看到问题所在。好的,发布了我的代码1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 在GUI可见后添加组件需要一些技巧。与其学这些把戏,不如用。。。。如中所示。这样,新组件的大小用于计算GUI的首选大小。在其中一张卡中,使用空白面板。另一个是按钮。