在 Netbeans GUI 中覆盖 PaintComponent

发布于 2024-09-06 09:44:10 字数 4432 浏览 6 评论 0原文

我已将 JPanel 添加到 Netbeans 生成的 GUI 中,并添加一个 JPanel BoxThing 来覆盖 paintComponent 并绘制一个小红色框,但它不显示,paintComponent 甚至从未被调用。如果我实例化自己的 JFrame 并将包含 BoxThing 的 JPanel 放入其中,它就可以正常工作。

我在随机论坛上多次看到这个问题,人们没有回答这个问题,而是指向 自定义绘画教程,这显然没有帮助。

我先尝试使用 Netbeans 5.5,然后切换到 Netbeans 6.8,同样的问题。

Main.java

package MadProGUI9000;

public class Main extends javax.swing.JFrame {

    /** Creates new form Main */
    public Main() {
        initComponents();
        panel.add(new BoxThing());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        panel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 185, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel panel;
    // End of variables declaration

}

BoxThing.java

package MadProGUI9000;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * A component with a red box in the center.
 */
public class BoxThing extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        Dimension size = getSize();

        int rX = (size.width - 5)/2;
        int rY = (size.height - 5)/2;

        g.setColor(Color.RED);
        g2.fillRect(rX, rY, 5, 5);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("BoxThing demo");
                JPanel panel = new JPanel();
                frame.add(panel);
                panel.add(new BoxThing());
                frame.setVisible(true);
                panel.setPreferredSize(new Dimension(100, 100));
                frame.pack();
            }
        });
    }

}

如您所见,如果您只运行 BoxThing.javamain,它就可以工作。如果您运行 Netbeans GUI,它将无法工作。那么,如何将自定义组件添加到 Netbeans 生成的 Swing GUI 中?

I've added a JPanel to my Netbeans generated GUI, and add a JPanel BoxThing that overrides paintComponent and draws a small red box, but it doesn't display, paintComponent never even gets invoked. If I instantiate my own JFrame and put a JPanel containing a BoxThing in it, it works fine.

I've seen this question asked a few other times on random forums, and the people don't answer the question, instead they point to the custom painting tutorial, which obviously doesn't help.

I tried with Netbeans 5.5 first, then switched to Netbeans 6.8, same issue.

Main.java

package MadProGUI9000;

public class Main extends javax.swing.JFrame {

    /** Creates new form Main */
    public Main() {
        initComponents();
        panel.add(new BoxThing());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        panel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 185, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel panel;
    // End of variables declaration

}

BoxThing.java

package MadProGUI9000;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * A component with a red box in the center.
 */
public class BoxThing extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        Dimension size = getSize();

        int rX = (size.width - 5)/2;
        int rY = (size.height - 5)/2;

        g.setColor(Color.RED);
        g2.fillRect(rX, rY, 5, 5);
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("BoxThing demo");
                JPanel panel = new JPanel();
                frame.add(panel);
                panel.add(new BoxThing());
                frame.setVisible(true);
                panel.setPreferredSize(new Dimension(100, 100));
                frame.pack();
            }
        });
    }

}

As you can see, it works if you just run BoxThing.java's main. If you run the Netbeans GUI, it wont work. So, how can I add custom components to a Netbeans generated Swing GUI?

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

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

发布评论

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

评论(1

日暮斜阳 2024-09-13 09:44:10

这就是组布局的工作方式。它将屏幕空间划分为。在布局期间,它循环遍历组以确定每个组件的边界。当您将面板添加到容器时,它没有添加到任何组,因此从未给出尺寸或位置。因此它的大小为 (0,0) 并且从未被绘制。

您可以通过设置大小来使其显示,但由于布局中未考虑它,因此它很可能最终会与其他组件重叠。

要实现您想要的效果,您需要将 panel 的布局设置为其他内容,例如 BorderLayout。例如:

public Main() {
    initComponents();
    panel.setLayout(new BorderLayout());
    panel.add(new BoxThing(), Borderlayout.CENTER);
}

That's the way Group layout works. It divides the screen real estate up into Groups. During layout it cycles through the groups to determine the bounds for each component. When you added your panel to the container, it was not added to any group, and so was never given a size or location. As a result it has a size of (0,0) and is never painted.

You can make it appear by setting a size, but as it's not being considered in the layout, it will most likely wind up overlapping with other components.

To accomplish what you want, you need to set panel's layout to something else, like BorderLayout. For example:

public Main() {
    initComponents();
    panel.setLayout(new BorderLayout());
    panel.add(new BoxThing(), Borderlayout.CENTER);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文