gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:55:31 24 4
gpt4 key购买 nike

我想让 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();

最佳答案

考虑使用 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();
}
}

关于java - 将 glCanvas 放置在包含按钮的 JPanel 旁边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23943524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com