为什么JLabel的实例只显示8行? [英] Why instance of JLabel shows only 8 lines?

查看:103
本文介绍了为什么JLabel的实例只显示8行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码段:

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;

/**
 *
 * @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 MyButtonListener());
    inputPanel.add(createButton);
    add(BorderLayout.SOUTH, inputPanel);
    //pack();
    setSize(640, 480);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class MyButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        codeLabel=new CodeLabel(area4Label.getText());
        codeLabel.setSize(getPreferredSize());
        codeLabel.setLocation(10, 10);
        magnet.add(codeLabel);
        magnet.repaint();
    }
    }

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

..

class CodeLabel extends JLabel{
    int initX;
    int initY;
    int screenX;
    int screenY;
    public CodeLabel(String title){
    super(title);
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e){
            screenX=e.getXOnScreen();
            screenY=e.getYOnScreen();
            initX=getX();
            initY=getY();
        }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e){
            int deltaX=e.getXOnScreen()-screenX;
            int deltaY=e.getYOnScreen()-screenY;
            setLocation(initX+deltaX, initY+deltaY);
        }
    });
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }
}

此处生成的标签有大而宽的边框。我希望它更小,并且必须显示我想要的行数(通过使用html创建标签)。

Here the label generated had large and wide border. I want it to be smaller as well as it must display the number of lines i wanted(by using html to create a label).

帮帮我!!!

推荐答案

在你的按钮监听器中,你要设置新的 CodeLabel 的大小

In your button listener, you are setting the size of your new CodeLabel with

codeLabel.setSize(getPreferredSize());

在此代码的上下文中, getPreferredSize() CodeMagnets 实例上调用。我认为这应该是:

In the context of this code, getPreferredSize() is called on the CodeMagnets instance. I think this should be :

codeLabel.setSize(codeLabel.getPreferredSize());

这篇关于为什么JLabel的实例只显示8行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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