由于在框架中要多次使用一个标签条。所以,决定定义一个标签条类(继承JPanel)。如下:
class LabelBar extends JPanel{ //标签条类,主要是显示列表信息

JLabel proc_id =new JLabel("进程标识符");
JLabel proc_name = new JLabel("进程名");
JLabel proc_priority = new JLabel("优先级");
JLabel remained_time = new JLabel("剩余时间");

public LabelBar(){


super();
setLayout(new BorderLayout());//设置面板的布局管理器为边框布局管理器

JPanel p = new JPanel();
p.setLayout(null);
proc_id.setBounds(0, 0, 75, 25);
proc_name.setBounds(75, 0, 75, 25);
proc_priority.setBounds(150, 0, 75, 25);
remained_time.setBounds(225, 0, 75, 25);

p.add(proc_id);
p.add(proc_name);
p.add(proc_priority);
p.add(remained_time);

add(p,"Center");



}

}

在主框架类中使用如下:。。
Container cp = getContentPane();
。。
cp.add(new LabelBar());

可是,其他部分都能显示,就是这标签条无法显示。请大家帮忙看一下》

解决方案 »

  1.   


    在主框架类中使用如下: 。。 
    Container cp = getContentPane(); 
    。。 
    cp.add(new LabelBar()); 
    可是,其他部分都能显示,就是这标签条无法显示。请大家帮忙看一下》

    看看你在主框架中的布局方式,如果是setLayout(null); 
    那这个要靠你自己布局了是不是被挡住了?,否则看看布局方式问题。
      

  2.   

    你可以先不要把你的代码放到框架中使用 先简单测试下 如果能出来一个LabelBar实例 再放到框架中 避免把问题搞复杂了
      

  3.   

    帖完整代码,我按照你说的跑起来可以
    package test;import java.awt.BorderLayout;
    import java.awt.Dimension;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class TestFrame2 extends JFrame { private static final long serialVersionUID = -362233428598274611L; public TestFrame2() {
    init();
    } private void init() {
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new LabelBar(), BorderLayout.CENTER);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(new Dimension(300, 200));
    this.setVisible(true);
    } public static void main(String[] args) {
    new TestFrame2();
    }
    }class LabelBar extends JPanel { // 标签条类,主要是显示列表信息 private static final long serialVersionUID = -4606974330336114587L; JLabel proc_id = new JLabel("进程标识符"); JLabel proc_name = new JLabel("进程名"); JLabel proc_priority = new JLabel("优先级"); JLabel remained_time = new JLabel("剩余时间"); public LabelBar() { super();
    setLayout(new BorderLayout());// 设置面板的布局管理器为边框布局管理器 JPanel p = new JPanel();
    p.setLayout(null);
    proc_id.setBounds(0, 0, 75, 25);
    proc_name.setBounds(75, 0, 75, 25);
    proc_priority.setBounds(150, 0, 75, 25);
    remained_time.setBounds(225, 0, 75, 25); p.add(proc_id);
    p.add(proc_name);
    p.add(proc_priority);
    p.add(remained_time); add(p, "Center"); }}
      

  4.   

    单从这点代码看没问题啊,我对应写了一下也没问题,你最好把代码全贴出来。package impl;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;public class Test extends JFrame { public Test() {
    this.getContentPane().add(new LabelBar(), BorderLayout.CENTER);
    } /**
     * @param args
     */
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() { @Override
    public void run() {
    Test f = new Test();
    f.setSize(500, 500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    } });
    }}class LabelBar extends JPanel { // 标签条类,主要是显示列表信息 JLabel proc_id = new JLabel("进程标识符");
    JLabel proc_name = new JLabel("进程名");
    JLabel proc_priority = new JLabel("优先级");
    JLabel remained_time = new JLabel("剩余时间"); public LabelBar() { super();
    setLayout(new BorderLayout());// 设置面板的布局管理器为边框布局管理器 JPanel p = new JPanel();
    p.setLayout(null);
    proc_id.setBounds(0, 0, 75, 25);
    proc_name.setBounds(75, 0, 75, 25);
    proc_priority.setBounds(150, 0, 75, 25);
    remained_time.setBounds(225, 0, 75, 25); p.add(proc_id);
    p.add(proc_name);
    p.add(proc_priority);
    p.add(remained_time); add(p, "Center");
    }
    }
      

  5.   

    主框架代码:public class ProcFrame extends JFrame{

    JLabel ready = new JLabel("就绪队列");
    JLabel block = new JLabel("阻塞队列");
    JLabel finish = new JLabel("完成队列");
    JLabel executing = new JLabel("正在执行的进程");
    JLabel pid = new JLabel("进程标识符");
    JLabel pname = new JLabel("进程名");


    JTextArea readytext = new JTextArea();
    JTextArea blocktext = new JTextArea();
    JTextArea finishtext = new JTextArea();
    JTextArea executingtext = new JTextArea();

    JTextField pidtext = new JTextField();
    JTextField pnametext = new JTextField();

    JButton create = new JButton("创建进程");
    JButton request = new JButton("申请I/O");
    JButton wakeup = new JButton("唤醒进程");
    JButton dispatch = new JButton("开始调度");
    JButton reset = new JButton("重置系统");


    //调度类
    Dispatcher dispatcher = new Dispatcher();

    public ProcFrame(String s){
    super(s);
    Container cp = getContentPane();
    cp.setLayout(null);
    ready.setBounds(10, 10, 100, 25);
            readytext.setBounds(10, 60, 300, 50);

    block.setBounds(10, 110, 100, 25);
            blocktext.setBounds(10, 160, 300, 50);

    finish.setBounds(10, 210, 100, 25);
    finishtext.setBounds(10, 260, 300, 50);

    executing.setBounds(10, 310, 150, 25);
    executingtext.setBounds(10, 360, 300, 20);

    pid.setBounds(350, 35, 100, 25);
    pidtext.setBounds(350, 65, 100, 25);

    pname.setBounds(350, 100, 100, 25);
    pnametext.setBounds(350, 130, 100, 25);

    create.setBounds(350, 200, 100, 30);
    request.setBounds(350, 240, 100, 30);
    wakeup.setBounds(350, 280, 100, 30);
    dispatch.setBounds(350, 320, 100, 30);
    reset.setBounds(350, 360, 100, 30);

    cp.add(ready);
    cp.add(new LabelBar());
    cp.add(readytext);
    cp.add(block);
    cp.add(new LabelBar());
    cp.add(blocktext);
    cp.add(finish);
    cp.add(new LabelBar());
    cp.add(finishtext);
    cp.add(executing);
    cp.add(new LabelBar());
    cp.add(executingtext);
    cp.add(pid);
    cp.add(pidtext);
    cp.add(pname);
    cp.add(pnametext);
    cp.add(create);
    cp.add(request);
    cp.add(wakeup);
    cp.add(dispatch);
    cp.add(reset);

    //下面为各个按钮注册时间监听器并定义处理事件的方法
    create.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){

    }
    });

    }

    public static void main(String args[]){
    ProcFrame pf = new ProcFrame("进程状态转换模拟系统");
    pf.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    });
    pf.setSize(500, 450);
    pf.setVisible(true);

    }



    }
      

  6.   

    你的layout是个null布局
    应该是你的各个控件的setBounds没设置好,把你的LabelBar盖住了用插入代码,选择java
      

  7.   

    cp.add(new LabelBar()); 中的new LabelBar()没有调用setBounds()方法。贴代码的时候先点击上面#图标,也可以看论坛中有一篇叫教你贴代码的帖子