Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

java - How to Draw some shape on JPanel using ActionListener?

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();
       }
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

RootPane

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

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

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...