Reputation: 363
I am newby to java swing gui and I cannot figure out this problem Lets assume I Have a Jframe size 100,100 I have 4 button positions
left top widht height
button1 0 0 10 10 // left up
button2 90 0 10 10 // right up
button3 45 45 10 10 // middle
button4 0 90 10 10 // left down
button5 90 90 10 10 // right down
All of left top width height sized scaled from 100,100.
I need using Absolute positioning (Becaouse my real situation different left,top,width,height) But responsiveness I consider maybe I can use factors to multiple the left,right,width,height values
For example after running gui if I change frame to 400,200 then new positions should be
button1 0 0 40 20 // left up
button2 360 0 40 20 // right up
button3 180 90 40 20 // middle
button4 0 180 40 20 // left down
button5 360 180 40 20 // right down
x factor = 400/100=4 and y factor = 200/100=2
How can I listen Jframe's changes from 100,100 to 400,200 and develop above approach. Any information appreciated.
EDIT
I get left top width height from different service and my example could be like
left top widht height
button1 01 04 11 14
button2 91 0 10 10
button3 44 45 9 14
button4 0 90 10 10
button5 90 90 1 1
Upvotes: 0
Views: 2694
Reputation: 347314
I need using Absolute positioning
When ever you think you need "absolute positioning", you don't. Seriously, I've never had a need to use one which wasn't solved through some other means, either an existing layout or a custom layout.
The layout management API is designed to do exactly what you're asking, better to make use of it and save your hair.
This is a pretty simple example using GridBagLayout
and yes, if the buttons had different heights, it would still cope
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Common properties...
gbc.weightx = 0.5;
gbc.weighty = 0.333;
// Top/left
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
add(new JButton("Top left is all mine"), gbc);
// Top/right
gbc.gridx = 2;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.NORTHEAST;
add(new JButton("Top right mine, keep out"), gbc);
// middle
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("My precious"), gbc);
// bottom/left
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.SOUTHWEST;
add(new JButton("Got this space covered"), gbc);
// bottom/right
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.SOUTHEAST;
add(new JButton("Why is every one so unfriendly?"), gbc);
}
}
}
Got high buttons...
Got that covered, automatically
Upvotes: 3