简体   繁体   中英

Trouble with ActionListener and object positioning

The following code draws a rectangle whenever I click on the button, I want the rectangle to be drawn only once regardless of how many times the button is clicked. Also how can I position the Rectangle in the center of the frame and the button above it?

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

class rectangle{
    public static void main(String args[]){
        EventQueue.invokeLater(new Runnable(){
            public void run(){

                final JFrame frame=new JFrame("RECTANGLE");
                final JPanel panel=new JPanel();
                JButton button=new JButton("DRAW");
                panel.add(button);
                frame.add(panel);
                frame.setSize(400,400);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent event){

                        panel.add(new drawrectangle());


                    }
                });
            }
        });
     }
}

class drawrectangle extends JComponent{
    public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D) g;
        g2.setPaint(Color.BLACK);
        Rectangle2D rect=new Rectangle2D.Double(50,50,200,200);
        g2.draw(rect);
        g2.fill(rect);
    }

}

You can declare field

boolean firstClick = true;

Than write something like that:

public void actionPerformed(ActionEvent event){
                        if(firstClick){
                        panel.add(new drawrectangle()); }
                        firstClick = false;

}

To answer your first question, you can keep track of how many times the button has been pressed using a variable and incrementing it in actionPerformed() method. Then you will know how many times the button has been pressed and take action accordingly.

To answer your second question, the easier way to do it would be to use BorderLayout . Create two JPanel s add button to one panel and add it to NORTH and add the second panel to CENTRE . Then when you press the button you can add the rectangle to the panel in the CENTRE.

The difficult approach but more precise way of doing it is to manually place the button and rectangle by removing the layout manager ( panel.setLayout(null) ), and then specifying the size and location of all components. You also will then have to keep track of change in window state etc.

Still another way is to use GridBagLayout , which is a great balance between the first and second approach.

The following code draws a rectangle whenever I click on the button, I want the rectangle to be drawn only once regardless of how many times the button is clicked. Also how can I position the Rectangle in the center of the frame and the button above it?

  1. No the code you posted dont draw the rectangle at all when you click the button.
  2. Just count how often your button got clicked and if it is the first time you add your button to the JPanel.
  3. You can use BorderLayout to position the Rectangle in the center and the button above it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM