大家好:D当有人按下按钮时,我试图在GUI中放置按钮,而我做到了。但我有一个问题,首先,在我的代码中,似乎框布局不起作用。我的意思是我希望该按钮出现在添加命令按钮的下方,但它出现在它的右侧(因为我想是流程布局)。代码-
static JLabel name=new JLabel("TESTING123");
static JButton add=new JButton("Add New Command");
static JButton a=new JButton("Press Me To Set Command Number 1");
static JPanel panel=new JPanel();
static JFrame frame=new JFrame("TEST FRAME");
public static void init(){
frame.getContentPane().setBackground(Color.WHITE);
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));//THIS IS NOT WORKING
//======================================================SOME FRAME PROPERTIES
panel.add(name);
panel.add(add);
//======================================================ADDING TO PANELS
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.add(a);
frame.repaint();
frame.validate();
}
});
//======================================================LISTENERS
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(450,500);
frame.setVisible(true);
}
我的意思是我希望该按钮出现在添加命令按钮的下方,但它出现在其右侧
panel.add(a);
JPanel的默认布局是FlowLayout。如果要在下面添加组件,则需要将面板的布局更改为BoxLayout。
或者,可以将其添加到框架的内容窗格中,而不是将组件添加到面板中,因为该组件已经在使用BoxLayout了:
frame.add(a);
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS)); //
更改框架的布局不会影响面板的布局。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句