简体   繁体   English

java gui builder custom jlabel

[英]java gui builder custom jlabel

如何在Netbeans的gui生成器中添加自定义JLabel

If you're just looking to see how to create JLabel's dynamically, you're close, although the snippet of code you posted in a comment has several errors in it. 如果您只是想看看如何动态创建JLabel,那么您就快结束了,尽管您在注释中发布的代码段中存在一些错误。 Here's a similar example to your code: 这是与您的代码类似的示例:

import javax.swing.*;

public class Jpl extends JPanel {
    public static final String[] LABEL_TEXT = {"Monday", "Tuesday", 
        "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    public Jpl() {
        for (int i = 0; i < LABEL_TEXT.length; i++) {
            JLabel lbl = new JLabel();
            lbl.setText(LABEL_TEXT[i]);
            add(lbl);
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Jpl");
        frame.getContentPane().add(new Jpl());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

If on the other hand, you're trying to create your own class that extends from JLabel that you can put in the NetBeans GUI-builder palette, then things will be a bit more difficult. 另一方面,如果您尝试创建从JLabel扩展的自己的类,然后将其放入NetBeans GUI-builder面板中,那么事情会有些困难。

edit: but not impossible. 编辑:但并非没有可能。 Custom components can be added via NetBeans Palette Manager. 可以通过NetBeans Palette Manager添加自定义组件。 For more on this, please check this link: Creating GUIs with NetBeans Check the section on Custom Components 有关此的更多信息,请检查以下链接: 使用NetBeans创建GUI检查“自定义组件”部分

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

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