为什么我的 Jlabels 或 Jpanels 没有显示?

发布于 2024-08-12 10:08:56 字数 3136 浏览 3 评论 0原文

我已经在 J​​frame 中添加了一个标题,但现在它阻止了其他所有内容,我做了什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-08-19 10:08:56

如果您没有使用 LayoutManager目的(看起来是这样)请确保为组件设置位置和大小。

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

但您应该删除此行

panel.setLayout(null);

并将其替换为类似以下内容:

panel.setLayout(new BorderLayout());

另外,不要忘记将您的 pTitle 添加到 panel

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.

各自安好 2024-08-19 10:08:56

将标签添加到面板中,不要为其创建新面板

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

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);
冬天的雪花 2024-08-19 10:08:56

尝试将 JPanel pTitle 添加到 frame.getContentPane() 或 JPanel panel

Edit

而不是

frame.add(pTitle);

这样做:

frame.getContentPanel().add(pTitle);

如果这个非常快速的修复没有帮助,请坚持您已经接受的答案。

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.

苍景流年 2024-08-19 10:08:56

您必须为框架设置一个 LayoutManager:

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

You have to set a LayoutManager for your frame:

frame = new JFrame();    
frame.getContentPane().setLayout(FooLayout());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文