Java 为什么不是';我的Jlabels或Jpanels没有显示吗?
我已经在我的Jframe中添加了一个标题,现在它阻止了所有其他内容,我做了什么Java 为什么不是';我的Jlabels或Jpanels没有显示吗?,java,user-interface,swing,jframe,jpanel,Java,User Interface,Swing,Jframe,Jpanel,我已经在我的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 JLabe
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);
}
尝试将您的JPanel
pTitle
添加到frame.getContentPane()
或JPanel面板
编辑
而不是
frame.add(pTitle);
做:
如果这个快速解决方案没有帮助,请坚持使用您已经接受的答案。如果您不是故意使用组件(看起来是这样的),请确保您为组件设置了位置和大小
pTitle.setLocation(100, 100);
pTitle.setSize(100, 100);
但是你应该删除这一行
panel.setLayout(null);
用这样的东西代替它:
panel.setLayout(new BorderLayout());
另外,不要忘记将您的
pTitle
添加到面板
将标签添加到面板,不要为其创建新面板
lblTitle = new JLabel("Bournemouth University Address Book");
lblTitle.setBounds(100, 0, 400, 20);
panel.add(lblTitle);
必须为框架设置LayoutManager:
frame = new JFrame();
frame.getContentPane().setLayout(FooLayout());
类名应该是大写的;此代码是否在EDT中运行?因为它应该。什么是“它阻止了其他一切”呢?它只显示我的标题栏,其他一切都不显示。你还应该尝试选择一个变量命名约定并坚持它。现在你有所有的小写,驼峰大小写(第一个字母是大写和小写),如果你想在那里用J作为你的Jlabel的缩写,那就不一致了。是的,我知道,但目前我的问题是没有Jlabel或JPanel出现我很抱歉我没有跟上,我是这方面的新手,请按照Erkan的建议阅读一篇教程:你能解释一下吗,我真的很抱歉,但我真的不懂java
frame = new JFrame();
frame.getContentPane().setLayout(FooLayout());