2011-05-13 116 views
4

我有一个简单的问题,因为我对Java GUI不太了解。我试图让下面的代码看到JLable,因为我发现很难理解这个概念。但仍然标签不可见,但框架在运行时打开。简单添加一个JLabel到JPanel

public class Sample extends JPanel { 

    public void Sample() { 
     JPanel p = new JPanel(); 

     JLabel lab1 = new JLabel("User Name", JLabel.LEFT); 
     p.setLayout(new FlowLayout()); 
     p.add(lab1 = new JLabel("add JLabel")); 
    } 

    public static void main(String[] args) { 

     JFrame frame = new JFrame(); 
     frame.getContentPane().add(new Sample()); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(200, 200); 
     frame.setVisible(true); 
    } 
} 

回答

19

你忘了添加面板p品尝。在最后使用add(p)或者仅删除面板p导致您的示例类正在扩展JPanel。

选项1:

JPanel p = new JPanel(); 

    JLabel lab1 = new JLabel("User Name", JLabel.LEFT); 
    p.setLayout(new FlowLayout()); 
    p.add(lab1 = new JLabel("add JLabel")); 
    add(p); 

选项2:

JLabel lab1 = new JLabel("User Name", JLabel.LEFT); 
    setLayout(new FlowLayout()); 
    add(lab1 = new JLabel("add JLabel")); 

的JLabel的还有你为什么要重写初始化?在你的代码中,JLable将始终保持“添加JLabel”的值。如果您想查看“用户名”,请使用add(lab1);而不是add(lab1 = new JLabel("add JLabel"));

可能你只是需要这样的:

JLabel lab1 = new JLabel("User Name", JLabel.LEFT); 
    setLayout(new FlowLayout()); 
    add(lab1); 

而且构造函数不能有返回类型所以从构造函数中删除无效。

+0

为什么你重新分配与''lab1'添加(lab1中的=新的JLabel( “添加的JLabel”) );'而不是'add(lab1);'? – Syjin 2011-05-13 05:43:22

+0

@Tommy:我向OP询问过同样的东西吗? – 2011-05-13 05:44:21

+0

非常感谢你们!在构造函数中放置一个void是一个愚蠢的错误。再次感谢Harry。 – Splitter 2011-05-13 06:27:48

0

样本是Jpanel。

示例扩展JPanel表示您从JPanel继承。

只是下降的JPanel p和所有的 “P。” S

2

您使用的构造函数不是正确的构造函数。java构造函数没有返回类型,void是额外的。当在主方法中调用新的Sample()时,它实际上并不是调用你的方法,而是默认存在的默认构造函数。

尝试这样的..

public Sample() { 
    JPanel p = new JPanel(); 

    JLabel lab1 = new JLabel("User Name", JLabel.LEFT); 
    p.setLayout(new FlowLayout()); 
    p.add(lab1 = new JLabel("add JLabel")); 
} 

还需要做些什么@Harry喜悦建议添加add(p);声明,否则仍不能加入该面板。

+0

+1。 ......... 不错的选择。 – 2011-05-13 06:02:53

2

请注意评论。

import java.awt.*; 
import javax.swing.*; 

public class Sample extends JPanel { 

    public Sample() { 
     // set the layout in the constructor 
     super(new FlowLayout(FlowLayout.LEADING)); 

     // best not to set size OR preferred size! 
     setPreferredSize(new Dimension(200,200)); 

     JLabel lab1 = new JLabel("User Name"); 
     add(lab1); 
    } 

    public static void main(String[] args) { 
     // construct the GUI on the EDT 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame("User Details"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       frame.getContentPane().add(new Sample()); 
       // important! 
       frame.pack(); 

       frame.setVisible(true); 
      } 
     }); 
    } 
} 

还要注意,它一般不被认为是一个好主意,扩展组件,除非添加自定义的功能。这意味着(例如)定义Sample面板的新方法(如果我正确地猜出你要使用该代码的位置,可以更好地标记为UserDetailsUserDetailsContainer ..)。或者它可能是Login组件..

+0

谢谢安德鲁! :)超级(新FlowLayout(FlowLayout.LEADING));对我来说是一件新事物我不知道如何使用super(),现在我知道如何使用它了。再次感谢.. – Splitter 2011-05-13 15:37:15

+0

@Splitter:我做了一个WAG,“FlowLayout.LEADING”是你试图通过在JLabel构造函数中使用JLabel.LEFT约束来实现的效果。我的习惯是在构造函数中提供布局,仅仅是因为我讨厌'浪费'另一行代码。 ;) – 2011-05-13 15:43:00

0

@SuppressWarnings( “串行”) 公共类MyGui扩展的JFrame {

private MyContentPane myContentPane = new MyContentPane(); 

public MyGui(){ 
    super("title"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setContentPane(myContentPane); 

    this.createMenu(); 

    this.pack(); 
    this.setVisible(true);  
} 

private void createMenu(){ 
    JMenuBar myMenuBar = new JMenuBar(); 

    JMenu xMenu = new JMenu("x"); 
    JMenu x = new JMenu("x"); 

    JMenuItem xItem = new JMenuItem("letter"); 

    JMenuItem exitItem = new JMenuItem("exit"); 

    xItem.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0) { 
      myContentPane.xPanel(); 
     } 
    }); 
    xItem.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0) { 
      myContentPane.setxPanel(); 
     } 
    }); 

    exitItem.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    }); 

    displayMenu.add(letterItem); 
    displayMenu.add(colorItem); 

    fileMenu.add(exitItem); 

    myMenuBar.add(displayMenu); 
    myMenuBar.add(fileMenu); 

    this.setJMenuBar(myMenuBar); 
} 

}