Why instance of JLabel shows only 8 lines?

房东的猫 提交于 2019-12-04 06:04:13

问题


Here's the code snippet:

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

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).

Help me out!!!


回答1:


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

codeLabel.setSize(getPreferredSize());

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

codeLabel.setSize(codeLabel.getPreferredSize());


来源:https://stackoverflow.com/questions/8347014/why-instance-of-jlabel-shows-only-8-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!