2

私のアプリケーションでは、 をクリックしたときにメソッドが実行される回数は、 のJButtonサイズを変更する回数に関連しているようJFrameです。以下の例では、アプリケーションを起動してサイズをまったく変更しないと、メソッドaddPanel(Dimension dim)が 2 回実行されます。一度リサイズしたら、さらに一気に。これはどのように説明できますか?ResizeControllerこれに影響はありますか?(アウトコメントされた行は、他の 2 つの「画面」に関するものであり、この場合は問題ではありません)。

Start.java

public class Start {

    public static void main(String[] arg){

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GroundFrame(Toolkit.getDefaultToolkit().getScreenSize());
            }
        });

    }
}

GroundFrame.java

public class GroundFrame extends JFrame{

    public static final long    serialVersionUID = 55;
    private Container           contentPane;
    public static Flyer         screenFlyer;
    public static Points        screenPoints;
    public static Sponsoren     screenSponsoren;
    public static JFrame        parent;

    public GroundFrame(Dimension screenDimension){
        createAndShowGUI(screenDimension);
        setVisible(true);
    }

    private void createAndShowGUI(Dimension screenDimension){
        setExtendedState(JFrame.MAXIMIZED_BOTH); 

        parent = this;

        contentPane = getContentPane();
        contentPane.setLayout(null);

        //screenFlyer = new Flyer(screenDimension);
        //screenSponsoren = new Sponsoren(screenDimension);
        //screenSponsoren.setVisible(false);
        screenPoints = new Points(screenDimension);
        //screenPoints.setVisible(false);

        ResizeController controller = new ResizeController();
        controller.start();

        //contentPane.add(screenFlyer);
        //contentPane.add(screenSponsoren);
        contentPane.add(screenPoints);
    }

    public static void exit(){
        System.exit(0);
    }
}

Points.java

public class Points extends JPanel{

    public static final long            serialVersionUID = 55;

    private JButton                     buttonNext, buttonExit, buttonAdd;
    private JLabel                      copyright;

    private Vector<PlayerPointsPanel>   panels = new Vector<PlayerPointsPanel>();
    final Color                         BACKGROUND_COLOR = new Color(60,60,60);                         
    private int                         pressed = 0;


    public Points(Dimension screenDimension){
        setLayout(null);
        setBackground(BACKGROUND_COLOR);
        setPreferredSize(screenDimension);
        initialize(screenDimension);
        setupElements(screenDimension);
    }

    private void initialize(Dimension dim){
        copyright = new JLabel("\u00a9 by Valentino Rugolo (2012)", SwingConstants.CENTER);
        buttonAdd = new JButton("+");
        buttonNext = new JButton("");
        buttonExit = new JButton("X");
        /*for(int i=0; i<8; i++){
            PlayerPointsPanel panel = new PlayerPointsPanel(dim, i);
            add(panel);
            panels.add(panel);
        }*/
    }

    private void setupElements(Dimension dim){
        setBounds(0,0,dim.width, dim.height);
        setLayout(null);
        dimensionCP = dim;

        copyright.setBounds((int)(dim.width*0.82), (int)(dim.height*0.9), (int)(dim.width*0.15), (int)(dim.height*0.05));
        copyright.setFont(new Font("Times New Roman", Font.PLAIN, (int)(dim.height*0.017)));
        copyright.setForeground(Color.white);

        buttonNext.setBounds((int)(dim.width*0.01), (int)(dim.height*0.9), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonNext.setBackground(Color.DARK_GRAY);
        buttonNext.setForeground(Color.white);
        buttonNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GroundFrame.screenPoints.setVisible(false);
                GroundFrame.screenFlyer.setVisible(true);
            }
        });

        buttonExit.setBackground(Color.DARK_GRAY);
        buttonExit.setForeground(Color.white);
        buttonExit.setBounds((int)(dim.width*0.95), (int)(dim.height*0.01), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GroundFrame.exit();
            }
        });

        buttonAdd.setBackground(Color.DARK_GRAY);
        buttonAdd.setForeground(Color.white);
        buttonAdd.setBounds((int)(dim.width*0.01), (int)(dim.height*0.01), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg1) {
                System.out.println("pressed " + pressed);
                pressed++;  
            }
        });

        add(buttonExit);
        add(buttonNext);
        add(copyright);
        add(buttonAdd);
    }

    public void resizeContent(Dimension dim){
        /*for(int i=0; i<panels.size(); i++){
            panels.elementAt(i).resizeContent(dim, i);
        }*/
        setupElements(dim);
    }
}

ResizeController.java

public class ResizeController extends Thread{

    public int sizebeforeX = GroundFrame.parent.getWidth();
    public int sizebeforeY = GroundFrame.parent.getHeight();

    public ResizeController(){

    }

    public void run(){
        while(true){

            int actualX = GroundFrame.parent.getWidth();
            int actualY = GroundFrame.parent.getHeight();

            if(sizebeforeX != actualX || sizebeforeY != actualY){
                //GroundFrame.screenFlyer.resizeContent(GroundFrame.parent.getSize());
                GroundFrame.screenPoints.resizeContent(GroundFrame.parent.getSize());
                //GroundFrame.screenSponsoren.resizeContent(GroundFrame.parent.getSize());
                sizebeforeX = actualX;
                sizebeforeY = actualY;
            }

            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
4

2 に答える 2

4

フレームのコンテンツのサイズを変更するときはいつでも、ボタンにリスナーを追加するsetupElements()を何度も呼び出します。フレームのinitialize()メソッドでリスナーを追加するコードを移動するだけです。

于 2012-11-09T08:09:00.680 に答える
4
  1. (これが理由です..)使用するComponentListener

  2. 最も安全な方法は実装ですComponentListener

  3. そのメソッドcomponentResized(ComponentEvent e)をオーバーライドする、または

  4. このメソッドの内部javax.swing.Timerでは、小さな取引から始めます350-500ms

  5. それでもサイズ変更が続く場合はTimer#restart()

  6. Swing Actionからの出力用に追加Swing Timer

  7. 削除Thread.sleep(int)、サイズ変更中にちらつきまたはフリーズが発生しました

  8. 上記のすべてのコードは、適切なLayout ManagerGridBagLayoutまたはカスタム(および今日)に置き換えることができますMigLayout

于 2012-11-09T08:13:56.907 に答える