Galera, estou precisnado retirar a barra de titulos do JInternalFrame mas de maneira que ele não perca o atributo de movimento.
Consegui retirar a barra de titulo, e adiconei os eventos do mouse para movimenta-lo, porém o mouse fica descentralizado, não consigo clicar no meio o JIF e move-lo, ao clicar e mover o mouse fica desalinhado.
Segue as imagens de como fica o mouse, e abaixo o código (rascunho), do que estou tentando fazer.
Alguem poderia me ajudar a encontrar o problema?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.plaf.synth.ColorType;
import javax.swing.plaf.synth.Region;
import javax.swing.plaf.synth.SynthContext;
import javax.swing.plaf.synth.SynthGraphicsUtils;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.plaf.synth.SynthPainter;
import javax.swing.plaf.synth.SynthStyle;
import javax.swing.plaf.synth.SynthStyleFactory;
/**
*
* @author Tomassoni
*/
public class MainPanel extends JPanel {
private MyInternalFrame iframe;
private final JDesktopPane desktop = new JDesktopPane();
public MainPanel() {
super(new BorderLayout());
JPanel p1 = new JPanel();
p1.setOpaque(false);
JPanel p2 = new JPanel() {
@Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(new Color(100, 50, 50, 100));
g.fillRect(0, 0, getWidth(), getHeight());
}
};
createFrame(p1, 1);
createFrame(p2, 2);
add(desktop);
setPreferredSize(new Dimension(480, 320));
}
protected JInternalFrame createFrame(JPanel panel, int idx) {
iframe = new MyInternalFrame();
if (panel != null) {
iframe.setContentPane(panel);
panel.add(new JLabel("label"));
panel.add(new JButton("button"));
iframe.getRootPane().setOpaque(false);
}
desktop.add(iframe);
iframe.setOpaque(false);
iframe.setVisible(true);
iframe.setLocation(120 * idx, 60 * idx);
iframe.addMouseMotionListener(new MouseHandler());
//frame.setBorder(null);
// ((BasicInternalFrameUI)getUI()).getNorthPane().setPreferredSize( new Dimension(0,0) );
iframe.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
javax.swing.plaf.InternalFrameUI ui = iframe.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);
desktop.getDesktopManager().activateFrame(iframe);
return iframe;
}
private void moveInternalFrame(MouseEvent e){
iframe.setLocation(e.getLocationOnScreen());
}
class Mouse extends MouseMotionAdapter{
Mouse(){
}
@Override
public void mouseDragged(MouseEvent e){
try{
iframe.setLocation(e.getLocationOnScreen());
System.out.println("mouse clicked!" + e.getX() + ":" + e.getY());
} catch(Exception excp){}
}
}
/**Classe pelos enventos do mouse, cliques, arrastes.
*
* @author Rogerio Tomassoni
*
* @version 0.1
*
* @since Release 01 da aplicacao
*
*/
private class MouseHandler implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent e) {
updateMouseCoordinates(e);
}
public void mousePressed(MouseEvent e) {
updateMouseCoordinates(e);
//System.out.println("PRESSED");
}
public void mouseReleased(MouseEvent e) {
updateMouseCoordinates(e);
updateMouseCoordinates(e);
}
public void mouseEntered(MouseEvent e) {
updateMouseCoordinates(e);
}
public void mouseExited(MouseEvent e) {
updateMouseCoordinates(e);
}
/*
*Metodo principal, ele é reponsavel pelo arraste do mouse
*
*@return void - Não há retorno.
*
*@param MouseEvent e - Recebe os eventos do mouse.
*
*/
public void mouseDragged(MouseEvent e) {
updateMouseCoordinates(e);
moveInternalFrame(e);
repaint();
}
public void mouseMoved(MouseEvent e) {
updateMouseCoordinates(e);
}
}
public void updateMouseCoordinates(MouseEvent e) {
String xCoor = "";
String yCoor = "";
if (e.getX() < 0) {
xCoor = "0";
} else {
xCoor = String.valueOf(e.getX());
}
if (e.getY() < 0) {
xCoor = "0";
} else {
yCoor = String.valueOf(e.getY());
}
//System.out.println("x:" + xCoor + " y:" + yCoor);
}
static class MyInternalFrame extends JInternalFrame {
public MyInternalFrame() {
super("t", true, true, true, true);
setSize(160, 100);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
try {
//UIManager.setLookAndFeel(
// "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
MySynthStyleFactory f = new MySynthStyleFactory(
SynthLookAndFeel.getStyleFactory());
SynthLookAndFeel.setStyleFactory(f);
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class MySynthStyleFactory extends SynthStyleFactory {
private SynthStyleFactory wrappedFactory;
public MySynthStyleFactory(SynthStyleFactory factory) {
this.wrappedFactory = factory;
}
public SynthStyle getStyle(JComponent c, Region id) {
SynthStyle s = wrappedFactory.getStyle(c, id);
//if(id==Region.INTERNAL_FRAME_TITLE_PANE||id==Region.INTERNAL_FRAME) {
if (id == Region.INTERNAL_FRAME) {
s = new TranslucentSynthSytle(s);
}
return s;
}
}
class TranslucentSynthSytle extends SynthStyle {
private final SynthStyle style;
public TranslucentSynthSytle(SynthStyle s) {
style = s;
}
@Override
public Object get(SynthContext context, Object key) {
return style.get(context, key);
}
public boolean getBoolean(SynthContext context,
Object key, boolean defaultValue) {
return style.getBoolean(context, key, defaultValue);
}
public Color getColor(SynthContext context, ColorType type) {
return style.getColor(context, type);
}
public Font getFont(SynthContext context) {
return style.getFont(context);
}
public SynthGraphicsUtils getGraphicsUtils(SynthContext context) {
return style.getGraphicsUtils(context);
}
public Icon getIcon(SynthContext context, Object key) {
return style.getIcon(context, key);
}
public Insets getInsets(SynthContext context, Insets insets) {
return style.getInsets(context, insets);
}
public int getInt(SynthContext context, Object key, int defaultValue) {
return style.getInt(context, key, defaultValue);
}
public SynthPainter getPainter(final SynthContext context) {
return new SynthPainter() {
public void paintInternalFrameBackground(SynthContext context,
Graphics g,
int x, int y, int w, int h) {
g.setColor(new Color(100, 200, 100, 100));
g.fillRoundRect(x, y, w, h, 5, 5);
}
};
}
public String getString(SynthContext context,
Object key, String defaultValue) {
return style.getString(context, key, defaultValue);
}
public void installDefaults(SynthContext context) {
style.installDefaults(context);
}
public void uninstallDefaults(SynthContext context) {
style.uninstallDefaults(context);
}
public boolean isOpaque(SynthContext context) {
if (context.getRegion() == Region.INTERNAL_FRAME) {
return false;
} else {
return style.isOpaque(context);
}
}
public Color getColorForState(SynthContext context, ColorType type) {
return null; //Color.RED;
}
public Font getFontForState(SynthContext context) {
return null; //new Font("MONOSPACE", Font.ITALIC, 24);
}
}