How to layout the JComponents by resolution of JPanel or JFrame?
Hello, I'm new to java coding and I'm having problem layoutting components in JPanel. Is there Good way to reset the size and location of components whenever Frame's resolution changes? here is my code.
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);
}
}
}
I want to resize all Components in Panel by resolution of JFrame. But couldn't found good solution for that.