Java JFrame help
Moderator:Moderators
Does anyone know how to create a JFrame that has no decorations but that is still movable? When I remove the decorations (the border with the minimize/maximize, and close buttons) it seems to be locked into a specific location and is not movable.
That's interesting, I've never tried creating an undecorated JFrame before.
I guess the thing is, in Microsoft Windows atleast, the border is the only comonent ever used to move windows around. If you want such behaviour, I s'pose you're going to have to write a mouse listener for the JFrame and tap in the movement code yourself - something like...
I know it's a month and a half late, but I hope it helps.
I guess the thing is, in Microsoft Windows atleast, the border is the only comonent ever used to move windows around. If you want such behaviour, I s'pose you're going to have to write a mouse listener for the JFrame and tap in the movement code yourself - something like...
Code: Select all
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
WindowMovementListener listener = new WindowMovementListener(frame);
Container c = frame.getContentPane();
JPanel toppanel = new JPanel();
JButton closebutton = new JButton("X");
closebutton.addActionListener(new CloseListener(frame));
toppanel.setBackground(Color.BLUE);
toppanel.setLayout(new BorderLayout());
toppanel.add(closebutton,BorderLayout.EAST);
c.setBackground(Color.DARK_GRAY);
c.setLayout(new BorderLayout());
c.add(toppanel,BorderLayout.NORTH);
frame.addMouseListener(listener);
frame.addMouseMotionListener(listener);
frame.setUndecorated(true);
frame.setBounds(100,100,500,300);
frame.setVisible(true);
}
}
class WindowMovementListener implements MouseMotionListener,MouseListener
{
private JFrame frm;
private int x;
private int y;
public WindowMovementListener(JFrame pFrm)
{
frm = pFrm;
x = 0;
y = 0;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x = e.getXOnScreen();
y = e.getYOnScreen();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
int movx = e.getXOnScreen() - x;
int movy = e.getYOnScreen() - y;
frm.setLocation(frm.getLocation().x + movx, frm.getLocation().y + movy);
x = e.getXOnScreen();
y = e.getYOnScreen();
}
}
class CloseListener implements ActionListener
{
private JFrame frm;
public CloseListener(JFrame pFrm)
{
frm = pFrm;
}
public void actionPerformed(ActionEvent e)
{
frm.dispose();
}
}