2009-11-24 25 views
0

我已經爲我的Jframe添加了一個標題,現在它阻止了其他一切,我做了什麼?爲什麼我的Jlabels或Jpanels不顯示?

public class addressbook 
{ 
public JFrame frame; 
public JButton btnadd, btndelete, btnsave, btnprev, btnnext; 
public JPanel panel, pTitle; 
public JTextField txtname, txtaddress, txthomeno, txtmobno; 
public JLabel JlbName , JlbHtn, JlbMtn, JlbAddress, lblTitle; 
public addressbook() { 


    //sets window 
    frame = new JFrame(); 
    frame.setTitle("Address Book"); 
    frame.setSize(450, 580); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //sets up panel 
    panel = new JPanel(); 
    panel.setLayout(null); 
    frame.getContentPane().add(panel); 
    pTitle = new JPanel(); 
pTitle.setLayout(new FlowLayout(FlowLayout.CENTER)); 
lblTitle = new JLabel("Bournemouth University Address Book"); 
pTitle.add(lblTitle); 
frame.add(pTitle); 



    //Labels 
    JlbName = new JLabel("Name:"); 
    JlbName.setBounds(10, 50, 100, 20); 
    panel.add(JlbName); 

    JlbHtn = new JLabel("Home Number:"); 
    JlbHtn.setBounds(10, 90, 150, 20); 
    panel.add(JlbHtn); 

    JlbMtn = new JLabel("Mobile Number:"); 
    JlbMtn.setBounds(10, 130, 200, 20); 
    panel.add(JlbMtn); 

    JlbAddress = new JLabel("Address:"); 
    JlbAddress.setBounds(10, 170, 250, 20); 
    panel.add(JlbAddress); 

    //Text Fields 
    txtname = new JTextField("Name"); 
    txtname.setBounds(120, 50, 200, 20); 
    panel.add(txtname); 

    txthomeno = new JTextField("Home Number"); 
    txthomeno.setBounds(120, 90, 200, 20); 
    panel.add(txthomeno); 

    txtmobno = new JTextField("Mob Number"); 
    txtmobno.setBounds(120, 130, 200, 20); 
    panel.add(txtmobno); 

    txtaddress = new JTextField("Address"); 
    txtaddress.setBounds(120, 170, 250, 20); 
    panel.add(txtaddress); 

    frame.setVisible(true); 



    //Buttons && Button Functions 
    btnadd = new JButton("Add", new ImageIcon("../files/add.png")); 
    btnadd.setBounds(180, 350, 100, 50); 
    btnadd.addActionListener(new ActionListener() 
    {public void actionPerformed(ActionEvent event) 
    { 

    }}); 
    panel.add(btnadd); 

    btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png")); 
    btndelete.setBounds(180, 450, 100, 50); 
    btndelete.addActionListener(new ActionListener() 
    {public void actionPerformed(ActionEvent event) 
    { 

    }}); 
    panel.add(btndelete); 

    btnsave = new JButton("Save", new ImageIcon("../files/save.png")); 
    btnsave.setBounds(180, 400, 100, 50); 
    btnsave.addActionListener(new ActionListener() 
    {public void actionPerformed(ActionEvent event) 
    { 

    }}); 
    panel.add(btnsave); 

    btnprev = new JButton(new ImageIcon("../files/left.png")); 
    btnprev.setBounds(180, 300, 100, 50); 
    btnprev.addActionListener(new ActionListener() 
    {public void actionPerformed(ActionEvent event) 
    { 

    }}); 
    panel.add(btnprev); 

    btnnext = new JButton(new ImageIcon("../files/right.png")); 
    btnnext.setBounds(180, 250, 100, 50); 
    btnnext.addActionListener(new ActionListener() 
    {public void actionPerformed(ActionEvent event) 
    { 

    }}); 
    panel.add(btnnext); 

    frame.setVisible(true); 
    panel.setVisible(true); 
} 
+0

類名應該是大寫的;此代碼是否在EDT中運行?因爲它應該。 – 2009-11-24 14:03:41

+0

「它阻止了一切」是什麼意思? – 2009-11-24 14:05:12

+0

它只顯示我的標題面板,其他所有內容都沒有顯示 – addiosamigo 2009-11-24 14:07:22

回答

0

標籤添加到面板上,不創建一個新的小組它

lblTitle = new JLabel("Bournemouth University Address Book"); 
lblTitle.setBounds(100, 0, 400, 20); 
panel.add(lblTitle); 
0

嘗試增加您的JPanel pTitleframe.getContentPane()或JPanel的panel

編輯

而不是

frame.add(pTitle); 

做:

frame.getContentPanel().add(pTitle); 

如果這種非常快速的修復不起作用,請堅持您已經接受的答案。

+0

你能解釋一下嗎,我真的很抱歉,但我真的不理解java – addiosamigo 2009-11-24 14:19:38

+0

謝謝,我無法讓它工作所以即時報廢! – addiosamigo 2009-11-24 16:26:04

1

如果您故意沒有使用LayoutManager(並且看起來如此),請確保您爲組件設置了位置和大小。

pTitle.setLocation(100, 100); 
pTitle.setSize(100, 100); 

不過你倒是應該刪除此行

panel.setLayout(null); 

和像這樣的東西替代它:

panel.setLayout(new BorderLayout()); 

另外,不要忘記添加pTitlepanel

+0

對不起,我沒有跟隨,我是這個 – addiosamigo 2009-11-24 14:18:55

+1

的新手然後按照Erkan的建議,並閱讀教程,請:http://java.sun.com/docs/books/tutorial/uiswing/start/compile。 HTML – Bombe 2009-11-24 14:21:42

0

您必須設置一個佈局管理你的框架:

frame = new JFrame();  
frame.getContentPane().setLayout(FooLayout());