JPanel失去焦点和听众没有被解雇

时间:2016-09-18 08:52:37

标签: java swing events jpanel border-layout

所以在我的窗口中,我将JFrame设置为undecorated(true)并在顶部设置了我自己的自定义标题(使用关闭和最小化按钮)。我唯一的问题是当你拖动这个'自定义标题'时窗口会移动。整个标头位于JPanel中,然后将其添加到北侧的JFrameBorderLayout.NORTH)。我已将MouseListenerMouseMotionListener添加到此JPanel,但它无法识别任何事件。我唯一可以假设的是如何弄清楚布局。下面是标题的代码,以及随之而来的视觉效果。

CODE:

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);
}

监听器:

的mousePressed:

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

的mouseDragged:

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的新手,所以提前感谢您的帮助。

0 个答案:

没有答案