【问题标题】:WindowsBuilder: How to draw a component when press a button?WindowBuilder:按下按钮时如何绘制组件?
【发布时间】:2013-03-03 06:33:36
【问题描述】:

我希望在按下按钮时绘制一个矩形,但它似乎不起作用。 这是我的代码:

package draw.rect;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DR extends JFrame {

    private JPanel contentPane;

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

    /**
     * Create the frame.
     */
    public DR() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 458, 312);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnDrawRrect = new JButton("Draw Rrect");
        btnDrawRrect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.print("hello");
                RectangleComponent r2 = new RectangleComponent();
                contentPane.add(r2);
                r2.revalidate();
                contentPane.revalidate();
            }
        });
        btnDrawRrect.setBounds(10, 11, 89, 23);
        contentPane.add(btnDrawRrect);    
    }
}

package draw.rect;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;

public class RectangleComponent extends JComponent {

      Rectangle rect;

      public RectangleComponent()
      {
        rect  = new Rectangle(50, 50, 120, 130);
      }

      public void paintComponent(Graphics g)
      {
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(rect);
      } 
}

问题在于,当调用动作事件监听器时,JComponent 已正确构造,但并未添加到框架中。 提前致谢

【问题讨论】:

    标签: java swing layout draw paintcomponent


    【解决方案1】:

    您正在使用空布局,并且在这样做时您完全负责设置使用容器添加到空布局的任何组件的大小和位置。您忽略了这个责任并且没有给您添加的 JComponent 一个大小或位置,因此当它被添加到容器中时,由于它的大小为 0,所以无法看到它。为了向您自己证明这一点,添加一个 System.out。 println() 打印出添加的组件是否可见以及它的边界是什么,您将自己看到。您的第二个问题是您在添加组件后没有调用repaint(),尽管这并不总是必要的,也不是您当前问题的原因。一个快速的解决方法是设置新添加的组件的边界,但我不建议你这样做。相反,我建议:

    • 我强烈建议您不要使用 null 布局,因为它们会使您的代码不灵活并且非常难以维护和升级。
    • 阅读教程并使用布局管理器。
    • 从容器中移除或添加组件后,在容器上调用revalidate(),然后调用repaint()

    【讨论】:

    • 我实际上是在使用 WindowsBuilder Pro 来构建我的 JFrame,所以这就是布局设置为 null 的原因。这就是没有绘制矩形的问题吗?
    • @user2109278:同样,矩形没有显示,因为包含它的组件的大小为 0,0。请注意,WindowsBuilderPro 并不强制您使用 null 布局,您仍然可以通过此工具使用所有布局管理器,尽管我建议您在充分理解 Swing 之前不要使用代码生成库。
    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多