我正在尝试创建一个 boxlayout 面板,其中有 2 个其他面板,其中一个只有另一个的一半大小(因此比例为 1/3 到 2/3)。设置首选大小似乎不起作用,我一直无法找出任何其他方式(以下代码主要是使用 Windowbuilder 生成的,如果有点混乱,请道歉):
public class GUIControls extends JFrame implements IGUIControls {
private JTextField textField;
private int posX=0,posY=0;
public GUIControls() {
getContentPane().setBackground(Color.WHITE);
getContentPane().setLayout(new CardLayout(0, 0));
JPanel rootPanel = new JPanel();
rootPanel.setBorder(new LineBorder(new Color(0, 0, 255), 1, true));
rootPanel.setBackground(Color.WHITE);
getContentPane().add(rootPanel, "name_25253210045969");
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS));
rootPanel.setPreferredSize(new Dimension(600, 400));
JPanel menuLeft = new JPanel();
rootPanel.add(menuLeft);
menuLeft.setBackground(Color.WHITE);
menuLeft.setLayout(null);
menuLeft.setPreferredSize(new Dimension((1/3)*rootPanel.getWidth(), rootPanel.getHeight()));
JPanel contentRight = new JPanel();
rootPanel.add(contentRight);
contentRight.setBackground(Color.WHITE);
contentRight.setPreferredSize(new Dimension((2/3)*rootPanel.getWidth(), rootPanel.getHeight()));
contentRight.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
CardLayout cardLayout = (CardLayout) getContentPane().getLayout();
cardLayout.show(getContentPane(), "name_36737116256884");
}
});
contentRight.setLayout(null);
setUndecorated(true);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
posX=e.getX();
posY=e.getY();
}
});
addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent evt)
{
//sets frame position when mouse dragged
setLocation (evt.getXOnScreen()-posX,evt.getYOnScreen()-posY);
}
});
}
public void runGUI(){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUIControls guiControls = new GUIControls();
guiControls.pack();
guiControls.setLocationRelativeTo(null);
guiControls.setSize(600, 400);
guiControls.setVisible(true);
}
});
}
}
有谁知道我会怎么做?
谢谢。