如何使用ActionListener在JPanel上绘制形状? [英] How to Draw some shape on JPanel using ActionListener?

查看:126
本文介绍了如何使用ActionListener在JPanel上绘制形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过单击Jbutton在JPanel上绘制形状,但是我不能.我上网已经五个小时了,但是我找不到办法. 这就是我要执行的操作:如果单击矩形"按钮,则按钮下方会出现一个矩形,如果单击圆形"按钮,则会出现一个圆圈.

I'm practising to draw a shape on a JPanel by clicking on a Jbutton, but I cannot. It's been five hours that I'm surfing the web, but I cannot find the way to do it. This is what I want to do: if I click on "Rectangle" button a rectangle appears under the buttons and if I click on "Circle" button a circle appears.

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

   class Shape extends JFrame {
       JButton rec, circle;
       static String botSelected;
       Shape (){
           frameSet ();
       }

       void frameSet(){
           JFrame frame = new JFrame();
           frame.setVisible(true);
           frame.setSize(600,300);
           rec = new JButton ("Rectangle");
           circle = new JButton("Circle");
           JPanel panel = new JPanel();
           frame.add(panel);
           panel.add(rec);
           panel.add(circle);
           Click clk = new Click();
           rec.addActionListener(clk);
           circle.addActionListener(clk);
       }

       public void paint (Graphics g){
           super.paint(g);
           if (botSelected.equals("Rectangle"))
               g.fillRect(50,50,50,50);
           else if (botSelected.equals("Circle"))
               g.fillOval(50,50,50,50);
       }

       public static void main (String [] arg){
           Shape s = new Shape();
       }
   }

   class Click implements ActionListener{
       public void actionPerformed (ActionEvent e){
           Shape.botSelected = e.getActionCommand();
       }
   }

推荐答案

我要做的第一件事是通读执行自定义绘画,以更好地了解绘画过程的工作原理.

The first thing I would do is have a read through Painting in Swing and Performing custom painting to better understand how the painting process works.

接下来,您需要了解JFrame是绘画的一个不好的选择.为什么?因为它是多层的.

Next you need to understand that JFrame is a bad choice for painting to. Why? Because it's multilayered.

JFrame包含一个JRootPane,其中包含一个JLayeredPanecontentPaneglassPaneJMenuBar,在您的示例中,它还包含一个JPanel.

A JFrame contains a JRootPane, which contains a JLayeredPane the contentPane, glassPane and the JMenuBar and in your example, it also contains a JPanel.

除了glassPane的(默认)例外,所有这些组件都是不透明的.

With the (default) exception of the glassPane, all these components are opaque.

虽然可以在paint方法中显示某些东西来显示它,但是如果其他任何组件自己绘制,则将其擦拭干净-这是因为Swing组件实际上可以彼此独立地绘制,并且先让父母自己绘画.

While it's possible to have something drawn in the paint method show it, if any of the other components paint themselves, it will be wiped clean - this is because Swing components can actually be painted independently of each other, with having to have the parent paint itself first.

更好的解决方案是从JPanel扩展并覆盖其paintComponent方法.

A better solution is to start by extending from JPanel and override its paintComponent method.

为简单起见,我也鼓励您也针对此类实现ActionListener,它将允许actionPerformed方法访问组件的属性,并且在您的情况下,调用repaint可以当您要更新用户界面时,触发绘画循环.

For simplicity, I'd also encourage you to implement the ActionListener against this class as well, it will allow the actionPerformed method to access the properties of the component and, in your case, call repaint to trigger a paint cycle when you want to update the UI.

这篇关于如何使用ActionListener在JPanel上绘制形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆