JLabel在Jpanel上不可见 [英] JLabel not visible on Jpanel

查看:416
本文介绍了JLabel在Jpanel上不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用JTextAreaJButtonJLabelJPanel的程序.
我要实现的逻辑是:用户在给定的textArea中键入文本,然后单击button.在按钮上,单击我要从textArea中检索文本,并用书面文本创建一个label(如textArea中所示),并将其显示在panel上.
我之前所做的所有操作都是正确的,但是问题出在labelpanel上. labelpanel上不可见.

I'm working on a program in which I'd used a JTextArea, JButton, JLabel and JPanel.
The logic I'd to implement is: user types a text in the given textArea and then click on the button. On button click I'd to retrieve the text from the textArea and create a label with the written text(as in textArea) and show it on the panel.
Everything I'd done previously is correct but the problem is with the label and panel. The label is not visible on the panel.

代码段是:

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.BevelBorder;

/**
 *
 * @author mohammadfaisal
 * http://ermohammadfaisal.blogspot.com
 * http://facebook.com/m.faisal6621
 * 
 */
public class CodeMagnets extends JFrame{
    private JTextArea area4Label;
    private JLabel codeLabel;
    private JButton createButton;
    private JPanel magnet;

    public CodeMagnets(String title) throws HeadlessException {
        super(title);
        magnet=new JPanel(null);
        JScrollPane magnetScroller=new JScrollPane(magnet);
              magnetScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        magnetScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(BorderLayout.CENTER, magnetScroller);
        JPanel inputPanel=new JPanel();
        area4Label=new JTextArea(5, 30);
        area4Label.setTabSize(4);
        JScrollPane textScroller=new JScrollPane(area4Label);
        inputPanel.add(textScroller);
        createButton=new JButton("Create code magnet");
        createButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //codeLabel=new JLabel(area4Label.getText());
                codeLabel=new MyLabel(area4Label.getText());//this is for my new question
                codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
                codeLabel.setLocation(50, 20);
                codeLabel.setVisible(true);
                magnet.add(codeLabel);
                area4Label.setText("");
                //pack();
            }
        });
        inputPanel.add(createButton);
        add(BorderLayout.SOUTH, inputPanel);
        //pack();
        setSize(640, 480);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new CodeMagnets("Code Magnets");
    }
}

推荐答案

在动态添加新组件之后,您需要重新绘画()/validate()您的面板.因此,在此之后:

You need to repaint()/validate() your panle after adding new components in it dynamically. So after this:

magnet.add(codeLabel);

添加此内容:

magnet.validate();

magnet.repaint();


还有一件事,您正在为磁铁面板使用空布局.因此,必须将jLable的setBounds()添加到磁铁面板上.变成了


Also one thing you are using null layout for magnet panel. So must have to setBounds() of jLable before adding it to magnet panel. So it becomes

public void actionPerformed(ActionEvent e) {
    codeLabel=new JLabel(area4Label.getText());
    codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    codeLabel.setBounds(50, 20, 100, 100);
    magnet.add(codeLabel);
    magnet.repaint();
    area4Label.setText("");
}

不建议使用null作为布局,根据您的要求,应使用BorderLayout或GridLayout之类的适当布局,或更简单的FlowLayout.

It is not recommended to use null as layout, you should use proper layout like BorderLayout or GridLayout or even simpler FlowLayout based on your requirement.

@Andrew所说的使用类似这样的内容:

As said by @Andrew use something like:

codeLabel.setSize(codeLabel.getPreferredSize());
codeLabel.setLocation(50, 20);

代替

codeLabel.setBounds(50, 20, 100, 100);

这将解决jLabel的大小问题.

This will solve the size issue of jLabel.

这篇关于JLabel在Jpanel上不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆