Как расположить JComponents всякий раз, когда изменяется разрешение Frame? - PullRequest
0 голосов
/ 21 декабря 2018

Как расположить JComponents по разрешению JPanel или JFrame ?

Здравствуйте, я новичок в Java-кодировании и у меня проблемы с компоновкой компонентовв JPanel .Есть ли хороший способ сбросить размер и расположение компонентов при изменении разрешения кадра ?вот мой код.

public class TestPanel extends JPanel{

private GameBoard SuperFrame; 
/*SuperFrame is JFrame where this Panel attach to..*/

public TestPanel(GameBoard SuperFrame) {
             .....
    this.SuperFrame=SuperFrame;
    this.setLayout(null);
    .......
    add(new Maximizebutton());
            ........
}

  private class MaxmizeButton extends MYUI{
 /*MYUI is abstract class which extends JComponent, and it inherits 
  preferredSize,preferredLocation and abstract void method revalidateSize ...*/

    JButton maxButton;
    public MaxmizeButton (){ 
        preferredSize = new Dimension(SuperFrame.getWidth()/10,SuperFrame.getHeight()/10);
        /*This is my crude solution about setting the size of Component
        by resolution of SuperFrame(JFrame) :( */

        preferredLocation = new Point();
        /*Is there good way to set or calculate the location of this component? */

        this.setSize(preferredSize);
        this.setLocation(preferredLocation);
        maxButton = new JButton("최대화");
        maxButton.setSize(preferredSize);
        maxButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SuperFrame.MaxmizeFrame();
             /*MaximizeFrame method maximizes the SuperFrame..*/
            }
        });
        add(maxButton,0);
        }

    @Override
    void revalidateSize() {
        /*whenever resolution of SuperFrame changes this method will be called 
        and should change the size and location of Components Ideally..*/

        preferredSize = new Dimension(SuperFrame.getWidth()/10,SuperFrame.getHeight()/10);
        preferredLocation = new Point();
        this.setSize(preferredSize);
        this.setLocation(preferredLocation);
        maxButton.setSize(preferredSize);
      }
    }
}

Я хочу изменить размер всех компонентов в Panel по разрешению JFrame.Но не смог найти хорошего решения для этого.

...