What is wrong with my layout code? [closed]

2019-09-23 01:44发布

So, when I start my program, my buttons get mixed up and I get a huge JButton accross whole screen. Here is the code:

super ("Title");

okvir = new JFrame();
okvir.setSize(1350, 768);
okvir.setLocation (0, 0);
okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okvir.setVisible(true);

sab = new JButton("sabiranje");
sab.setBounds (20 , 20 , 50 , 20);
okvir.add(sab);
od = new JButton ("oduzimanje");
od.setBounds (20 , 40 , 50 , 20);
okvir.add(od);
rez = new JLabel ("rezultat");
rez.setBounds (20 , 60 , 50 , 20);
okvir.add(rez);
x = new JTextField ("",4);
x.setBounds (20 , 80 , 50 , 20);
okvir.add(x);
y = new JTextField("",4);
y.setBounds (20 , 100 , 50 , 20);
okvir.add(y);
mnoz = new JButton("mnozenje");
mnoz.setBounds (20 , 120 , 50 , 20);
okvir.add(mnoz);
delj = new JButton ("deljenje");
delj.setBounds (20 , 140 , 50 , 20);
okvir.add(delj);

标签: java layout
1条回答
贪生不怕死
2楼-- · 2019-09-23 02:24

You're placing all the buttons in the BorderLayout.CENTER position of the frame which not only respect referred sizes of its child components but replaces each subsequent button. Create a new panel to accomodate the buttons.

Calling setBounds will have no effect since youre using a layout manager so these statements can be deleted

查看更多
登录 后发表回答