0

I want to make 3 panels, where one is to the west side, one to the east, and one to the south. When I complie this, it gives me frame with colors all one above another and it doesn't give me buttons.

frame = new JFrame();
frame.setBounds(600, 200, 500, 350);
frame.setTitle("Dr. Idrizovic");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panelWest = new JPanel();
panelWest.setBounds(0, 0, 175, 310);
panelWest.setLayout(null);
panelWest.setBackground(Color.green);   

regUslugaButt = new JButton("Registar usluga");
regUslugaButt.setBounds(12, 35, 150, 25);

regMaterijalaButt = new JButton("Registar materijala");
regMaterijalaButt.setBounds(12, 95, 150, 25);

regIntervencijaButt = new JButton("Registar intervencija");
regIntervencijaButt.setBounds(12, 155, 150, 25);

regDijagnozaButt = new JButton("Registar dijagnoza");
regDijagnozaButt.setBounds(12, 215, 150, 25);


panelEast = new JPanel();
panelEast.setBounds(325, 0, 175, 310);
panelEast.setLayout(null);
panelEast.setBackground(Color.red);

evidencijaPacButt = new JButton("Evidencija pacijenata");
evidencijaPacButt.setBounds(324, 35, 150, 25);

zakazivanjePacButt = new JButton("Zakazivanje pacijenata");
zakazivanjePacButt.setBounds(12, 95, 150, 25);

evidencijaStomatologaButt = new JButton("Evidencija stomatologa");
evidencijaStomatologaButt.setBounds(12, 155, 150, 25);

izvrseneUslugeButt = new JButton("Izvrsene usluge");
izvrseneUslugeButt.setBounds(12, 215, 150, 25);






panelSouth = new JPanel();
panelSouth.setBounds(175, 310, 150, 40);
panelSouth.setLayout(null);
panelSouth.setBackground(Color.black);

exitButt = new JButton("Kraj rada");
exitButt.setBounds(174, 260, 150, 25);






panelWest.add(regUslugaButt);
panelWest.add(regMaterijalaButt);
panelWest.add(regIntervencijaButt);
panelWest.add(regDijagnozaButt);

panelEast.add(evidencijaPacButt);
panelEast.add(zakazivanjePacButt);
panelEast.add(evidencijaStomatologaButt);
panelEast.add(izvrseneUslugeButt);

panelSouth.add(exitButt);

frame.add(panelWest);
frame.add(panelSouth);
frame.add(panelEast);
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
vulovicv
  • 19
  • 4
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) Provide ASCII art, or a simple drawing, of the layout of the GUI. – Andrew Thompson Nov 06 '14 at 23:29
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See [Why is it frowned upon to use a null layout in SWING?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) for more details – MadProgrammer Nov 06 '14 at 23:54

2 Answers2

3

I want to make 3 panels where one is to the west side,one to the east and one to the south.

Don't use a null layout. Don't use setBounds().

Instead you should be using a BorderLayout for you main panel. Your child panels should also use an appropriate layout manager.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • no,because it doesnt give me the interface I want. Can I set bounds for panels and then use layout inside them for buttons? – vulovicv Nov 06 '14 at 16:34
  • Then use a different layout manager for the main layout. Layout managers are extremely flexible once you learn to use them. – camickr Nov 06 '14 at 16:35
  • and why is it not setBounds() useful? – vulovicv Nov 06 '14 at 16:36
  • 2
    Read the tutorial!!! Swing was designed to be used with layout managers for many reasons. For example, manually trying to set the size/location of a components just doesn't work on different platforms because each platform uses different fonts. Scrolling doesn't work. Take the time to learn to create reusable/maintainable code rather than using brute force code which is not maintainable. – camickr Nov 06 '14 at 16:40
  • @vulovicv Here is the official tutorial for null layout: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html First line is "Don't use null layout." – DSquare Nov 06 '14 at 16:42
0

you have a JFrame you need to set a layout to the frame you have like this:

          frame.setLayout(new BorderLayout());

and later you need to add each panel in the position you like, like this:

    frame.add(panelWest, BorderLayout.WEST);

remember to this at the end, when you alredy set all the properties of all the panels.

Juan Henao
  • 220
  • 1
  • 3
  • 14
  • The BorderLayout is the default layout for the JFrame so you do not need to set it manually. The tutorial already has all this information. – camickr Nov 06 '14 at 16:56