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

时间:2017-02-06 15:02:37

标签: java swing user-interface windowbuilder

我使用Eclipse中的WindowBuilder构建了一个Java-Swing GUI。但是当我尝试使用.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);
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

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);
    }
}

尝试学习Layout Managers。使用适当的布局管理器时,不应设置组件的大小/位置。