0
我想创建一个JFrame,并在其中我想让按钮(选择设备)在顶部和一个文本消息(活动),它是在底部的标签形式。我无法做到这一点,他们都在同一行旁边。对齐JFrame上的JButton和JLabel
JFrame f= new JFrame("AutoV");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
JButton b=new JButton("Select the Device");
JLabel lab=new JLabel("Active");
lab.setVerticalAlignment(SwingConstants.BOTTOM);
//p.add(b);
p.add(lab);
p.setBorder(BorderFactory.createLineBorder(Color.black));
f.add(p);
Dimension dim1 = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim1.width/2-f.getSize().width/2, dim1.height/2-f.getSize().height/2);
1的JPanel具有的FlowLayout和2 f.setVisible(真);必须是最后一个代码行,3 – mKorbel
控制位置由布局控制,哪一个用于画布? –
@RomanC作为@mKorbel所说的默认布局是'FlowLayout',因为提问者没有设置其他任何东西。此外,在Swing中没有画布:) – user1803551