-1

Here is my code.I have some issue and i don't know how to fix that,so look..

package testdnevnik;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class StartWindow extends JPanel {

  JPanel jp = new JPanel();
  JPanel jp1 = new JPanel();
  JLabel jl = new JLabel();
  JLabel headline = new JLabel();
  JButton viewStudents = new JButton();
  JButton editStudents = new JButton();
  JButton addStudents = new JButton();
  JButton removeStudents = new JButton();

  public StartWindow () {

  jp.setLayout(new BorderLayout());
  // image for background
  jl.setIcon(new ImageIcon("img\\bg2.jpg"));

  jl.setPreferredSize(new Dimension(800,600));
  jl.setMaximumSize(new Dimension(800,600));
  jl.setMinimumSize(new Dimension(800,600));
  jl.setSize(800, 600);

  // headline
 headline.setText("Diary");
 headline.setLocation(200, 30);
 headline.setHorizontalAlignment(JLabel.CENTER);
 headline.setFont(new Font("Serif", Font.BOLD, 60));
 headline.setForeground(Color.ORANGE);
 headline.setSize(300, 50);
 headline.setVisible(true);

  // Buttons
 //View Students - Button
 viewStudents.setText("View All Students");
 viewStudents.setSize(200,40);
 viewStudents.setLocation(250 , 200);
 viewStudents.setVisible(true);

  // Edit Students - Button
 editStudents.setText("Edit Students");
 editStudents.setSize(200,40);
 editStudents.setLocation(250 , 300);
 editStudents.setVisible(true);

  // Add Students - Button
 addStudents.setText("Add New Students");
 addStudents.setSize(200,40);
 addStudents.setLocation(250 ,400);
 addStudents.setVisible(true);

  // Remove Students - Button
 removeStudents.setText("Remove Students");
 removeStudents.setSize(200,40);
 removeStudents.setLocation(250 , 500);
 removeStudents.setVisible(true);

 // panel
 jp.setPreferredSize(new Dimension(800,600));
 jp.setMaximumSize(new Dimension(800,600));
 jp.setMinimumSize(new Dimension(800,600));
 jp.setSize(800, 600);

 jp1.add(viewStudents); // add ViewStudents button to the panel
 jp1.add(editStudents); // add EditStudents button to the panel
 jp1.add(addStudents); // add AddStudents button to the panel
 jp1.add(removeStudents); // add RemoveStudents button to the panel
 jp1.add(headline); // add headline label to the panel
 jp.add(jl); // add background image to the panel

 add(jp);
 add(jp1);

 validate();
}

  public void checkStudents(int n) {
     int numberOfStudents = n;
        if (numberOfStudents == 0) {
           removeStudents.setEnabled(false);
     }
  }
}

and here is the main class.

package testdnevnik;

import javax.swing.JFrame;

public class Diary {

  StartWindow startWindow = new StartWindow();

  JFrame frame = new JFrame("Diary");

  int n = 0;

  public Diary() {

     startWindow.checkStudents(n);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(800, 600);
     frame.setResizable(false);
     frame.setVisible(true);
     frame.add(startWindow);

  }

  public static void main(String[] args) {
     new Diary();
  }
}   

Now,for the issue..why i have a white space in the top of the frame??how to fix that??

enter image description here

please help if someone know how to fix that

Kiki
  • 2,243
  • 5
  • 30
  • 43
Stefcho
  • 19
  • 4
  • Using JLabel to represent background seems just wrong. Why not create your own JPanel class which will print image over its background in `painComponent` method like http://pastebin.com/XuNmJvzh and use its instances as your panels? – Pshemo Apr 15 '15 at 20:54

2 Answers2

2

Don't use setPreferredSize(), setMinimumSize() or setMaximumSize(). The size of the label will be the size of the image.

Don't use frame.setSize(...). Instead you use frame.pack() and the frame will be sized to the preferred size of the components added to the frame.

The order or your code should be:

frame.setResizable( false );
frame.add(startWindow)
frame.pack();
frame.setVisible(true);

Finally I think the main problem is that your StartWindow class is a JPanel which by default uses a FlowLayout, which by default use a 5 pixel gap between components. You can change the layout by using setLayout( new BorderLayout() ); now there will be no gap at the top.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

What is your background image size "img\\bg2.jpg" ?

You are setting your frame size as 800x600 so make sure your image the same size as frame. Or you can scale image to fit your frame size.

Ioane Sharvadze
  • 2,118
  • 21
  • 35