minimizeButton,maximizeButton,closeButton没有显示在标题栏
时间: 2024-04-17 09:26:24 浏览: 91
带最小化、最大化、关闭按钮的titlewindow
抱歉,我之前的回答有误。在标题栏中添加按钮时,您需要使用`setLayout(null)`来禁用默认的布局管理器,并使用`setBounds()`方法来设置按钮的位置和大小。以下是更新后的代码示例:
```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.setLayout(null); // 禁用默认布局管理器
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.setBounds(5, 5, 40, 40);
maximizeButton.setBounds(50, 5, 40, 40);
closeButton.setBounds(95, 5, 40, 40);
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);
}
});
// 添加按钮到标题栏
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();
}
});
}
}
```
请确保将按钮图标文件替换为实际的图片文件,并确保这些图片文件存在于项目目录中。这样,按钮就应该正确地显示在标题栏上了。如果还有其他问题,请随时提问。
阅读全文