将glCanvas放在包含按钮的JPanel旁边

时间:2014-05-29 21:43:42

标签: java swing jogl

我希望JButton panel占用frame个水平空间的约30%。
右侧glCanvas占据frame空间的其余部分 我怎样才能实现这种布局?

目前:

enter image description here

Main.java

GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);

GLCanvas glcanvas = new GLCanvas(capabilities);
glcanvas.addGLEventListener(new GameRenderer());
glcanvas.setSize(100, 100);

JFrame frame = new JFrame("Tool");

JPanel panel = new JPanel();
JPanel cpanel=new JPanel();
panel.setLayout(null);
cpanel.setLayout(null);

JButton ButtonBR = new JButton("1");
JButton ButtonE = new JButton("2");
JButton ButtonR = new JButton("3");
JButton ButtonC = new JButton("4");
JButton ButtonT = new JButton("5");
JButton ButtonCR = new JButton("6");

ButtonBR.setBounds(10, 30, 150, 40);
ButtonE.setBounds(10, 80, 150, 40);
ButtonR.setBounds(10, 130, 150, 40);
ButtonC.setBounds(10, 180, 150, 40);
ButtonT.setBounds(10, 230, 150, 40);
ButtonCR.setBounds(10, 450, 150, 40);
cpanel.add(glcanvas);

panel.add(ButtonBR);
panel.add(ButtonE);
panel.add(ButtonR);
panel.add(ButtonC);
panel.add(ButtonT);
panel.add(ButtonCR);    
frame.add(cpanel);
frame.add(panel);

frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setVisible(true);

Animator animator = new Animator(glcanvas));
animator.start();

1 个答案:

答案 0 :(得分:1)

考虑使用MigLayout:

import javax.swing.JFrame;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;

public class Main extends JFrame {
    private static final long serialVersionUID = 1L;

    public Main() {
        getContentPane().setLayout(new MigLayout("", "[grow 30][grow 70]", "[]"));
        JLabel label = new JLabel("30%");
        getContentPane().add(label, "cell 0 0");
        JTextField textField = new JTextField("70%");
        getContentPane().add(textField, "cell 1 0,growx");
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main();
    }
}