Não consigo mover o janela com JFrame.setUndecorated(true)

Eu quero usar setUndecorated(true) na minha janela, mas isto acaba acarretando de eu não conseguir mover a janela. Poderiam me mandar soluções? Sou novo no site, me desculpem se fiz algo de errado no tópico.

Você precisa habilitar o JFrame para permitir ser arrastado ao se clicar no corpo. Procure por algo como “drag undecorated JFrame”, que você deve achar alguns exemplos de como fazer isso. Ex: https://stackoverflow.com/questions/16046824/making-a-java-swing-frame-movable-and-setundecorated

Abraço.

1 curtida

Um outro exemplo é usar alguns eventos:

MouseDragged
MousesReleased
e Mouse Pressed

Aí vc pode colocar um painel no topo, chamar o painel de menu (exemplo) e aplicar os eventos do mouse nesse componente:

int xMouse = 0, yMouse = 0; //variáveis

private void menuMouseDragged(MouseEvent evt) {
        int x = evt.getXOnScreen();
        int y = evt.getYOnScreen();

        this.setLocation(x - xMouse, y - yMouse);
    }

    private void menuMouseReleased(MouseEvent evt) {
        setOpacity((float) 1.0);
    }

    private void menuMousePressed(MouseEvent evt) {
        setOpacity((float) 0.8);
        xMouse = evt.getX();
        yMouse = evt.getY();
    }
1 curtida

Utilize a classe abaixo, aí basta fazer DragListener.add( objetoDaTelaUndecorated ):

import java.awt.Component;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;

public final class DragListener extends MouseInputAdapter {

    public static void add(Frame frame) {
        if (!frame.isUndecorated()) {
            throw new IllegalArgumentException("Frame is not undecorated!");
        }
        DragListener listener = new DragListener();
        frame.addMouseListener(listener);
        frame.addMouseMotionListener(listener);
    }

    private Point location;
    private MouseEvent pressed;

    public void mousePressed(MouseEvent me) {
        pressed = me;
    }

    public void mouseDragged(MouseEvent me) {
        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX();
        int y = location.y - pressed.getY() + me.getY();
        component.setLocation(x, y);
    }

    private DragListener() {}
}

Exemplo de uso:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

@SuppressWarnings("serial")
public class TelaExemplo extends JFrame {

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            TelaExemplo tela = new TelaExemplo();
            tela.setDefaultCloseOperation(EXIT_ON_CLOSE);
            DragListener.add(tela); // permitir o arrasto da tela udecorated
            tela.setVisible(true);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public TelaExemplo() {
        super("Exemplo");
        setMinimumSize(new Dimension(480, 320));
        setUndecorated(true); // minha janela é undecorated
        setLocationRelativeTo(null);

        JLabel label = new JLabel("Minha tela undecorated", SwingConstants.CENTER);
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(label, BorderLayout.CENTER);
    }

}
1 curtida

Obrigado.

1 curtida