问题
i've been doing some introduction to solidstate physics with using Java3D. After delving into many 3D things inside, decided to put some buttons to test different things. My problem emerged here.
In the below picture, i was using setLayout(null) to place buttons and Canvas3D:
What i did not want here is:
- Canvas3D overlaps buttons.
- Canvas3D does not stretch to keep same ratio of picture size / window size so when i resize window it just is in same size.
Then i tried using setLayout(new BorderLayout());
and add("Center",component);
for the Canvas3D while "South", "West", "East" for the buttons. Below picture is showing the case:
This looks better but this time another problem comes:
- Buttons are not the size i wanted(stretches to window) and is not at the offset position i wanted.
- Buttons also overlap eachother when i put two on the same area like "South".
Question: How can i make Canvas3D stretch to window while buttons are just in place i wanted (x,y) and has the size i wanted (h,w) without being overlapped by Canvas3D. I tried different layouts for Canvas3D and Buttons-->failed. Only one layout is showing.
Thank you for your time.
Java3D 1.5.2 and Eclipse . Windows XP and intel centrino
回答1:
This isn't really a Java3D problem so much as it is a Swing Layout problem.
The correct solution is probably some kind of nested layout, depending on how you want it to look.
For example, you could try a border layout, with a nested FlowLayout on the North or West sides for your buttons, which is probably closer to what you are looking for. Try something like below, and customize to your needs.
JPanel rootPanel = new JPanel(new BorderLayout());
JPanel controlPanel = new JPanel(new FlowLayout());
controlPanel.add(new JButton("Alpha"));
controlPanel.add(new JButton("Beta"));
controlPanel.add(new JButton("Gamma"));
rootPanel.add(controlPanel, BorderLayout.NORTH);
rootPanel.add(myCanvas3D, BorderLayout.CENTER);
来源:https://stackoverflow.com/questions/12314324/java3d-with-canvas3d-and-button-overlapping