使用Java Swing设计: 1.创建一个背景色为new Color(204, 204, 204);width:1100;height:700;的窗口; 2.去掉窗口边框默认样式,且禁用窗口缩放;窗口居中显示;鼠标可以移动窗口。 3.在窗口最上方添加自定义的白色标题栏; 4.标题栏最右侧有最小化按钮,最大化按钮,关闭按钮,全部按钮使用png图片,且有对应功能实现;且鼠标移动到按钮上时,对应的按钮会高亮; 5.关闭窗口,进程也会结束;
时间: 2024-04-17 21:26:14 浏览: 156
以下是使用Java Swing设计实现你所描述的窗口的代码示例:
```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.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();
}
});
}
}
```
请注意,上述代码中的按钮图标需要根据你的实际情况替换为相应的图片文件,并确保这些图片文件存在于项目目录中。此外,你还可以根据需要对按钮的样式和功能进行修改。
阅读全文