在Netbeans GUI中覆盖paintComponent

时间:2010-06-15 15:18:14

标签: java swing netbeans java-2d

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

我已经在随机论坛上看到过这个问题了几次,而且人们没有回答这个问题,而是指向custom painting tutorial,这显然无济于事。

我首先尝试使用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.java的{​​{1}},它就会有效。如果您运行Netbeans GUI,它将无法正常工作。那么,如何将自定义组件添加到Netbeans生成的Swing GUI?

1 个答案:

答案 0 :(得分:1)

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

您可以通过设置尺寸来显示它,但由于它未在布局中考虑,因此最有可能与其他组件重叠。

要完成您想要的任务,您需要将panel的布局设置为其他内容,例如BorderLayout。例如:

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