gpt4 book ai didi

Java如何在窗口或JFrame的边框/周围放置一个按钮

转载 作者:行者123 更新时间:2023-12-01 13:36:45 26 4
gpt4 key购买 nike

如何在框架周围的边框上放置一个按钮,就像这个齿轮:

enter image description here

最佳答案

_"Is the a short example anywhere? "

是的,这里...这是非常基本的。你需要做更多的事情。您会注意到,我必须向充当顶部框架边框的 JPanel 添加一个 MouseMotionListener ,因为当您从框架中删除装饰时,您也会采取去掉这个功能。因此,MouseMotionListener 使框架再次可拖动。

如果您愿意,您还必须实现调整大小。当您按下图像时,我已经实现了Systemexit()`。测试一下。您需要提供您自己的图像。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class UndecoratedExample {
static JFrame frame = new JFrame();
static class MainPanel extends JPanel {

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}

static class BorderPanel extends JPanel {

JLabel stackLabel;
int pX, pY;

public BorderPanel() {
ImageIcon icon = new ImageIcon(getClass().getResource(
"/resources/stackoverflow1.png"));
stackLabel = new JLabel();
stackLabel.setIcon(icon);

setBackground(Color.black);
setLayout(new FlowLayout(FlowLayout.RIGHT));

add(stackLabel);

stackLabel.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
System.exit(0);
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
// Get x,y and store them
pX = me.getX();
pY = me.getY();
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent me) {
frame.setLocation(frame.getLocation().x + me.getX() - pX,
frame.getLocation().y + me.getY() - pY);
}
});
}
}

static class OutsidePanel extends JPanel {

public OutsidePanel() {
setLayout(new BorderLayout());
add(new MainPanel(), BorderLayout.CENTER);
add(new BorderPanel(), BorderLayout.PAGE_START);
setBorder(new LineBorder(Color.BLACK, 5));
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {

frame.setUndecorated(true);
frame.add(new OutsidePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

enter image description here

关于Java如何在窗口或JFrame的边框/周围放置一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com