Java 如何在 Swing 组件调整大小时“做某事”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1088595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "do something" on Swing component resizing?
提问by
I've a class which extends JPanel
. I overwrote protected void paintComponent(Graphics g)
.
我有一个扩展JPanel
. 我覆盖了protected void paintComponent(Graphics g)
。
There is a variable which has to be recalculated when the panel's dimensions change. How do I do that in a proper way?
当面板的尺寸发生变化时,必须重新计算一个变量。我该如何以正确的方式做到这一点?
采纳答案by camickr
If I understand the question correctly then you should read the section from the Swing tutorial on How to Write a Component Listenerwhich shows you how to listen for a change in a components size.
如果我正确理解了这个问题,那么您应该阅读 Swing 教程中关于如何编写组件侦听器的部分,该部分向您展示了如何侦听组件大小的变化。
回答by jjnguy
If the calculation isn't time consuming, I would just re-calculate the value each time in paintComponent()
.
如果计算不耗时,我只会在paintComponent()
.
Otherwise, you can save a value that is the size of the component and check it against the new size in paintComponent
. If the size changed, then recalculate, otherwise don't.
否则,您可以保存一个作为组件大小的值,并根据 中的新大小进行检查paintComponent
。如果大小改变,则重新计算,否则不要。
private Dimension size;
protected void paintComponent(Graphics g){
if (!size.equals(getSize())){
size = getSize();
// recalculate value
}
}
Or, you can do the calculation on a resize event.
或者,您可以对调整大小事件进行计算。
private CompoentListener resizeListener = new ComponentAdapter(){
public void componentResized(ActionEvent e){
// recalculate value
}
};
//in the constructor add the line
addComponentListener(resizeListener);
回答by Adam Paynter
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 ComponentListenerand simply listen to itself for resize events.
我想你可以覆盖各种setSize
和resize
方法并在那里执行计算。但是,您可能找不到所有可以更改大小的地方。您可能希望让您的类实现ComponentListener并简单地监听自身的调整大小事件。
Warning:I am not a Swing expert.
警告:我不是 Swing 专家。
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) {}
}
回答by Charnia
Like Adam Paynter suggested, you can also add an inner class to your code, like this:
就像 Adam Paynter 建议的那样,您还可以在代码中添加一个内部类,如下所示:
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()
. 通过这种方式,您可以从内部类内部调用组件的任何方法,例如
e.getComponent().getWeight();
回答by guest
It resizes automatically if it's
如果是,它会自动调整大小
- inside a BorderLayout panel and
- put there as BorderLayout.CENTER component.
- 在 BorderLayout 面板内和
- 放在那里作为 BorderLayout.CENTER 组件。
If it doesn't work, you probably have forgotten one of these two.
如果它不起作用,您可能已经忘记了这两个中的一个。
回答by Alexander
The simplest way is to implement a ComponentListener
:
最简单的方法是实现一个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()
.
在这里,我使用了 aComponentAdapter
因为我只想覆盖componentResized()
.
回答by Jeremiah
Here's what I use (where CoordinatePlane is a JPanel):
这是我使用的(其中 CoordinatePlane 是 JPanel):
I'm not an expert
我不是专家
public CoordinatePlane() {
setBackground(Color.WHITE);
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
//YOUR CODE HERE
}
});
}