简体   繁体   English

简单地将JLabel添加到JPanel

[英]Simple Adding a JLabel to JPanel

I have simple problem as I am not much familiar with Java GUI. 我有一个简单的问题,因为我对Java GUI不太熟悉。 I am trying to make visible the JLable on the below code as I find it hard to understand the concept. 我试图在下面的代码中看到JLable,因为我发现很难理解这个概念。 But still label is not visible but the frame do open on run time. 但仍然看不到标签,但框架在运行时打开。

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

You are forgot to add panel p to sample. 您忘了将面板p添加到样本中。 Either use add(p) at the end or just remove panel p cause your sample class is extending JPanel. 要么在最后使用add(p) ,要么只删除面板p因为你的样本类正在扩展JPanel。

Option 1: 选项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);

option 2: 选项2:

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

Also why are you overriding initialization of JLabel? 另外你为什么要重写JLabel的初始化? In your code JLable will always hold value "add JLabel". 在您的代码中,JLable将始终保持值“添加JLabel”。 If you want to see "User Name" then use this add(lab1); 如果要查看“用户名”,请使用此add(lab1); instead of add(lab1 = new JLabel("add JLabel")); 而不是add(lab1 = new JLabel("add JLabel")); .

May be you just require this: 可能你只需要这个:

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

Also constructor can not have return type so remove void from your constructor. 构造函数也不能有返回类型,因此从构造函数中删除void。

The constructor that you are using is not a proper constructor .. A java constructor does not have a return type and the void is extra. 您正在使用的构造函数不是一个正确的构造函数。一个java构造函数没有返回类型,而void是额外的。 When in the main method you are calling new Sample() it is not actually calling your method but the default constructor that exists by default. 当你在main方法中调用new Sample()时,它实际上并没有调用你的方法,而是默认存在的默认构造函数。

Try like this .. 试试这样..

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

also you need to do what @Harry Joy suggested to add the add(p); 你还需要做@Harry Joy建议添加add(p); statement otherwise the panel is still not added. 声明,否则该小组仍未添加。

Note the comments. 请注意评论。

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

Note also, that it is generally not considered a good idea to extend a component unless adding custom functionality. 另请注意,除非添加自定义功能,否则扩展组件通常不被视为好主意。 That would mean (for example) defining new methods for the Sample panel (Which might better be labelled a UserDetails or UserDetailsContainer if I guess correctly where you are going with that code..). 这将意味着(例如)为Sample面板定义新方法(如果我正确地猜测您将使用该代码,那么最好将其标记为UserDetailsUserDetailsContainer )。 Or it might be a Login component.. 或者它可能是一个Login组件..

@SuppressWarnings("serial") public class MyGui extends JFrame{ @SuppressWarnings(“serial”)公共类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);
}

} }

Sample is the Jpanel. 样品是Jpanel。

Sample extends JPanel means you inherit from JPanel. 示例扩展JPanel意味着您从JPanel继承。

just drop JPanel p and all your "p."s 只需删除JPanel p和所有“p。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM