【问题标题】:Why instance of JLabel shows only 8 lines? [duplicate]为什么 JLabel 的实例只显示 8 行? [复制]
【发布时间】:2011-12-01 17:31:02
【问题描述】:

那里有确切的重复和好的答案:
Why instance of JLabel shows only 8 lines?

我要一个代码 sn-p:

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


class MyLabel extends JLabel{
    MyLabel(String text){
        super(text);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    public static void main(String...args){
         String text="<html>"
                     +"1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>"
                     +"</html>";//this is the text which is typed in the text box by the user for creating a label
    }
}

这里创建的标签仅正确显示8 行,并且边框比需要的长得足够长(我需要一个label,其边框覆盖其边界但不是这样)。

谁能帮我让更多的线条可见(不建议使用ScrollPane,因为我的意图不是那样)并创建一个与文本完全一致的边框?

请检查这个问题也更清楚我想要什么:

JLabel not visible on Jpanel

【问题讨论】:

标签: java html swing jlabel


【解决方案1】:

根本不要使用 setSize!

改为将组件添加到 JFrame 的 contentPane,在 JFrame 上调用 pack(),然后调用 setVisible(true)。这样,您可以让 Swing 布局管理器决定如何调整所有内容的大小,以便以最佳方式显示组件。

【讨论】:

    【解决方案2】:

    有一段时间没有使用 JLabel,但我相信你可以在它上面调用 setSize - 这会做你喜欢的吗?

    【讨论】:

      【解决方案3】:

      您是否要求边框应围绕MyLabel 组件?
      如果是,请尝试其他布局,例如:frame.setLayout(new FlowLayout());

      如果您希望框架不包含多余的空间,请使用frame.pack();

      【讨论】:

        猜你喜欢
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-02
        • 1970-01-01
        相关资源
        最近更新 更多