如何更改下面的代码,改变框架大小后,框架上其他对象的大小也以相同的比例变化?
package application;
import javax.swing.*;
import java.awt.*;
public class Program extends JFrame {
private GroupLayout layout;
private GroupLayout.Group groupX;
private GroupLayout.Group groupY;
public Program() {
initialize();
}
private void initialize() {
this.layout = new GroupLayout(this.getContentPane());
this.groupX = this.layout.createParallelGroup(GroupLayout.Alignment.LEADING);
this.groupY = this.layout.createParallelGroup(GroupLayout.Alignment.LEADING);
}
public void addComponent(Component component, int x, int y) {
getContentPane().setLayout(layout);
groupX.addGroup(layout.createSequentialGroup()
.addGap(x, x, x)
.addComponent(component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
groupY.addGroup(layout.createSequentialGroup()
.addGap(y, y, y)
.addComponent(component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
layout.setHorizontalGroup(groupX);
layout.setVerticalGroup(groupY);
}
public static void main(String[] args) {
Program jf = new Program();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(new Rectangle(0, 0, 250, 100));
JButton p = new JButton("Click!!");
jf.addComponent(p, 86, 20);
jf.setVisible(true);
}
}
如何更改下面的代码,改变框架大小后,框架上其他对象的大小也以相同的比例变化?