2014-07-07 58 views
-2

這裏是我的Java代碼:我的JTextField不會出現

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(); 
} 

當我調試代碼,我JTextField不會出現。只有第一個JLabel可以出現,我沒有看到任何其他JLabelJTextFieldJButton。當我的光標在上面時,我的按鈕就會出現。我必須做這個項目,但我還沒有創建用戶界面。有誰能夠幫助我?

+0

否認:icerik。的getX(); – umitkilic

+0

我建議您閱讀['Layout' managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html),而不是使用'null'佈局。雖然你在那,仔細閱讀[使用多個JFrames,良好的壞做法](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

+0

Java GUI可能需要在多種平臺上工作,使用不同的屏幕分辨率並使用不同的PLAF。因此,它們不利於組件的準確放置。爲了組織強大的GUI,請改爲[使用佈局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)或[它們的組合](http:// stackoverflow.com/a/5630271/418556),以及[white space]的佈局填充和邊框(http://stackoverflow.com/q/17874717/418556)。 –

回答

2

您應該避免使用null佈局。

可以實現通過使用layout經理相同(或版本aproximate)UI。正如我指出的this comment

這裏有一個MCVE我建議你在進一步的問題做。也請看How to ask guide以便提出更好的問題。

我做了一個新班,因爲試圖修改你的是比這更多的工作。複製粘貼並理解它是如何工作的,然後將其適用於您自己的課程。

編輯

您可以添加separators加空格。也作爲@Andrew Thompson在他的評論中說,你可以看看How to use multiple layout managers(我做了這樣的例子)。以下是How to add spaces in swing上的其他選項。也許GridBag Layout是更像是null佈局的那個。

這裏有一個佈局guide(鏈接也由安德魯提供)。

閱讀您的問題後:

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

你應該不惜一切代價避免使用setBounds(x,y,width,height);,因爲,也安德魯放棄的原因的解釋:

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.

使用null佈局會在執行程序時帶來一些意外的錯誤。這是蠻好用的null佈局(我的意思是,setBounds(x,y,width,height);當且僅當你在揮杆新手,但只要更多鈔票嘗試啓動null佈局使用佈局管理器來代替。

什麼我想說的是:這是沒有錯的使用null佈局只爲教育目的,但即使是較大的,有時比較複雜,需要多一點的思考,這是更好,以避免發生意外錯誤使用它們雖然。你是學生使用它,但避免它,如果它是一個專業的方案。

這裏是輸出所以你可以看到在鞦韆中使用空間。

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(); 
      } 
     }); 
    } 
} 
+0

非常感謝。這個問題現在已經解決了,但我會問一個問題。我可以使用setBounds(x,y,width,height);函數與此代碼?或者我能做些什麼來使用這個函數(setBounds(x,y,width,height);)?因爲我想確定自己的x,y點JLabel,JTextField,JButton – umitkilic

+0

@umtklc實際上這是我一直在討論的空佈局。您應該不惜一切代價避免它,如果此程序僅用於教育目的,並且您仍然在學習Java Swing,那麼您可以使用空佈局,否則,BoxLayout具有addSeparator方法,這將使您在組件之間提供更多或更少的空間。一旦我在PC上,我會添加鏈接到文檔。請閱讀我發佈的佈局管理器,因爲Swing旨在與它們配合使用而不是null佈局(即setBounds(...)) – Frakcool

+0

@umtklc請檢查編輯,如果您有任何疑問,請詢問,我會盡快回復並儘可能清楚。 – Frakcool