1

In my Java program when a button is called then some label and text field will be displayed. But, text field is only displayed and when I decrease or increase the frame size using mouse then the label is displayed.

Code:

public void actionPerformed(ActionEvent e)
{

    if(e.getSource()==button)
    {

    l1= new JLabel("Name: ");
    l1.setForeground(Color.white);
    l1.setFont(new Font("Serif", Font.BOLD, 15));
    l1.setBounds(100,100,150,20);

    l2= new JLabel("Mobile Number: ");
    l2.setForeground(Color.white);
    l2.setFont(new Font("Serif", Font.BOLD, 15));
    l2.setBounds(100,150,150,20);

    l3= new JLabel("Father's Name: ");
    l3.setForeground(Color.white);
    l3.setFont(new Font("Serif", Font.BOLD, 15));
    l3.setBounds(100,200,150,20);

    l4= new JLabel("Mobile Number: ");
    l4.setForeground(Color.white);
    l4.setFont(new Font("Serif", Font.BOLD, 15));
    l4.setBounds(100,250,150,20);

    l5= new JLabel("Mother's Name: ");
    l5.setForeground(Color.white);
    l5.setFont(new Font("Serif", Font.BOLD, 15));
    l5.setBounds(100,300,150,20);

    l6= new JLabel("Mobile Number: ");
    l6.setForeground(Color.white);
    l6.setFont(new Font("Serif", Font.BOLD, 15));
    l6.setBounds(100,350,150,20);

    l7= new JLabel("Village: ");
    l7.setForeground(Color.white);
    l7.setFont(new Font("Serif", Font.BOLD, 15));
    l7.setBounds(100,400,150,20);

    l8= new JLabel("Police Station: ");
    l8.setForeground(Color.white);
    l8.setFont(new Font("Serif", Font.BOLD, 15));
    l8.setBounds(100,450,150,20);

    l9= new JLabel("District: ");
    l9.setForeground(Color.white);
    l9.setFont(new Font("Serif", Font.BOLD, 15));
    l9.setBounds(100,500,150,20);

    l10= new JLabel("Blood Group: ");
    l10.setForeground(Color.white);
    l10.setFont(new Font("Serif", Font.BOLD, 15));
    l10.setBounds(100,550,150,20);

    l11= new JLabel("Department: ");
    l11.setForeground(Color.white);
    l11.setFont(new Font("Serif", Font.BOLD, 15));
    l11.setBounds(600,100,150,20);

    l12= new JLabel("Series: ");
    l12.setForeground(Color.white);
    l12.setFont(new Font("Serif", Font.BOLD, 15));
    l12.setBounds(600,150,150,20);

    l13= new JLabel("Roll Number: ");
    l13.setForeground(Color.white);
    l13.setFont(new Font("Serif", Font.BOLD, 15));
    l13.setBounds(600,200,150,20);

    l14= new JLabel("CGPA: ");
    l14.setForeground(Color.white);
    l14.setFont(new Font("Serif", Font.BOLD, 15));
    l14.setBounds(600,250,150,20);

    l15= new JLabel("Room Number: ");
    l15.setForeground(Color.white);
    l15.setFont(new Font("Serif", Font.BOLD, 15));
    l15.setBounds(600,300,150,20);





    tf1= new JTextField();
    tf1.setBounds(300,100,200,20);

    tf2= new JTextField();
    tf2.setBounds(300,150,200,20);

    tf3= new JTextField();
    tf3.setBounds(300,200,200,20);

    tf4= new JTextField();
    tf4.setBounds(300,250,200,20);

    tf5= new JTextField();
    tf5.setBounds(300,300,200,20);

    tf6= new JTextField();
    tf6.setBounds(300,350,200,20);

    tf7= new JTextField();
    tf7.setBounds(300,400,200,20);

    tf8= new JTextField();
    tf8.setBounds(300,450,200,20);

    tf9= new JTextField();
    tf9.setBounds(300,500,200,20);

    tf10= new JTextField();
    tf10.setBounds(300,550,200,20);

    tf11= new JTextField();
    tf11.setBounds(710,100,200,20);

    tf12= new JTextField();
    tf12.setBounds(710,150,200,20);

    tf13= new JTextField();
    tf13.setBounds(710,200,200,20);

    tf14= new JTextField();
    tf14.setBounds(710,250,200,20);

    tf15= new JTextField();
    tf15.setBounds(710,300,200,20);



    tf1.setEditable(false);
    tf2.setEditable(false);
    tf3.setEditable(false);

    tf4.setEditable(false);
    tf5.setEditable(false);
    tf6.setEditable(false);

    tf7.setEditable(false);
    tf8.setEditable(false);
    tf9.setEditable(false);

    tf10.setEditable(false);
    tf11.setEditable(false);
    tf12.setEditable(false);

    tf13.setEditable(false);
    tf14.setEditable(false);
    tf15.setEditable(false);






    try {  

        Class.forName("com.mysql.jdbc.Driver");  

        Connection con=DriverManager.getConnection(  
        "jdbc:mysql://localhost:3306/hostel_management","root","");  

        //here project is database name, root is username and password is empty 

        Statement stmt=con.createStatement();  

        ResultSet rs=stmt.executeQuery("select * from student_info where roll_no = '" + textfield.getText() + "'");  

        //if(rs.next()=null)
            //JOptionPane.showMessageDialog(null, "No Available Seat. Please Select Another Room.", "Warning", JOptionPane.ERROR_MESSAGE);
        int count= 0;










        while(rs.next())  
        {

             count++;


             tf1.setText(rs.getString(1));
             tf1.setHorizontalAlignment(JTextField.CENTER);

             tf2.setText(rs.getString(2));
             tf2.setHorizontalAlignment(JTextField.CENTER);

             tf3.setText(rs.getString(3));
             tf3.setHorizontalAlignment(JTextField.CENTER);

             tf4.setText(rs.getString(4));
             tf4.setHorizontalAlignment(JTextField.CENTER);

             tf5.setText(rs.getString(5));
             tf5.setHorizontalAlignment(JTextField.CENTER);

             tf6.setText(rs.getString(6));
             tf6.setHorizontalAlignment(JTextField.CENTER);

             tf7.setText(rs.getString(7));
             tf7.setHorizontalAlignment(JTextField.CENTER);

             tf8.setText(rs.getString(8));
             tf8.setHorizontalAlignment(JTextField.CENTER);

             tf9.setText(rs.getString(9));
             tf9.setHorizontalAlignment(JTextField.CENTER);

             tf10.setText(rs.getString(10));
             tf10.setHorizontalAlignment(JTextField.CENTER);

             tf11.setText(rs.getString(11));
             tf11.setHorizontalAlignment(JTextField.CENTER);

             tf12.setText(rs.getString(12));
             tf12.setHorizontalAlignment(JTextField.CENTER);

             tf13.setText(rs.getString(13));
             tf13.setHorizontalAlignment(JTextField.CENTER);

             tf14.setText(rs.getString(14));
             tf14.setHorizontalAlignment(JTextField.CENTER);

             tf15.setText(rs.getString(15));
             tf15.setHorizontalAlignment(JTextField.CENTER)

            add(l1);
              add(tf1);
              add(l2);
              add(tf2);
              add(l3);
              add(tf3);
              add(l4);
              add(tf4);
              add(l5);
              add(tf5);
              add(l6);
              add(tf6);
              add(l7);
              add(tf7);
              add(l8);
              add(tf8);
              add(l9);
              add(tf9);
              add(l10);
              add(tf10);
              add(l11);
              add(tf11);
              add(l12);
              add(tf12);
              add(l13);
              add(tf13);
              add(l14);
              add(tf14);
              add(l15);
              add(tf15);



                add(frame_imagelabel);

        }

Screen shot is given below 1

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 04 '15 at 21:08

4 Answers4

1

Maybe the problem is that don't set target for label. You should yse this method - label.setLabelFor(target). So try to modify your code and add this for each label:

l1= new JLabel("Name: ");
l1.setForeground(Color.white);
l1.setFont(new Font("Serif", Font.BOLD, 15));
l1.setBounds(100,100,150,20);
l1.setLabelFor(targetTextField)
Vladyslav Sheruda
  • 1,856
  • 18
  • 26
1

Solved Code Using pack()...Thnks all :)...

import javax.swing.*;



import java.util.Vector;
import java.sql.*;
import java.awt.event.*; 
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;



public class Student_Database extends JFrame implements ActionListener {


private JTextField textfield, tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10, tf11, tf12, tf13, tf14,tf15;
private JLabel label,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15, l16, frame_imagelabel;
private JButton button, button2;
private ImageIcon frame_image, icon, icon1;
private Image img, myimg;   

public Student_Database() {


    setTitle("Student Information");



    label= new JLabel("Enter Roll Number: ");
    label.setForeground(Color.white);
    label.setFont(new Font("Serif", Font.BOLD, 18));
    label.setBounds(200,30,160,20);
    textfield= new JTextField(6);
    textfield.setBounds(370, 30, 100, 20);
    button= new JButton ("OK");
    button.setBounds(480, 30, 80, 20);

    add(label);
    add(textfield);
    add(button);


    button2=new JButton("CLICK for Main Menu");//creating instance of JButton  
    button2.setBounds(5,10,160,40);//x axis, y axis, width, height  
    add(button2);//adding button in JFrame 
    button2.setBackground(Color.white);
    button2.setForeground(Color.black);
    button2.addActionListener(this);



    button.addActionListener(this);


    frame_image= new ImageIcon ("F:/Java_program/Hostel_Management/Resource/images (1).jpg");
    frame_imagelabel= new JLabel(frame_image);
    frame_imagelabel.setBounds(0,0,1000, 700);
    add(frame_imagelabel);



    setSize(1000,700);//400 width and 600 height  
    setLayout(new BorderLayout());//using no layout managers  
    //getContentPane().setBackground(Color.white);  
    //pack();
    //revalidate();
    setVisible(true);//making the frame visible 

}

public void actionPerformed(ActionEvent e)
{

    if(e.getSource()==button)
    {

    l1= new JLabel("Name: ");
    l1.setForeground(Color.white);
    l1.setFont(new Font("Serif", Font.BOLD, 15));
    l1.setBounds(100,100,150,20);
    //l1.setLabelFor(tf1);

    l2= new JLabel("Mobile Number: ");
    l2.setForeground(Color.white);
    l2.setFont(new Font("Serif", Font.BOLD, 15));
    l2.setBounds(100,150,150,20);
    //l1.setLabelFor(tf2);

    l3= new JLabel("Father's Name: ");
    l3.setForeground(Color.white);
    l3.setFont(new Font("Serif", Font.BOLD, 15));
    l3.setBounds(100,200,150,20);

    l4= new JLabel("Mobile Number: ");
    l4.setForeground(Color.white);
    l4.setFont(new Font("Serif", Font.BOLD, 15));
    l4.setBounds(100,250,150,20);

    l5= new JLabel("Mother's Name: ");
    l5.setForeground(Color.white);
    l5.setFont(new Font("Serif", Font.BOLD, 15));
    l5.setBounds(100,300,150,20);

    l6= new JLabel("Mobile Number: ");
    l6.setForeground(Color.white);
    l6.setFont(new Font("Serif", Font.BOLD, 15));
    l6.setBounds(100,350,150,20);

    l7= new JLabel("Village: ");
    l7.setForeground(Color.white);
    l7.setFont(new Font("Serif", Font.BOLD, 15));
    l7.setBounds(100,400,150,20);

    l8= new JLabel("Police Station: ");
    l8.setForeground(Color.white);
    l8.setFont(new Font("Serif", Font.BOLD, 15));
    l8.setBounds(100,450,150,20);

    l9= new JLabel("District: ");
    l9.setForeground(Color.white);
    l9.setFont(new Font("Serif", Font.BOLD, 15));
    l9.setBounds(100,500,150,20);

    l10= new JLabel("Blood Group: ");
    l10.setForeground(Color.white);
    l10.setFont(new Font("Serif", Font.BOLD, 15));
    l10.setBounds(100,550,150,20);

    l11= new JLabel("Department: ");
    l11.setForeground(Color.white);
    l11.setFont(new Font("Serif", Font.BOLD, 15));
    l11.setBounds(600,100,150,20);

    l12= new JLabel("Series: ");
    l12.setForeground(Color.white);
    l12.setFont(new Font("Serif", Font.BOLD, 15));
    l12.setBounds(600,150,150,20);

    l13= new JLabel("Roll Number: ");
    l13.setForeground(Color.white);
    l13.setFont(new Font("Serif", Font.BOLD, 15));
    l13.setBounds(600,200,150,20);

    l14= new JLabel("CGPA: ");
    l14.setForeground(Color.white);
    l14.setFont(new Font("Serif", Font.BOLD, 15));
    l14.setBounds(600,250,150,20);

    l15= new JLabel("Room Number: ");
    l15.setForeground(Color.white);
    l15.setFont(new Font("Serif", Font.BOLD, 15));
    l15.setBounds(600,300,150,20);





    tf1= new JTextField();
    tf1.setBounds(300,100,200,20);

    tf2= new JTextField();
    tf2.setBounds(300,150,200,20);

    tf3= new JTextField();
    tf3.setBounds(300,200,200,20);

    tf4= new JTextField();
    tf4.setBounds(300,250,200,20);

    tf5= new JTextField();
    tf5.setBounds(300,300,200,20);

    tf6= new JTextField();
    tf6.setBounds(300,350,200,20);

    tf7= new JTextField();
    tf7.setBounds(300,400,200,20);

    tf8= new JTextField();
    tf8.setBounds(300,450,200,20);

    tf9= new JTextField();
    tf9.setBounds(300,500,200,20);

    tf10= new JTextField();
    tf10.setBounds(300,550,200,20);

    tf11= new JTextField();
    tf11.setBounds(710,100,200,20);

    tf12= new JTextField();
    tf12.setBounds(710,150,200,20);

    tf13= new JTextField();
    tf13.setBounds(710,200,200,20);

    tf14= new JTextField();
    tf14.setBounds(710,250,200,20);

    tf15= new JTextField();
    tf15.setBounds(710,300,200,20);



    tf1.setEditable(false);
    tf2.setEditable(false);
    tf3.setEditable(false);

    tf4.setEditable(false);
    tf5.setEditable(false);
    tf6.setEditable(false);

    tf7.setEditable(false);
    tf8.setEditable(false);
    tf9.setEditable(false);

    tf10.setEditable(false);
    tf11.setEditable(false);
    tf12.setEditable(false);

    tf13.setEditable(false);
    tf14.setEditable(false);
    tf15.setEditable(false);






    try {  

        Class.forName("com.mysql.jdbc.Driver");  

        Connection con=DriverManager.getConnection(  
        "jdbc:mysql://localhost:3306/hostel_management","root","");  

        //here project is database name, root is username and password is empty 

        Statement stmt=con.createStatement();  

        ResultSet rs=stmt.executeQuery("select * from student_info where roll_no = '" + textfield.getText() + "'");  

        //if(rs.next()=null)
            //JOptionPane.showMessageDialog(null, "No Available Seat. Please Select Another Room.", "Warning", JOptionPane.ERROR_MESSAGE);
        int count= 0;










        while(rs.next())  
        {

             count++;


             tf1.setText(rs.getString(1));
             tf1.setHorizontalAlignment(JTextField.CENTER);

             tf2.setText(rs.getString(2));
             tf2.setHorizontalAlignment(JTextField.CENTER);

             tf3.setText(rs.getString(3));
             tf3.setHorizontalAlignment(JTextField.CENTER);

             tf4.setText(rs.getString(4));
             tf4.setHorizontalAlignment(JTextField.CENTER);

             tf5.setText(rs.getString(5));
             tf5.setHorizontalAlignment(JTextField.CENTER);

             tf6.setText(rs.getString(6));
             tf6.setHorizontalAlignment(JTextField.CENTER);

             tf7.setText(rs.getString(7));
             tf7.setHorizontalAlignment(JTextField.CENTER);

             tf8.setText(rs.getString(8));
             tf8.setHorizontalAlignment(JTextField.CENTER);

             tf9.setText(rs.getString(9));
             tf9.setHorizontalAlignment(JTextField.CENTER);

             tf10.setText(rs.getString(10));
             tf10.setHorizontalAlignment(JTextField.CENTER);

             tf11.setText(rs.getString(11));
             tf11.setHorizontalAlignment(JTextField.CENTER);

             tf12.setText(rs.getString(12));
             tf12.setHorizontalAlignment(JTextField.CENTER);

             tf13.setText(rs.getString(13));
             tf13.setHorizontalAlignment(JTextField.CENTER);

             tf14.setText(rs.getString(14));
             tf14.setHorizontalAlignment(JTextField.CENTER);

             tf15.setText(rs.getString(15));
             tf15.setHorizontalAlignment(JTextField.CENTER);



             Blob blob = rs.getBlob("image");
             int blobLength = (int) blob.length();  

             byte[] bytes = blob.getBytes(1, blobLength);
            //blob.free();
             BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

             ImageIcon icon = new ImageIcon(bytes); // you can read straight from byte array
             l16 = new JLabel(icon);
             l16.setBounds(100, 100, 100, 50);
             add(l16);



             //add(frame_imagelabel);
              add(l1);
              add(tf1);
              add(l2);
              add(tf2);
              add(l3);
              add(tf3);
              add(l4);
              add(tf4);
              add(l5);
              add(tf5);
              add(l6);
              add(tf6);
              add(l7);
              add(tf7);
              add(l8);
              add(tf8);
              add(l9);
              add(tf9);
              add(l10);
              add(tf10);
              add(l11);
              add(tf11);
              add(l12);
              add(tf12);
              add(l13);
              add(tf13);
              add(l14);
              add(tf14);
              add(l15);
              add(tf15);



              add(frame_imagelabel);
              pack();
              setSize(1000,700);


        }


        //if(im!=null)
        //JOptionPane.showMessageDialog(null, "No image", "Warning", JOptionPane.ERROR_MESSAGE);


        if(count==0)
            JOptionPane.showMessageDialog(null, "This Student is not a member of this hall.", "Warning", JOptionPane.ERROR_MESSAGE);


        con.close();  

        } catch(Exception ex){ JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE); }  


    }





if(e.getSource()==button2)
{
    project obj =new project();
    dispose();
}

}
}
0

I can't comment so I'll make an answer. If I were you, I'd make a Class like Label.java Create a constructor that takes all the neccessary data:

private JLabel label;
private Color fg;
private Font font;
private Rectangle rect;

public Label(String name, Color foreground, Font font, Rectangle rect){
    this.label = new JLabel(name);
    this.label.setForeground(foreground);
    this.label.setFont(font);
    this.label.setBounds(rect);
}

and an arraylist to store all those Labels and just load them using forloop, it would make the code look a lot more clear

ArrayList<Label> labels = new ArrayList<Label>();
labels.add(new Label("Name: ", Color.white, new Font("Serif", Font.BOLD, 15), new Rectangle(100, 100, 150, 20)));
labels.add(new Label("Mobile Number: ", Color.white, new Font("Serif", Font.BOLD, 15), new Rectangle(100, 150, 150, 20)));
/*.
  .
  .*/
user3738243
  • 159
  • 2
  • 13
0

You are adding frame with background in last you should add background first and than add your labels ,buttons etc

setLayout(new BorderLayout());
setContentPane(new JLabelnew ImageIcon(getClass().getResource("image.jpg"))));

first copy and paste your background image in src folder than add this background code first and than add labels,buttons etc. I suggest you to use Layout rather than bounds

Programmer
  • 445
  • 4
  • 12