English 中文(简体)
why isn t my Jlabels or Jpanels showing?
原标题:

i ve added a title to my Jframe, and now its blocked everything else, what have I done??

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);
}
最佳回答

Add the label to the panel, don t create a new Panel for it

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

If you are not using a LayoutManager on purpose (and it looks that way) make sure that you set a location and a size for your component.

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

But you should rather remove this line

panel.setLayout(null);

and replace it with something like this:

panel.setLayout(new BorderLayout());

Also, don’t forget to add your pTitle to panel.

Try adding your JPanel pTitle to frame.getContentPane() or to the JPanel panel

Edit

Instead of

frame.add(pTitle);

do:

frame.getContentPanel().add(pTitle);

If this very quick fix doesn t help, stick with the answer you ve already accepted.

You have to set a LayoutManager for your frame:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签