gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:09:23 25 4
gpt4 key购买 nike

我已经向我的 Jframe 添加了一个标题,但现在它阻止了其他所有内容,我做了什么?

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

最佳答案

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

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

但是你应该删除这一行

panel.setLayout(null);

并将其替换为如下内容:

panel.setLayout(new BorderLayout());

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

关于java - 为什么我的 Jlabels 或 Jpanels 没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1790228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com