如何通过 JPanel 或 JFrame 的分辨率来布局JComponent?
您好,我是Java编码的新手,而且在布局组件时遇到问题 在 JPanel 中。 每当Frame的分辨率发生变化时,是否可以通过一种好方法来重置组件的大小和位置? 这是我的代码。
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);
}
}
}
我想通过JFrame的分辨率在面板中调整所有组件的大小。 但是找不到合适的解决方案。