爪哇。通过单击按钮绘制形状的问题

发布于 2024-11-07 01:38:58 字数 1555 浏览 4 评论 0原文

再会。

我开发的程序在用户单击按钮时必须显示一些形状。至少它没有表现出来。怎么了? 代码是:

public class ShowFrame extends JFrame
{
    public ShowFrame()
    {
        this.setTitle("Show data");                                             //Title
        this.setSize( DEF_WIDTH, DEF_HEIGHT );                                  //Size of frame
        this.setResizable(false); 

    //...                                     

    JButton testButton = new JButton("Test");
    buttonPanel.add(testButton);
    this.add(buttonPanel, BorderLayout.SOUTH);


    testButton.addActionListener( new ActionListener() {                    //Add listener
        public void actionPerformed(ActionEvent e) {              
            DrawStuff stuff = new DrawStuff();                              //Create class which draws shapes
            add(stuff, BorderLayout.CENTER);
            System.out.println("Test Button");
        }
    } );
   }

public static final int DEF_WIDTH  = 600;                                   
public static final int DEF_HEIGHT = 400;                                   

private JPanel buttonPanel = new JPanel();
}

绘制形状的类:

public class DrawStuff extends JComponent
{
    public void paintComponent( Graphics g )
    {
        Graphics2D g2 = (Graphics2D) g;
        //...
        Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
        Line2D line      = new Line2D.Double(leftX, topY, 0, 0);
        //...
        g2.draw(rect);
        g2.draw(line);
        //...
    }

}

Good day.

I develop program which must show few shapes when user clicks the button. At least it doesn't show it. What is wrong?
Code is:

public class ShowFrame extends JFrame
{
    public ShowFrame()
    {
        this.setTitle("Show data");                                             //Title
        this.setSize( DEF_WIDTH, DEF_HEIGHT );                                  //Size of frame
        this.setResizable(false); 

    //...                                     

    JButton testButton = new JButton("Test");
    buttonPanel.add(testButton);
    this.add(buttonPanel, BorderLayout.SOUTH);


    testButton.addActionListener( new ActionListener() {                    //Add listener
        public void actionPerformed(ActionEvent e) {              
            DrawStuff stuff = new DrawStuff();                              //Create class which draws shapes
            add(stuff, BorderLayout.CENTER);
            System.out.println("Test Button");
        }
    } );
   }

public static final int DEF_WIDTH  = 600;                                   
public static final int DEF_HEIGHT = 400;                                   

private JPanel buttonPanel = new JPanel();
}

Class which draws shapes:

public class DrawStuff extends JComponent
{
    public void paintComponent( Graphics g )
    {
        Graphics2D g2 = (Graphics2D) g;
        //...
        Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
        Line2D line      = new Line2D.Double(leftX, topY, 0, 0);
        //...
        g2.draw(rect);
        g2.draw(line);
        //...
    }

}

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

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

发布评论

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

评论(1

短叹 2024-11-14 01:38:58

当您在可见 GUI 上添加/删除组件时,代码应该是:

panel.add(...);
panel.revalidate();
panel.repaint();

每次单击按钮时添加新面板的设计并不是一个很好的设计。

相反,您应该创建一个自定义绘画面板并重写 PaintComponent() 方法。然后,当您单击按钮时,您会调用自定义组件中的方法来设置要绘制的形状。 PaintComponent() 方法应该足够智能来绘制形状。然后在面板上调用 repaint()。

有关详细信息,请阅读 Swing 教程中关于 自定义绘制 的部分和工作示例。

When you add/remove components on a visible GUI the code should be:

panel.add(...);
panel.revalidate();
panel.repaint();

Your design of adding a new panel every time you click a button is not a very good one.

Instead you should create a custom painting panel and override the paintComponent() method. Then when you click a button you invoke a method in your custom component to set the shape you want to draw. The paintComponent() method should be smart enought to paint the shape. Then you invoke repaint() on the panel.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文