为什么JLabel的实例只显示8行?

时间:2011-12-01 18:13:36

标签: java swing layout jlabel

以下是代码段:

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创建标签)。

Screen shot of the output

帮帮我!!!

1 个答案:

答案 0 :(得分:1)

在您的按钮监听器中,您将使用

设置新CodeLabel的大小
codeLabel.setSize(getPreferredSize());

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

codeLabel.setSize(codeLabel.getPreferredSize());