How to “do something” on Swing component resizing?

后端 未结 8 1120
遇见更好的自我
遇见更好的自我 2021-01-17 07:53

I\'ve a class which extends JPanel. I overwrote protected void paintComponent(Graphics g).

There is a variable which has to be recalculate

相关标签:
8条回答
  • 2021-01-17 08:04

    If I understand the question correctly then you should read the section from the Swing tutorial on How to Write a Component Listener which shows you how to listen for a change in a components size.

    0 讨论(0)
  • 2021-01-17 08:12

    Here's what I use (where CoordinatePlane is a JPanel):

    I'm not an expert

    public CoordinatePlane() {
        setBackground(Color.WHITE);
        this.addComponentListener(new ComponentAdapter(){
            public void componentResized(ComponentEvent e) {
                //YOUR CODE HERE         
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-17 08:19

    The simplest way is to implement a ComponentListener:

    myjpanel.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            //recalculate variable
        }
    });
    

    Here, I have used a ComponentAdapter because I only intend on overriding componentResized().

    0 讨论(0)
  • 2021-01-17 08:21

    Like Adam Paynter suggested, you can also add an inner class to your code, like this:

    class ResizeListener extends ComponentAdapter {
            public void componentResized(ComponentEvent e) {
                // Recalculate the variable you mentioned
            }
    }
    

    The code you have entered between the innermost brackets will be executed everytime the component get resized.

    Then you add this listener to your component with

    myJPanel.addComponentListener(new ResizeListener());
    

    You can get your component by using e.getComponent(). This way you can call any method of your component from inside the inner class like

    e.getComponent().getWeight();
    
    0 讨论(0)
  • 2021-01-17 08:21

    I suppose you could override the various setSize and resize methods and perform the calculation there. However, you may not find all the places where the size can be changed. You may want to have your class implement ComponentListener and simply listen to itself for resize events.

    Warning: I am not a Swing expert.

    Warning: I have not compiled this code.

    public class MyJPanel extends JPanel implements ComponentListener {
    
        public MyJPanel() {
            this.addComponentListener(this);
        }
    
        public void paintComponent(Graphics g) {
            // Paint, paint, paint...
        }
    
        public void componentResized(ComponentEvent e) {
            // Perform calculation here
        }
    
        public void componentHidden(ComponentEvent e) {}
    
        public void componentMoved(ComponentEvent e) {}
    
        public void componentShown(ComponentEvent e) {}
    
    }
    
    0 讨论(0)
  • 2021-01-17 08:21
    This simple example is drawing a red circle in the resized frame....
    
    import java.awt.*;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class RedCircle extends JFrame implements ComponentListener {
         int getWidth;
         int getHeight;
         
          public RedCircle() {
            super("Red Circle");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addComponentListener(this);             
            pack();
            setVisible(true);
        }
        
          public void componentResized(ComponentEvent e) {
                getWidth = e.getComponent().getWidth();
                getHeight = e.getComponent().getHeight();
                
                Panel pane = new Panel(getWidth,getHeight);
                
                add(pane);
          }
        
       
        public static void main(String[] args) {
            
             RedCircle rc = new RedCircle();
        }
        
    
     
     public void componentMoved(ComponentEvent e) {
           
        }
    
        
        public void componentShown(ComponentEvent e) {
         
        }
    
        
        public void componentHidden(ComponentEvent e) {
        
        }
    
    
    }
    
    
    class Panel extends JPanel {
        int panelWidth;
        int panelHeight;
        
        public Panel(Integer getWidth,Integer getHeight) {
           
        panelWidth = getWidth;
        panelHeight = getHeight;
        
        }
        
    public void paintComponent(Graphics comp) {
            super.paintComponent(comp);
            Graphics2D comp2D = (Graphics2D) comp;
           
            int realWidth = panelWidth - 17;
            int realHeight = panelHeight - 40;
           
            float Height = (realHeight);
            float Width = (realWidth);
            
        // draw the Red Circle
            comp2D.setColor(Color.red);
            Ellipse2D.Float redCircle = new Ellipse2D.Float(0F, 0F, Width, Height);
            comp2D.fill(redCircle);
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题