setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);报错Cannot resolve symbol 'origin',代码没有声明origin变量;按钮没有宽高
时间: 2024-04-21 07:23:11 浏览: 100
pdf操作盖章签字 spire.rar
非常抱歉,我在之前的代码中犯了一个错误。请尝试将`origin`变量的声明移动到`CustomWindow`类的顶部,如下所示:
```java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CustomWindow extends JFrame {
private JPanel titleBarPanel;
private JButton minimizeButton;
private JButton maximizeButton;
private JButton closeButton;
private Point origin;
public CustomWindow() {
// 设置窗口大小和背景色
this.setSize(1100, 700);
this.getContentPane().setBackground(new Color(204, 204, 204));
// 去除窗口边框默认样式
this.setUndecorated(true);
// 禁用窗口缩放
this.setResizable(false);
// 居中显示窗口
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int windowWidth = this.getWidth();
int windowHeight = this.getHeight();
int x = (screenWidth - windowWidth) / 2;
int y = (screenHeight - windowHeight) / 2;
this.setLocation(x, y);
// 鼠标可以移动窗口
titleBarPanel = new JPanel();
titleBarPanel.setBackground(Color.WHITE);
titleBarPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
origin = new Point(e.getX(), e.getY());
}
@Override
public void mouseReleased(MouseEvent e) {
origin = null;
}
});
titleBarPanel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point p = getLocation();
setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
}
});
// 添加最小化、最大化和关闭按钮
minimizeButton = new JButton(new ImageIcon("minimize.png"));
maximizeButton = new JButton(new ImageIcon("maximize.png"));
closeButton = new JButton(new ImageIcon("close.png"));
minimizeButton.setBorderPainted(false);
maximizeButton.setBorderPainted(false);
closeButton.setBorderPainted(false);
minimizeButton.setOpaque(false);
maximizeButton.setOpaque(false);
closeButton.setOpaque(false);
minimizeButton.setContentAreaFilled(false);
maximizeButton.setContentAreaFilled(false);
closeButton.setContentAreaFilled(false);
// 添加按钮功能
minimizeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setExtendedState(JFrame.ICONIFIED);
}
});
maximizeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (getExtendedState() != JFrame.MAXIMIZED_BOTH) {
setExtendedState(JFrame.MAXIMIZED_BOTH);
} else {
setExtendedState(JFrame.NORMAL);
}
}
});
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 高亮按钮
minimizeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
minimizeButton.setIcon(new ImageIcon("minimize_highlight.png"));
}
@Override
public void mouseExited(MouseEvent e) {
minimizeButton.setIcon(new ImageIcon("minimize.png"));
}
});
maximizeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
maximizeButton.setIcon(new ImageIcon("maximize_highlight.png"));
}
@Override
public void mouseExited(MouseEvent e) {
maximizeButton.setIcon(new ImageIcon("maximize.png"));
}
});
closeButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
closeButton.setIcon(new ImageIcon("close_highlight.png"));
}
@Override
public void mouseExited(MouseEvent e) {
closeButton.setIcon(new ImageIcon("close.png"));
}
});
// 将按钮添加到标题栏
titleBarPanel.add(minimizeButton);
titleBarPanel.add(maximizeButton);
titleBarPanel.add(closeButton);
// 将标题栏添加到窗口
this.add(titleBarPanel, BorderLayout.NORTH);
// 设置窗口关闭操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CustomWindow();
}
});
}
}
```
这样,`origin`变量就能在窗口移动的方法中正确地使用了。同时,请确保将按钮图标文件替换为实际的图片文件,并确保这些图片文件存在于项目目录中。希望这次能帮到你!如果还有其他问题,请随时提问。
阅读全文