Results 1 to 3 of 3
  1. #1
    lazybud is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default New to Java, I get an ActionEvent when I change the JFrame size

    Hi, I'm new to Java (one week now) and I can't find the solution to my problem. It's possible that I'm missing something obvious and simple, I'm sorry if that is the case.

    I wanted to make a program that would change the color of a circle when I click a button. The problem is that the circle also changes color when I manually change the size of the frame with my mouse. For some reason it trigers an ActionEvent, and i don't know why. Although the program does what I want it to do it bothers me that this happens and I would like to know why and how to solve it. Any help would be apreciated. This is also my first thread, so if I'm doing anything wrong please tell me. English is nos my first language.

    My code is the following:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MyFirstGui implements ActionListener {
        JFrame frame;
        
        public static void main(String[] args){
            MyFirstGui gui=new MyFirstGui();
            gui.go();
        }
        
        public void go(){
            frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JButton button=new JButton("Change color");
            button.addActionListener(this);
            
            MyDrawPanel drawPanel=new MyDrawPanel();
            
            frame.getContentPane().add(BorderLayout.SOUTH,button);
            frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
            frame.setSize(300,300);
            frame.setVisible(true);        
        }
        
        public void actionPerformed(ActionEvent event){
            frame.repaint();
        }
    }
    and

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MyDrawPanel extends JPanel {
        public void paintComponent(Graphics g){
            g.fillRect(0,0,this.getWidth(),this.getHeight());
            int red=(int)(Math.random()*255);
            int green=(int)(Math.random()*255);
            int blue=(int)(Math.random()*255);
            Color randomColor = new Color(red,green,blue);
            g.setColor(randomColor);
            g.fillOval(70, 70, 100, 100);
        }
    }

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: New to Java, I get an ActionEvent when I change the JFrame size

    When you change the size of your frame, the frame must be drawn again. It is not the action listener. Just add a println line to see it yourself.

    This color change should also happen if your window was overlapped by some other window ....

    With kind regards,

    Konrad

  3. #3
    lazybud is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: New to Java, I get an ActionEvent when I change the JFrame size

    Thanks for the reply, I didn't know that changing size repainted the frame. I managed to make the program do what i wanted it to.

Similar Threads

  1. Replies: 0
    Last Post: 02-20-2014, 10:44 PM
  2. Replies: 1
    Last Post: 03-05-2012, 10:44 AM
  3. how can I change font size?
    By rippon in forum AWT / Swing
    Replies: 6
    Last Post: 11-10-2011, 02:42 PM
  4. change size
    By lucasmcd in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 12:07 AM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 03:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •