问题:1.标题栏上的按钮没有显示;2.改进一下,将标题栏的高修改为25。
时间: 2024-04-17 15:26:19 浏览: 96
标题栏上的按钮
非常抱歉,我忘记在代码中设置标题栏的高度。以下是更新后的代码,包括添加了按钮和修改了标题栏的高度为25:
```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;
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.setPreferredSize(new Dimension(this.getWidth(), 25)); // 设置标题栏高度为25
titleBarPanel.addMouseListener(new MouseAdapter() {
private Point origin;
@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();
}
});
}
}
```
请确保将按钮图标文件替换为实际的图片文件,并确保这些图片文件存在于项目目录中。这次代码已经包括了标题栏高度的修改。希望这次能帮到你!如果还有其他问题,请随时提问。
阅读全文