私はあなたがボタンへのアクセス権を持っているボタンをクリックしたときに私は私のプログラムで
をボタンを押すと、ハーフサイズになるためのJFrameをアニメーション化します。次に、使用することができます:
SwingUtilities.windowForComponent(theButton);
フレームへの参照を取得します。
これで、TimerのActionListenerを作成するときに、WindowにActionListenerの引数として渡すことができます。
編集:MREによって
提案は、シンプルでまっすぐ進むと、多くの場合、使いやすい(そしておそらく、この場合は、よりよい解決策)。
私の提案はもう少し複雑ですが、SwingUtilitiesメソッドを紹介しています。これにより、作成するフレームやダイアログで使用される可能性のある再利用可能なコードを書くことができます。 winowは、特定の最小サイズに達したときに、タイマーを停止したいと思う。もちろん、
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationSSCCE extends JPanel
{
public AnimationSSCCE()
{
JButton button = new JButton("Start Animation");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
WindowAnimation wa = new WindowAnimation(
SwingUtilities.windowForComponent(button));
}
});
add(button);
}
class WindowAnimation implements ActionListener
{
private Window window;
private Timer timer;
public WindowAnimation(Window window)
{
this.window = window;
timer = new Timer(20, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e)
{
window.setSize(window.getWidth() - 5, window.getHeight() - 5);
// System.out.println(window.getBounds());
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("AnimationSSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new AnimationSSCCE());
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
:
簡単な例は次のようになります。私はそのコードをあなたに任せます。
'frame.getWidth()'を試してください。ここで 'frame'はあなたが参照しているJFrameです。 – fireshadow52