如何在Swing组件大小调整上“做点什么”?

时间:2009-07-06 18:41:15

标签: java swing

我有一个扩展JPanel的课程。我覆盖了protected void paintComponent(Graphics g)

当面板的尺寸发生变化时,有一个必须重新计算的变量。我该如何以正确的方式做到这一点?

8 个答案:

答案 0 :(得分:27)

就像Adam Paynter建议的那样,您也可以在代码中添加内部类,如下所示:

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

每次调整组件大小时,都会在最里面的括号之间输入代码。

然后使用

将此侦听器添加到组件中
myJPanel.addComponentListener(new ResizeListener());

您可以使用e.getComponent()获取您的组件。这样,您可以从内部类中调用组件的任何方法,如

e.getComponent().getWeight();

答案 1 :(得分:17)

我想您可以覆盖各种setSizeresize方法并在那里执行计算。但是,您可能找不到可以更改大小的所有位置。您可能希望让您的类实现ComponentListener并只是自己调整resize事件。

警告:我不是Swing专家。

警告:我还没有编译此代码。

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) {}

}

答案 2 :(得分:13)

如果我正确理解了这个问题,那么您应该阅读How to Write a Component Listener上的Swing教程中的部分,其中介绍了如何监听组件大小的变化。

答案 3 :(得分:4)

如果计算不耗时,我只会在paintComponent()每次重新计算一次。

否则,您可以保存一个与组件大小相同的值,并根据paintComponent中的新大小进行检查。如果尺寸发生变化,则重新计算,否则不重新计算。

private Dimension size;

protected void paintComponent(Graphics g){
    if (!size.equals(getSize())){
        size = getSize();
        // recalculate value
    }
}

或者,您可以对resize事件进行计算。

private CompoentListener resizeListener = new ComponentAdapter(){
    public void componentResized(ActionEvent e){
        // recalculate value
    }
};

//in the constructor add the line
addComponentListener(resizeListener);

答案 4 :(得分:2)

最简单的方法是实施ComponentListener

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

在这里,我使用了ComponentAdapter,因为我只打算覆盖componentResized()

答案 5 :(得分:0)

如果是

,它会自动调整大小
  1. 在BorderLayout面板中,
  2. 将其放在BorderLayout.CENTER组件中。
  3. 如果它不起作用,你可能已经忘记了这两个中的一个。

答案 6 :(得分:0)

这是我使用的(CoordinatePlane是JPanel的地方):

我不是专家

public CoordinatePlane() {
    setBackground(Color.WHITE);
    this.addComponentListener(new ComponentAdapter(){
        public void componentResized(ComponentEvent e) {
            //YOUR CODE HERE         
        }
    });
}

答案 7 :(得分:0)

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);
    }
}

相关问题