简体   繁体   English

在Netbeans GUI中重写paintComponent

[英]Override paintComponent in Netbeans 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. 我已经在我的Netbeans生成的GUI中添加了一个JPanel,并添加了一个覆盖paintComponent并绘制一个小红色框的JPanel BoxThing ,但是它不显示, paintComponent甚至都不会被调用。 If I instantiate my own JFrame and put a JPanel containing a BoxThing in it, it works fine. 如果我实例化我自己的JFrame并在其中包含一个BoxThing的JPanel,它工作正常。

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. 我先尝试使用Netbeans 5.5,然后又切换到Netbeans 6.8,同样的问题。

Main.java 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 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 . 如您所见,如果您仅运行BoxThing.javamain ,它将起作用。 If you run the Netbeans GUI, it wont work. 如果您运行Netbeans GUI,它将无法正常工作。 So, how can I add custom components to a Netbeans generated Swing GUI? 因此,如何将自定义组件添加到Netbeans生成的Swing GUI中?

That's the way Group layout works. 这就是组布局的工作方式。 It divides the screen real estate up into Groups . 它将屏幕房地产分成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. 结果,它的大小为(0,0),并且永远不会被绘制。

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 . 为了完成所需的操作,您需要将panel的布局设置为其他内容,例如BorderLayout For example: 例如:

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

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

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