Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        5年前关闭。
                                                                                            
                
        
目前,我正在处理与Java中的GUI制作相关的事情,我想尝试制作如下所示的GUI:



您如何建议这样做?

首先,尽管我将类称为“我的框架”,但它将包含一个数组,该数组保存每个多边形的点(因此每个多边形将是一个单独的框架)。

但是,在重新考虑一下这些三角形的用途之后,我想让它们成为按钮,每次按下其中一个按钮都会更改八边形内部的内容,因此,更像是三角形属于按钮,而八角形本身就是内容窗格,那么你们如何建议我实现这一目标?

另外一个相关的问题是,我想使这些三角形随八角形一起移动,如果用鼠标拖动八角形,三角形也会随之移动,所以我应该将它们作为八角形框架的父级吗?

我想问的另一件事是,由于我将框架设置为未装饰以更改其形状,因此我取消了调整框架大小的选项,并且取消了图标化和关闭按钮的显示,因此如何添加这些元素回来了,我在网上搜索了一下,但没有找到任何解释。

编辑对话课:

package rt;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;

public class MyDialog extends JDialog {

private int pointX;
private int pointY;
JLabel octagonLabel;
JLabel triangleAboutLabel;
MyMouseInputAdapter mmia;

public MyDialog() {
    octagonLabel = createOctagonLabel();
    triangleAboutLabel = createTriangleAboutLabel();
    JTextField textField = createTextField();
    setContentPane(octagonLabel);
    add(textField);
    setUndecorated(true);
    setBackground(new Color(0, 0, 0, 0));
    pack();
    setLocationRelativeTo(null);
    mmia = new MyMouseInputAdapter();
    addMouseListener(mmia);
    addMouseMotionListener(mmia);
    setVisible(true);
}

private JTextField createTextField() {
    final JTextField field = new JTextField(20);
    field.setText("Type \"exit\" to terminate.");
    field.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = field.getText();
            if ("exit".equalsIgnoreCase(text)) {
                System.exit(0);
            }
        }
    });
    return field;
}

private JLabel createOctagonLabel() {
    Image image = null;
    try {
        image = ImageIO.read(Class.class.getResourceAsStream("/resources/gui/octagon.png"));
    } catch (IOException ex) {
        Logger.getLogger(MyDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
    JLabel label = new JLabel(new ImageIcon(image));
    label.setLayout(new GridBagLayout());
    return label;
}

private JLabel createTriangleAboutLabel() {
    Image image = null;
    try {
        image = ImageIO.read(Class.class.getResourceAsStream("/resources/gui/triangle_about.png"));
    } catch (IOException ex) {
        Logger.getLogger(MyDialog.class.getName()).log(Level.SEVERE, null, ex);
    }
    JLabel label = new JLabel(new ImageIcon(image));
    label.setLayout(new GridBagLayout());
    label.setLocation(octagonLabel.getLocation().x - 32, octagonLabel.getLocation().y - 32);

    return label;
}

private class MyMouseInputAdapter extends MouseInputAdapter {
    @Override
    public void mouseDragged(MouseEvent e) {
        MyDialog.this.setLocation(MyDialog.this.getLocation().x + e.getX() - pointX, MyDialog.this.getLocation().y + e.getY() - pointY);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        pointX = e.getX();
        pointY = e.getY();
    }
}


}

最新编辑:
@peeskillet很抱歉在过去几天没有在这里,家人在医院..我试图实现您所说的内容,我为角三角形制作了不同的鼠标侦听器,但遇到一个问题,mouseEntered和MouseExited事件得到了当鼠标位置位于标签的边界(不是多边形)的边界上时调用,因此如果我将鼠标输入标签的边界(但是鼠标的位置仍在多边形的外部),然后决定移动鼠标(虽然我仍在标签的边界内)在多边形的边界内,它不会检查它是否在内部,因为已经调用mouseEntered,并且在该方法中对多边形内的点进行了检查(相同因此,我尝试而不是覆盖mouseEntered和mouseExited做自己的方法,而只是将它们命名为mouseEntered2和mouseExited2,并且每次鼠标随着mouseMoves事件移动时我都会调用它们,但是当我移动鼠标之后使用它甚至都没有被称为mouseMoved事件,我也不知道为什么。这是代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DragDialog extends JDialog {

private int pointX;
private int pointY;

public DragDialog() {
    JTextField textField = createTextField();
    setContentPane(createBackgroundPanel());
    setUndecorated(true);
    setBackground(new Color(0, 0, 0, 0));
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

private JPanel createBackgroundPanel() {
    JPanel panel = new JPanel() {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(527, 527);
        }
    };
    panel.setOpaque(false);
    panel.setLayout(null);

    String[] filePaths = getFilePaths();
    Map<String, ImageIcon> imageMap = initImages(filePaths);
    Map<String, Rectangle> rectMap = createRectangles();
    Map <String, Polygon> polygonMap = createPolygons();
    for (Map.Entry<String, Rectangle> entry: rectMap.entrySet()) {
        JLabel label = new JLabel();
        label.setOpaque(false);
        String fullImageKey = entry.getKey() + "-default";
        ImageIcon icon = imageMap.get(fullImageKey);
        label.setIcon(icon);
        label.setBounds(entry.getValue());
        if (entry.getKey().equals("northwest") ||
            entry.getKey().equals("northeast") ||
            entry.getKey().equals("southwest") ||
            entry.getKey().equals("southeast")) {
                label.addMouseListener(new CornerLabelMouseListener(entry.getKey(), imageMap, polygonMap.get(entry.getKey())));
        }
        else {
            label.addMouseListener(new SideLabelMouseListener(entry.getKey(), imageMap));
        }
        panel.add(label);
    }

    JLabel octagon = createOctagonLabel();
    octagon.setBounds(85, 85, 357, 357);
    panel.add(octagon);

    return panel;
}



private Map<String, Rectangle> createRectangles() {
    Map<String, Rectangle> rectMap = new HashMap<>();
    rectMap.put("north", new Rectangle(191, 0, 145, 73));
    rectMap.put("northwest", new Rectangle(74, 74, 103, 103));
    rectMap.put("northeast", new Rectangle(348, 74, 103, 103));
    rectMap.put("west", new Rectangle(0, 190, 73, 145));
    rectMap.put("east", new Rectangle(453, 190, 73, 145));
    rectMap.put("south", new Rectangle(190, 453, 145, 73));
    rectMap.put("southwest", new Rectangle(74, 348, 103, 103));
    rectMap.put("southeast", new Rectangle(349, 349, 103, 103));

    return rectMap;
}

private Map <String, Polygon> createPolygons() {
    Map <String, Polygon> polygonMap = new HashMap <> ();
    int [] x = new int[3];
    int [] y = new int [3];
    x[0] = 0;
    x[1] = 103;
    x[2] = 0;
    y[0] = 0;
    y[1] = 0;
    y[2] = 103;
    polygonMap.put("northwest", new Polygon(x, y, 3));
    x[2] = 103;
    polygonMap.put("northeast", new Polygon(x, y, 3));
    y[0] = 103;
    polygonMap.put("southeast", new Polygon(x, y, 3));
    x[1] = 0;
    polygonMap.put("southwest", new Polygon(x, y, 3));
    return polygonMap;
}

private class SideLabelMouseListener extends MouseAdapter {
    private String position;
    private Map<String, ImageIcon> imageMap;

    public SideLabelMouseListener(String position, Map<String, ImageIcon> imageMap) {
        this.imageMap = imageMap;
        this.position = position;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println(position + " pressed");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel label = (JLabel)e.getSource();
        String fullName = position + "-hovered";
        ImageIcon icon = imageMap.get(fullName);
        label.setIcon(icon);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        JLabel label = (JLabel)e.getSource();
        String fullName = position + "-default";
        ImageIcon icon = imageMap.get(fullName);
        label.setIcon(icon);
    }
}

private class CornerLabelMouseListener extends MouseAdapter{
    private String position;
    private Map <String, ImageIcon> imageMap;
    private Polygon polygon;

    public CornerLabelMouseListener(String position, Map <String, ImageIcon> imageMap, Polygon polygon) {
        this.imageMap = imageMap;
        this.position = position;
        this.polygon= polygon;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (polygon.contains(e.getPoint())) {
            System.out.println(position + " pressed");
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("moving");
        mouseEntered2(e);
        mouseExited2(e);
    }

    //@Override
    public void mouseEntered2(MouseEvent e) {
        System.out.println("point = " +e.getPoint().toString());
        if (polygon.contains(e.getPoint())) {
            System.out.println("here");
            JLabel label = (JLabel)e.getSource();
            String fullName = position + "-hovered";
            ImageIcon icon = imageMap.get(fullName);
            label.setIcon(icon);
        }
    }

    //@Override
    public void mouseExited2(MouseEvent e) {
        if (!polygon.contains(e.getPoint())) {
            JLabel label = (JLabel)e.getSource();
            String fullName = position + "-default";
            ImageIcon icon = imageMap.get(fullName);
            label.setIcon(icon);
        }
    }

}

private String[] getFilePaths() {
    String[] paths = { "north-default.png", "north-hovered.png",
            "northwest-default.png", "northwest-hovered.png",
            "northeast-default.png", "northeast-hovered.png",
            "west-default.png", "west-hovered.png", "east-default.png",
            "east-hovered.png", "south-default.png", "south-hovered.png",
            "southwest-default.png", "southwest-hovered.png",
            "southeast-default.png", "southeast-hovered.png"

    };
    return paths;
}

private Map<String, ImageIcon> initImages(String[] paths) {
    Map<String, ImageIcon> map = new HashMap<>();
    for (String path: paths) {
        ImageIcon icon = null;
        try {
            System.out.println(path);
            icon = new ImageIcon(ImageIO.read(getClass().getResource("/octagonframe/" + path)));
        } catch (IOException e) {

            e.printStackTrace();
        }
        String prefix = path.split("\\.")[0];
        map.put(prefix, icon);
    }
    return map;
}

private JTextField createTextField() {
    final JTextField field = new JTextField(20);
    field.setText("Type \"exit\" to terminate.");
    field.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = field.getText();
            if ("exit".equalsIgnoreCase(text)) {
                System.exit(0);
            }
        }
    });
    return field;
}

private JLabel createOctagonLabel() {
    Image image = null;
    try {
        image = ImageIO.read(getClass().getResource("/octagonframe/octagon.png"));
    } catch (IOException ex) {
        Logger.getLogger(DragDialog.class.getName()).log(Level.SEVERE,
                null, ex);
    }
    JLabel label = new JLabel(new ImageIcon(image));
    label.setLayout(new GridBagLayout());
    label.add(createTextField());

    label.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            DragDialog.this.setLocation(
                    DragDialog.this.getLocation().x + e.getX() - pointX,
                    DragDialog.this.getLocation().y + e.getY() - pointY);
        }
    });
    label.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            pointX = e.getX();
            pointY = e.getY();
        }
    });

    return label;
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new DragDialog();
        }
    });
}

最佳答案

只需使用图像即可。从your last post中,您试图创建框架的形状。但是您可以只使用透明图像。



测试程序。

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DragDialog extends JDialog {

    private int pointX;
    private int pointY;

    public DragDialog() {
        JLabel backgroundLabel = createBackgroundLabel();
        JTextField textField = createTextField();
        setContentPane(backgroundLabel);
        add(textField);
        setUndecorated(true);
        setBackground(new Color(0, 0, 0, 0));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private JTextField createTextField() {
        final JTextField field = new JTextField(20);
        field.setText("Type \"exit\" to terminate.");
        field.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String text = field.getText();
                if ("exit".equalsIgnoreCase(text)) {
                    System.exit(0);
                }
            }
        });
        return field;
    }

    private JLabel createBackgroundLabel() {
        Image image = null;
        try {
            image = ImageIO.read(new URL("http://i.stack.imgur.com/A9zlj.png"));
        } catch (IOException ex) {
            Logger.getLogger(DragDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        JLabel label = new JLabel(new ImageIcon(image));
        label.setLayout(new GridBagLayout());

        label.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                DragDialog.this.setLocation(DragDialog.this.getLocation().x + e.getX() - pointX,
                        DragDialog.this.getLocation().y + e.getY() - pointY);
            }
        });
        label.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                pointX = e.getX();
                pointY = e.getY();
            }
        });

        return label;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DragDialog();
            }
        });
    }
}




这是我使用的图像。 (只是一些简单的Photoshop图片





更新

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DragDialog extends JDialog {

    private int pointX;
    private int pointY;

    public DragDialog() {
        JTextField textField = createTextField();
        setContentPane(createBackgroundPanel());
        setUndecorated(true);
        setBackground(new Color(0, 0, 0, 0));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private JPanel createBackgroundPanel() {
        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(527, 527);
            }
        };
        panel.setOpaque(false);
        panel.setLayout(null);

        String[] filePaths = getFilePaths();
        Map<String, ImageIcon> imageMap = initImages(filePaths);
        Map<String, Rectangle> rectMap = createRectangles();

        for (Map.Entry<String, Rectangle> entry: rectMap.entrySet()) {
            JLabel label = new JLabel();
            label.setOpaque(false);
            String fullImageKey = entry.getKey() + "-default";
            ImageIcon icon = imageMap.get(fullImageKey);
            label.setIcon(icon);
            label.setBounds(entry.getValue());
            label.addMouseListener(new LabelMouseListener(entry.getKey(), imageMap));
            panel.add(label);
        }

        JLabel octagon = createOctagonLabel();
        octagon.setBounds(85, 85, 357, 357);
        panel.add(octagon);

        return panel;
    }



    private Map<String, Rectangle> createRectangles() {
        Map<String, Rectangle> rectMap = new HashMap<>();
        rectMap.put("north", new Rectangle(191, 0, 145, 73));
        rectMap.put("northwest", new Rectangle(74, 74, 103, 103));
        rectMap.put("northeast", new Rectangle(348, 74, 103, 103));
        rectMap.put("west", new Rectangle(0, 190, 73, 145));
        rectMap.put("east", new Rectangle(453, 190, 73, 145));
        rectMap.put("south", new Rectangle(190, 453, 145, 73));
        rectMap.put("southwest", new Rectangle(74, 348, 103, 103));
        rectMap.put("southeast", new Rectangle(349, 349, 103, 103));

        return rectMap;
    }

    private class LabelMouseListener extends MouseAdapter {
        private String position;
        private Map<String, ImageIcon> imageMap;

        public LabelMouseListener(String position, Map<String, ImageIcon> imageMap) {
            this.imageMap = imageMap;
            this.position = position;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println(position + " pressed");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            String fullName = position + "-hovered";
            ImageIcon icon = imageMap.get(fullName);
            label.setIcon(icon);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            JLabel label = (JLabel)e.getSource();
            String fullName = position + "-default";
            ImageIcon icon = imageMap.get(fullName);
            label.setIcon(icon);
        }
    }

    private String[] getFilePaths() {
        String[] paths = { "north-default.png", "north-hovered.png",
                "northwest-default.png", "northwest-hovered.png",
                "northeast-default.png", "northeast-hovered.png",
                "west-default.png", "west-hovered.png", "east-default.png",
                "east-hovered.png", "south-default.png", "south-hovered.png",
                "southwest-default.png", "southwest-hovered.png",
                "southeast-default.png", "southeast-hovered.png"

        };
        return paths;
    }

    private Map<String, ImageIcon> initImages(String[] paths) {
        Map<String, ImageIcon> map = new HashMap<>();
        for (String path: paths) {
            ImageIcon icon = null;
            try {
                System.out.println(path);
                icon = new ImageIcon(ImageIO.read(getClass().getResource("/octagonframe/" + path)));
            } catch (IOException e) {

                e.printStackTrace();
            }
            String prefix = path.split("\\.")[0];
            map.put(prefix, icon);
        }
        return map;
    }

    private JTextField createTextField() {
        final JTextField field = new JTextField(20);
        field.setText("Type \"exit\" to terminate.");
        field.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = field.getText();
                if ("exit".equalsIgnoreCase(text)) {
                    System.exit(0);
                }
            }
        });
        return field;
    }

    private JLabel createOctagonLabel() {
        Image image = null;
        try {
            image = ImageIO.read(getClass().getResource("/octagonframe/octagon.png"));
        } catch (IOException ex) {
            Logger.getLogger(DragDialog.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        JLabel label = new JLabel(new ImageIcon(image));
        label.setLayout(new GridBagLayout());
        label.add(createTextField());

        label.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                DragDialog.this.setLocation(
                        DragDialog.this.getLocation().x + e.getX() - pointX,
                        DragDialog.this.getLocation().y + e.getY() - pointY);
            }
        });
        label.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                pointX = e.getX();
                pointY = e.getY();
            }
        });

        return label;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DragDialog();
            }
        });
    }
}

10-08 12:53