java中Shape下的文本

时间:2014-04-18 16:36:13

标签: java swing

我想创建一个可拖动的组件,其下包含一个形状(圆圈)和一个文本(JLabel)。但我没有得到jpanel中的形状和文字。我附上了以下代码。

SubMapViewer类

package test;

import com.businesslense.topology.client.config.Condition;
import com.businesslense.topology.client.config.ConfigReader;
import com.businesslense.topology.client.config.NodeConfig;
import com.businesslense.topology.client.config.Parameter;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class SubMapViewer extends JLayeredPane {

    @Override
    public void paint(Graphics grphcs) {
        super.paint(grphcs); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Paint called");
    }

    public void showMap() {
        Runnable gui = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                JFrame f = new JFrame("Draggable Components");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300, 300);
                f.setLocationRelativeTo(null);
                JLayeredPane panel = new SubMapViewer();
                panel.setLayout(null);

                NodeComponent nodeComponent = new NodeComponent(50);
                JPanel jPanel = new JPanel();
                jPanel.setSize(100, 100);

                jPanel.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK, 2));
                jPanel.add(nodeComponent);
                jPanel.setVisible(true);
                Draggable d2 = new Draggable(jPanel, 200, 150);

                panel.add(d2);
                f.add(panel);
                f.setVisible(true);
            }
        };
        //GUI must start on EventDispatchThread:  
        SwingUtilities.invokeLater(gui);
    }

    Properties getDisplayProperties(Properties properties) {
        //List<String> menuList = new ArrayList<>();
        List<com.businesslense.topology.client.config.NodeConfig> nodeConfigs = ConfigReader.getData().getNodeConfig();
        outer:
        for (Iterator<NodeConfig> it = nodeConfigs.iterator(); it.hasNext();) {
            NodeConfig nodeConfig = it.next();
            List<Condition> conditions = nodeConfig.getCondtion();
            boolean match = false;
            for (Iterator<Condition> it1 = conditions.iterator(); it1.hasNext();) {
                Condition condition = it1.next();
                if (condition.getValue().equalsIgnoreCase("" + properties.get(condition.getName()))) {
                    match = true;
                } else {
                    continue outer;
                }
            }
            if (match) {

                Properties displayProperties = new Properties();
                for (Iterator<Parameter> it1 = nodeConfig.getParameter().iterator(); it1.hasNext();) {
                    Parameter parameter = it1.next();
                    displayProperties.put(parameter.getName(), parameter.getValue());
                }
                return displayProperties;
            }
        }
        return null;
    }

    public static void main(String[] arv) {
        SubMapViewer subMapViewer = new SubMapViewer();
        subMapViewer.showMap();
    }
}

NodeComponent类

package test;

import com.businesslense.topology.client.marker.DefaultNodeComponent;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.log4j.Logger;

public class NodeComponent extends JPanel {

    private String name;
    private Integer size;
    static Logger logger = Logger.getLogger(DefaultNodeComponent.class.getName());

    public NodeComponent(Integer size) {
        setLayout(null);

        setSize(size  , size * 2);
        this.size = size;
        this.name = "ICON NAME";
        JLabel textLabel = new JLabel(name);
        textLabel.setLocation(size, 10);
        add(textLabel);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        int shapePaddingVal = (size * 5) / 100;
        int shapeRadius = (size * 90) / 100;
        Color shapeColor = Color.BLACK;
        g2d.setColor(shapeColor);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawOval(shapePaddingVal, shapePaddingVal, shapeRadius/2, shapeRadius/2);
        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(0, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    }
}

Draggable Class

package test;

/* 
 * Draggable.java 
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import javax.swing.border.Border;
import javax.swing.event.MouseInputAdapter;

public class Draggable extends JComponent {

    private Point pointPressed;
    private JComponent draggable;

    public Draggable(final JComponent component, final int x, final int y) {
        draggable = component;
        //        draggable.setCursor(draggable.getCursor());  
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        setLocation(x, y);
        setSize(component.getPreferredSize());
        setLayout(new BorderLayout());
        add(component);
        MouseInputAdapter mouseAdapter = new MouseHandler();
        addMouseMotionListener(mouseAdapter);
        addMouseListener(mouseAdapter);
    }

    @Override
    public void setBorder(final Border border) {
        super.setBorder(border);
        if (border != null) {
            Dimension size = draggable.getPreferredSize();
            Insets insets = border.getBorderInsets(this);
            size.width += (insets.left + insets.right + 5);
            size.height += (insets.top + insets.bottom);
            setSize(size);
        }
    }

    private class MouseHandler extends MouseInputAdapter {

        @Override
        public void mouseDragged(final MouseEvent e) {
            Point pointDragged = e.getPoint();
            Point loc = getLocation();
            loc.translate(pointDragged.x - pointPressed.x,
                    pointDragged.y - pointPressed.y);
            setLocation(loc);
        }

        @Override
        public void mousePressed(final MouseEvent e) {
            pointPressed = e.getPoint();
        }
    }

}

1 个答案:

答案 0 :(得分:1)

  

我想在它下面创建一个包含形状(圆圈)和文字(JLabel)的可拖动组件

为什么不使用带有图标和文字的JLabel?

NodeComponent nodeComponent = new NodeComponent(50);
JPanel jPanel = new JPanel();
jPanel.setSize(100, 100);
jPanel.add(nodeComponent);

默认情况下,JPanel使用FlowLayout,它尊重组件&#34;首选大小&#34;。您的NodeComponent类应覆盖getPreferredSize()方法以返回实际值。使用JLabel的另一个原因,因为它将根据图标和文本确定您的首选大小。

JLabel textLabel = new JLabel(name);
textLabel.setLocation(size, 10);
add(textLabel);

您正在将标签添加到使用空布局的组件中。由于大小为零,因此不会显示文本。