JAVA在单选按钮之间画线,连接两个以上

JAVA在单选按钮之间画线,连接两个以上,java,Java,我加入按钮,但我不能加入超过两个。 这就是我所拥有的(最后的代码) 这就是我想要的(用画图编辑图像) 可以绑定到多个点的点谢谢我使用GlassedPane 我的代码: package interfaz; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; imp

我加入按钮,但我不能加入超过两个。 这就是我所拥有的(最后的代码)

这就是我想要的(用画图编辑图像) 可以绑定到多个点的点谢谢我使用GlassedPane

我的代码:

        package interfaz;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.HashMap;
    import java.util.Map;

    /**
     * @see http://stackoverflow.com/a/12389479/909085
     */
    public class ComponentLinkerTest extends JComponent {

        private Map<JComponent, JComponent> linked;
        private static ComponentLinkerTest linker;
        private static JRadioButton last = null;

        public static void main(String[] args) {
            setupLookAndFeel();
            JFrame frame = new JFrame();
            JPanel general = new javax.swing.JPanel();
            JPanel botones = new javax.swing.JPanel();
            general.setLayout(new javax.swing.BoxLayout(general, javax.swing.BoxLayout.LINE_AXIS));
            linker = new ComponentLinkerTest();
            frame.setGlassPane(linker);
            linker.setVisible(true);

            JPanel content = new JPanel();
            content.setLayout(new GridLayout(10, 10, 5, 5));
            content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            general.add(content);
            JButton aceptar = new JButton("Generar ruta");
            aceptar.addActionListener((ActionEvent e) -> {
                System.out.println(linker.toString());
            });
            botones.add(aceptar);
            general.add(botones);
            frame.add(general);
            int a=0;
            int b= 0;
            //frame.add ( content );
            for (int i = 0; i < 100; i++) {
                if(b==10){
                    a++;
                    b=0;
                }
                String pos = "";
                pos=a+","+b;
                JRadioButton button = new JRadioButton();
                button.setName(pos);
                //final JButton button = new JButton ();
                button.addActionListener((ActionEvent e) -> {
                    button.setSelected(true);
                    link(button);
                });
                content.add(button);
                b++;
            }

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        public ComponentLinkerTest() {
            super();
            linked = new HashMap<JComponent, JComponent>();
        }

        public void link(JComponent c1, JComponent c2) {
            linked.put(c1, c2);
            repaint();
        }

        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g2d.setPaint(Color.BLACK);
            for (JComponent c1 : linked.keySet()) {
                Point p1 = getRectCenter(getBoundsInWindow(c1));
                Point p2 = getRectCenter(getBoundsInWindow(linked.get(c1)));
                g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }

        private Point getRectCenter(Rectangle rect) {
            return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
        }

        private Rectangle getBoundsInWindow(Component component) {
            return getRelativeBounds(component, getRootPaneAncestor(component));
        }

        private Rectangle getRelativeBounds(Component component, Component relativeTo) {
            return new Rectangle(getRelativeLocation(component, relativeTo),
                    component.getSize());
        }

        private Point getRelativeLocation(Component component, Component relativeTo) {
            Point los = component.getLocationOnScreen();
            Point rt = relativeTo.getLocationOnScreen();
            return new Point(los.x - rt.x, los.y - rt.y);
        }

        private JRootPane getRootPaneAncestor(Component c) {
            for (Container p = c.getParent(); p != null; p = p.getParent()) {
                if (p instanceof JRootPane) {
                    return (JRootPane) p;
                }
            }
            return null;
        }

        public boolean contains(int x, int y) {
            return false;
        }

        private static void link(JRadioButton button) {
            System.out.println(""+button.getName());
            if (last == null) {
                last = button;
            } else {
                linker.link(last, button);
                last = null;
            }
        }

        private static void setupLookAndFeel() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
        }
    }
package-interfaz;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.HashMap;
导入java.util.Map;
/**
*@见http://stackoverflow.com/a/12389479/909085
*/
公共类组件LinkerTest扩展了JComponent{
私人地图连接;
私有静态组件链接器;
私有静态JRadioButton last=null;
公共静态void main(字符串[]args){
setupLookAndFeel();
JFrame=新JFrame();
jpanelgeneral=newjavax.swing.JPanel();
JPanel-botones=newjavax.swing.JPanel();
setLayout(新的javax.swing.BoxLayout(general,javax.swing.BoxLayout.LINE_轴));
链接器=新组件LinkerTest();
frame.setGlassPane(链接器);
linker.setVisible(true);
JPanel content=新的JPanel();
content.setLayout(新的GridLayout(10,10,5,5));
content.setboorder(BorderFactory.createEmptyBorder(5,5,5,5));
总则.增加(内容);
JButton aceptar=新JButton(“Generar ruta”);
aceptar.addActionListener((ActionEvent e)->{
System.out.println(linker.toString());
});
添加(aceptar);
一般。添加(两个);
框架.增加(一般);
int a=0;
int b=0;
//frame.add(内容);
对于(int i=0;i<100;i++){
如果(b==10){
a++;
b=0;
}
字符串pos=“”;
位置=a+“,”+b;
JRadioButton按钮=新的JRadioButton();
按钮。设置名称(pos);
//最终JButton按钮=新JButton();
addActionListener((ActionEvent e)->{
按钮。设置选择(真);
链接(按钮);
});
内容。添加(按钮);
b++;
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共组件LinkerTest(){
超级();
linked=新HashMap();
}
公共无效链接(JC组件c1、JC组件c2){
连接。放置(c1,c2);
重新油漆();
}
受保护组件(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(renderingHits.KEY\u抗锯齿,renderingHits.VALUE\u抗锯齿\u开启);
g2d.setPaint(颜色:黑色);
对于(JComponentC1:linked.keySet()){
点p1=getRectCenter(getBoundsInWindow(c1));
点p2=getRectCenter(getBoundsInWindow(linked.get(c1));
g2d.绘制线(p1.x、p1.y、p2.x、p2.y);
}
}
专用点getRectCenter(矩形rect){
返回新点(矩形x+矩形宽度/2,矩形y+矩形高度/2);
}
私有矩形getBoundsInWindow(组件){
返回getRelativeBounds(组件,getRootPaneAncestor(组件));
}
私有矩形getRelativeBounds(组件组件,组件相对){
返回新矩形(getRelativeLocation(component,relativeTo),
getSize());
}
私有点getRelativeLocation(组件组件,组件相对){
点服务水平=组件。getLocationOnScreen();
点rt=相对于屏幕上的GetLocationOn();
返回新点(los.x-rt.x,los.y-rt.y);
}
专用JRootPane getRootPaneAncestor(组件c){
对于(容器p=c.getParent();p!=null;p=p.getParent()){
if(JRootPane的p实例){
返回(JRootPane)p;
}
}
返回null;
}
公共布尔包含(int x,int y){
返回false;
}
专用静态无效链接(JRadioButton按钮){
System.out.println(“+button.getName());
if(last==null){
最后=按钮;
}否则{
linker.link(最后一个按钮);
last=null;
}
}
私有静态void setupLookAndFeel(){
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(实例化异常e){
e、 printStackTrace();
}捕获(非法访问例外e){
e、 printStackTrace();
}捕获(无支持的LookandFeelException e){
e、 printStackTrace();
}
}
}
专用静态无效链接(JRadioButton按钮){

我在这里发现一个错误。你应该最后重置到当前按钮,但你把它设为空 {

专用静态无效链接(JRadioButton按钮){

我在这里发现一个错误。你应该最后重置到当前按钮,但你把它设为空 {


链接的
类型是
Map
。但您需要的是
组件对的
列表。
以下是一个解决方案:
[我已经调整了一些代码以与JDK 1.7兼容。我的笔记本电脑上没有1.8。我想你可以重新调整它。]

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @see http://stackoverflow.com/a/12389479/909085
 */
public class ComponentLinkerTest extends JComponent {

    private List<Map.Entry<Component, Component>> linked;
    private static ComponentLinkerTest linker;
    private static JRadioButton last = null;

    public static void main(String[] args) {
            setupLookAndFeel();
            JFrame frame = new JFrame();
            JPanel general = new javax.swing.JPanel();
            JPanel botones = new javax.swing.JPanel();
            general.setLayout(new javax.swing.BoxLayout(general, javax.swing.BoxLayout.LINE_AXIS));
            linker = new ComponentLinkerTest();
            frame.setGlassPane(linker);
            linker.setVisible(true);

            JPanel content = new JPanel();
            content.setLayout(new GridLayout(10, 10, 5, 5));
            content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            general.add(content);
            JButton aceptar = new JButton("Generar ruta");
            aceptar.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                       System.out.println(linker.toString());
                                    }
            });
            botones.add(aceptar);
            general.add(botones);
            frame.add(general);
            int a=0;
            int b= 0;
            //frame.add ( content );
            for (int i = 0; i < 100; i++) {
                if(b==10){
                    a++;
                    b=0;
                }
                String pos = "";
                pos=a+","+b;
                final JRadioButton button = new JRadioButton();
                button.setName(pos);
                //final JButton button = new JButton ();
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button.setSelected(true);
                        link(button);
                    }
                });
                content.add(button);
                b++;
            }

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    public ComponentLinkerTest() {
        super();
        linked = new ArrayList<Map.Entry<Component,Component>>();
    }

    public void link(JComponent c1, JComponent c2) {
        Map.Entry entry = new AbstractMap.SimpleEntry<>(c1, c2);
        linked.add(entry);
        repaint();
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setPaint(Color.BLACK);
        for (Map.Entry entry : linked) {
            Point p1 = getRectCenter(getBoundsInWindow((Component)entry.getKey()));
            Point p2 = getRectCenter(getBoundsInWindow((Component)entry.getValue()));
            g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }

    private Point getRectCenter(Rectangle rect) {
        return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
    }

    private Rectangle getBoundsInWindow(Component component) {
        return getRelativeBounds(component, getRootPaneAncestor(component));
    }

    private Rectangle getRelativeBounds(Component component,
            Component relativeTo) {
        return new Rectangle(getRelativeLocation(component, relativeTo),
                component.getSize());
    }

    private Point getRelativeLocation(Component component, Component relativeTo) {
        Point los = component.getLocationOnScreen();
        Point rt = relativeTo.getLocationOnScreen();
        return new Point(los.x - rt.x, los.y - rt.y);
    }

    private JRootPane getRootPaneAncestor(Component c) {
        for (Container p = c.getParent(); p != null; p = p.getParent()) {
            if (p instanceof JRootPane) {
                return (JRootPane) p;
            }
        }
        return null;
    }

    public boolean contains(int x, int y) {
        return false;
    }

    private static void link(JRadioButton button) {
        System.out.println("" + button.getName());
        if (last == null) {
            last = button;
        } else {
            linker.link(last, button);
            last = null;
        }
    }

    private static void setupLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }
}
导入java.awt.Color;
导入java.awt.Component;
导入java.awt
linker.link(last, button);

 last = button;

 //and not 

 last=null; 
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @see http://stackoverflow.com/a/12389479/909085
 */
public class ComponentLinkerTest extends JComponent {

    private List<Map.Entry<Component, Component>> linked;
    private static ComponentLinkerTest linker;
    private static JRadioButton last = null;

    public static void main(String[] args) {
            setupLookAndFeel();
            JFrame frame = new JFrame();
            JPanel general = new javax.swing.JPanel();
            JPanel botones = new javax.swing.JPanel();
            general.setLayout(new javax.swing.BoxLayout(general, javax.swing.BoxLayout.LINE_AXIS));
            linker = new ComponentLinkerTest();
            frame.setGlassPane(linker);
            linker.setVisible(true);

            JPanel content = new JPanel();
            content.setLayout(new GridLayout(10, 10, 5, 5));
            content.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            general.add(content);
            JButton aceptar = new JButton("Generar ruta");
            aceptar.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                       System.out.println(linker.toString());
                                    }
            });
            botones.add(aceptar);
            general.add(botones);
            frame.add(general);
            int a=0;
            int b= 0;
            //frame.add ( content );
            for (int i = 0; i < 100; i++) {
                if(b==10){
                    a++;
                    b=0;
                }
                String pos = "";
                pos=a+","+b;
                final JRadioButton button = new JRadioButton();
                button.setName(pos);
                //final JButton button = new JButton ();
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        button.setSelected(true);
                        link(button);
                    }
                });
                content.add(button);
                b++;
            }

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    public ComponentLinkerTest() {
        super();
        linked = new ArrayList<Map.Entry<Component,Component>>();
    }

    public void link(JComponent c1, JComponent c2) {
        Map.Entry entry = new AbstractMap.SimpleEntry<>(c1, c2);
        linked.add(entry);
        repaint();
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setPaint(Color.BLACK);
        for (Map.Entry entry : linked) {
            Point p1 = getRectCenter(getBoundsInWindow((Component)entry.getKey()));
            Point p2 = getRectCenter(getBoundsInWindow((Component)entry.getValue()));
            g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }

    private Point getRectCenter(Rectangle rect) {
        return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
    }

    private Rectangle getBoundsInWindow(Component component) {
        return getRelativeBounds(component, getRootPaneAncestor(component));
    }

    private Rectangle getRelativeBounds(Component component,
            Component relativeTo) {
        return new Rectangle(getRelativeLocation(component, relativeTo),
                component.getSize());
    }

    private Point getRelativeLocation(Component component, Component relativeTo) {
        Point los = component.getLocationOnScreen();
        Point rt = relativeTo.getLocationOnScreen();
        return new Point(los.x - rt.x, los.y - rt.y);
    }

    private JRootPane getRootPaneAncestor(Component c) {
        for (Container p = c.getParent(); p != null; p = p.getParent()) {
            if (p instanceof JRootPane) {
                return (JRootPane) p;
            }
        }
        return null;
    }

    public boolean contains(int x, int y) {
        return false;
    }

    private static void link(JRadioButton button) {
        System.out.println("" + button.getName());
        if (last == null) {
            last = button;
        } else {
            linker.link(last, button);
            last = null;
        }
    }

    private static void setupLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }
}