Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中形状下的文本_Java_Swing - Fatal编程技术网

java中形状下的文本

java中形状下的文本,java,swing,Java,Swing,我想创建一个可拖动组件,该组件包含一个形状(圆形)和一个文本(JLabel)。但是我不知道jpanel中的形状和文本。我把代码附在下面 潜艇级 package test; import com.businesslense.topology.client.config.Condition; import com.businesslense.topology.client.config.ConfigReader; import com.businesslense.topology.client.c

我想创建一个可拖动组件,该组件包含一个形状(圆形)和一个文本(JLabel)。但是我不知道jpanel中的形状和文本。我把代码附在下面

潜艇级

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();
    }
}
可拖动类

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

}
我想创建一个可拖动组件,该组件包含一个形状(圆形)和一个文本(JLabel)

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

NodeComponent nodeComponent = new NodeComponent(50);
JPanel jPanel = new JPanel();
jPanel.setSize(100, 100);
jPanel.add(nodeComponent);
默认情况下,JPanel使用符合组件“首选大小”的FlowLayout。NodeComponent类应重写
getPreferredSize()
方法以返回实际值。使用JLabel的另一个原因是,它将根据图标和文本确定您的首选大小

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

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

我想在文本周围添加一个类似圆形或多边形的形状。@CrazyProgrammer,您可以在图标下方显示文本。无论如何,我给了你其他一些建议来解决你的问题。希望你理解,我的问题是形状下的文本而不是图标。@CrazyProgrammer,我理解你的问题,并给了你一些建议。我还为您提供了另一种解决方案,即使用带有图标和文本的JLabel来简化代码。
JLabel textLabel = new JLabel(name);
textLabel.setLocation(size, 10);
add(textLabel);