-2

Here is my java code:

JLabel persAdi,persSoyadi,persKodu,ust,alt;
JTextField ad,soyad,tckimlikno;
JButton bul,iptal;

    public persBul(){
        setSize (400,600);
        setResizable(false);
        setLocation (20, 20);
        setVisible(true);

        Container icerik = getContentPane();
        icerik.setLayout(null);
        icerik.getX();

        ust=new JLabel("Bulmak İstediğiniz Personelin;");
        ust.setBounds(10, 10, 200, 30);
        icerik.add(ust);

        persAdi=new JLabel("Adı:");
        persAdi.setBounds(10,40,80,15);
        icerik.add(persAdi);

        ad=new JTextField();
        ad.setBounds(50, 40, 80, 20);
        icerik.add(ad);


        persSoyadi=new JLabel("Soyadı:");
        persSoyadi.setBounds(10, 180, 80, 30);
        icerik.add(persSoyadi);

        soyad=new JTextField();
        soyad.setBounds(200, 40, 100, 30);

        Icon bulPng=new ImageIcon(getClass().getResource("search.png"));
        Icon iptalPng=new ImageIcon(getClass().getResource("cancel.png"));

        bul=new JButton("",bulPng);
        bul.setBounds(20, 120, 40, 40);
        icerik.add(bul);
        iptal=new JButton("",iptalPng);
        iptal.setBounds(90, 120, 40, 40);
        icerik.add(iptal);

    }

public static void main(String[] args){
    persBul app=new persBul();
}

When I debug this code, my JTextField doesn't appear. Only first JLabel can appear and I don't see any other JLabel or JTextField or JButton. My button appears when my cursor is on it. I have to do this project but I haven't created the user interface yet. Can anybody help me?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
umitkilic
  • 327
  • 1
  • 4
  • 17
  • Disregard : icerik.getX(); – umitkilic Jul 07 '14 at 18:45
  • I recommend you to read [`Layout` managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) instead of using a `null` layout. While you're in that, carefully read [The use of multiple JFrames, Good Bad Practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657) especialy: [`CardLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Frakcool Jul 07 '14 at 18:55
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 07 '14 at 23:58

1 Answers1

2

You should avoid using null layouts.

You can achieve the same (or a ver aproximate) UI by using layout managers. As I pointed in this comment.

Here's a MCVE which I recommend you to do in further questions. Also look at How to ask guide in order to make better questions.

I made a new class because trying to modify yours was more work than this. Copy-paste it and understand how it works, then adapt it to your own class.

Edit

You can add separators to add spaces. Also as @Andrew Thompson said in his comment, you can look at How to use multiple layout managers (I did something like that in the example). Here are other options on How to add spaces in swing. Maybe GridBag Layout is the one which seems more like a null layout.

Here's a guide on layouts (link also provided by Andrew).

After reading your question:

Can I use setBounds(x,y,width,height); function with this code? Or What can I do for using this function (setBounds(x,y,width,height);)? Because I want to determine my own self x,y points of the JLabel,JTextField,JButton

You should avoid using setBounds(x,y,width,height); at all costs, because, as also Andrew gave the explanation of why:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components.

Using a null layout could bring some unexpected errors while executing program. It is fine to use null layout (I mean, setBounds(x,y,width,height);, if and only if you're a newbie on swing, but as soon as posible try to start using the Layout Managers instead of the null layout.

What I want to say is: It's not wrong to use null layout only for educational purposes, but even as it is larger and sometimes more complex and requires a bit more thinking, it's better to use them in order to avoid unexpected errors. While you're a student use it, but avoid it if it's for a professional program.

Here's the output of this answer So you can see the use of spaces in swing.

Output example

import javax.swing.*;
import java.awt.*;
class example {
    JFrame frame;
    JPanel panel, hPanel1, hPanel2;
    JLabel label1, label2;
    JTextField field1, field2;
    example() {
        frame = new JFrame("example");

        panel = new JPanel();
        hPanel1 = new JPanel();
        hPanel2 = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        hPanel1.setLayout(new FlowLayout());
        hPanel2.setLayout(new FlowLayout());

        label1 = new JLabel("Label 1");
        label2 = new JLabel("Label 2");
        field1 = new JTextField();
        field2 = new JTextField();

        field1.setColumns(6);
        field2.setColumns(6);

        hPanel1.add(label1);
        hPanel1.add(field1);

        hPanel2.add(label2);
        hPanel2.add(field2);

        panel.add(hPanel1);
        panel.add(hPanel2);

        frame.add(panel);

        frame.setSize(400,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new example();
            }
        });
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • thanks a lot. The problem is solved for now.But I will ask a question. Can I use setBounds(x,y,width,height); function with this code? Or What can I do for using this function (setBounds(x,y,width,height);)? Because I want to determine my own self x,y points of the JLabel,JTextField,JButton – umitkilic Jul 08 '14 at 11:46
  • @umtklc actually that's the null layout I've been talking about. You should avoid it at all costs, if this program is for educational purposes and you're still learning Java Swing then you can use the null layout, otherwise, BoxLayout has an addSeparator method which will make you give more or less space between components. Once I'm on a PC I'll add the link to documentation. Please read the Layout Managers I posted since Swing is designed to work with them and not null layout (i.e. setBounds(...)) – Frakcool Jul 08 '14 at 11:51
  • @umtklc please check edit, if you have any doubt please ask, I'll answer as soon and clear as possible. – Frakcool Jul 08 '14 at 14:19
  • thanks for all answers :) it is a little late :) sorry – umitkilic May 06 '15 at 12:59
  • Yeah, it's ok... hope it helped. – Frakcool May 06 '15 at 15:07