目前我正在体验与Java制作相关的事情,我想尝试制作如下所示的GUI:
你是如何建议的?
首先,我创建了一个名为“My Frame”的类,它将包含一个包含每个多边形点的数组(因此每个多边形将是一个单独的框架)。
但是经过重新思考后,我想到了这些三角形的目的,我想让它们成为按钮,每次按下其中一个按钮会改变八角形内的内容,所以它更像是三角形的按钮和Octagon本身就是内容窗格,所以你们怎么建议我实现这个目标呢?
另一个相关的问题是,我想让这些三角形与八角形一起移动,如果用鼠标拖动八角形,三角形也随之移动,那么我应该将它们放到八角形框架中吗? / p>
我想问的另一件事是,因为我将框架设置为未修饰以改变它的形状我取消了调整框架大小的选项以及我使iconify和关闭按钮消失,所以怎么能我添加了这些功能,我搜索了网络,但我没有找到任何解释。
EDITED DIALOG CLASS:
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事件得到了当鼠标位置带有标签的BORDER(而不是多边形)时调用,所以如果我将鼠标输入标签的边框(但鼠标的位置仍然在多边形之外)然后我决定移动鼠标(虽然我仍然在标签的边框内)在多边形的边框内,它不会检查它是否在里面,因为mouseEntered已被调用,并且在该方法中检查多边形内部的点(相同)问题为mouseExited)所以我尝试了而不是覆盖mouseEntered和mouseExited做我自己的方法只是调用它们mouseEntered2和mouseExited2我每次鼠标移动时都调用它们与mouseMoves事件,但是在我移动mo之后这样做使用它甚至不会被称为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();
}
});
}
答案 0 :(得分:1)
只需使用图片。从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();
}
});
}
}