gpt4 book ai didi

java - JPanel 失去焦点并且听众没有射击

转载 作者:行者123 更新时间:2023-12-02 03:21:28 27 4
gpt4 key购买 nike

因此,在我的窗口中,我将 JFrame 设置为 undecorated(true) ,并在顶部有我自己的自定义 header (带有关闭和最小化按钮)。我遇到的唯一问题是当您拖动此“自定义标题”时使窗口移动。整个标题位于 JPanel 中,然后将其添加到北侧的 JFrame (BorderLayout.NORTH)。我已将 MouseListenerMouseMotionListener 添加到此 JPanel,但它无法识别任何事件。我唯一可以假设的是我如何弄清楚布局。下面是标题的代码,以及与之配套的视觉效果。

代码:

private void addHeader()
{
headPane = new JPanel();
headPane.setLayout(new BoxLayout(headPane, BoxLayout.LINE_AXIS));
buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 2));
buttonPane.setBackground(mouseLineColor);
headPane.setBackground(Color.GREEN);

Font buttonFont = new Font("", Font.PLAIN, 18);
minimize.setFocusable(false);
minimize.setPreferredSize(new Dimension(30, 20));
minimize.setMargin(new Insets(0, 0, 0, 0));
minimize.setOpaque(false);
minimize.setBorder(null);
minimize.setForeground(Color.WHITE);
minimize.setOpaque(true);
minimize.setFont(buttonFont);
minimize.setBackground(buttonColor);

quit.setFocusable(false);
quit.setPreferredSize(new Dimension(30, 20));
quit.setMargin(new Insets(0, 0, 0, 0));
quit.setOpaque(false);
quit.setBorder(null);
quit.setForeground(Color.WHITE);
quit.setOpaque(true);
quit.setFont(buttonFont);
quit.setBackground(buttonColor);

back.setFocusable(false);
back.setPreferredSize(new Dimension(30, 20));
back.setMargin(new Insets(0, 0, 0, 0));
back.setOpaque(false);
back.setBorder(null);
back.setForeground(Color.WHITE);
back.setOpaque(true);
back.setFont(buttonFont);
back.setBackground(buttonColor);

if(screen != GAME_MENU)
buttonPane.add(back);
else
buttonPane.remove(back);

buttonPane.add(minimize);
buttonPane.add(quit);

headTitle = new JLabel("Bouncy Ball Version " + VERSION);
headTitle.setBorder(new EmptyBorder(0, 5, 0, 0));
headTitle.setFont(new Font("", Font.BOLD, 14));
headTitle.setForeground(Color.BLACK);
headTitle.setBackground(Color.YELLOW);
headTitle.setOpaque(true);
headTitle.setFocusable(false);
headPane.setFocusable(false);
buttonPane.setFocusable(false);

buttonPane.setBackground(Color.RED);

headPane.add(headTitle);
headPane.add(Box.createHorizontalGlue());
headPane.add(buttonPane);

if(callOnce)
{
minimize.addActionListener(this);
quit.addActionListener(this);
back.addActionListener(this);

minimize.addMouseListener(this);
quit.addMouseListener(this);
back.addMouseListener(this);

headPane.addMouseListener(this);
headPane.addMouseMotionListener(this);

callOnce = false;
}

headPane.setPreferredSize(new Dimension(headPane.getPreferredSize().width, 24));
frame.add(headPane, BorderLayout.NORTH);
}

听众:

按下鼠标:

Object source = e.getSource();
if(source == headPane)
{
mouseX = e.getX();
mouseY = e.getY();
movingWindow = true;
}

鼠标拖动:

Object source = e.getSource();
if(source == headPane)
{
if(movingWindow)
{
int x = e.getXOnScreen();
int y = e.getYOnScreen();
frame.setLocation(x - mouseX, y - mouseY);
}
}

enter image description here

我还要补充一点,当我单击 headPane 时,JButton 也将停止工作。我不知道它为什么这样做,或者答案是否真的很简单,我只是很愚蠢,但我尝试过的都没有奏效。

我对 Java 还很陌生,所以提前感谢您的帮助。

最佳答案

我认为不需要“callOnce”变量。框架和添加到框架的组件只能在创建类时创建一次。如果您多次调用“addHeader()”方法,那么我建议您存在设计问题。

此外,您不应该将 ActionListeners 添加到按钮两次。

The only problem I'm having is making the window move when you drag this 'custom header'.

查看Moving Windows用于允许您拖动任何组件的通用类。通常,您会在面板中拖动组件。

但是,该类还有一个功能,允许您通过拖动添加到窗口的组件来拖动桌面上的窗口。

关于java - JPanel 失去焦点并且听众没有射击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555806/

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