Hei Leute,
ich hab folgendes Problem: Ich habe eine eigene Titelleiste programmiert und sie als JPanel in mein Fenster eingefügt. Nur weiß ich nicht, wie ich jetzt das Fenster beim Ziehen mit der Maus auf der Titelleiste verschieben kann. Ich hab es mit der Methode mouseDragged(MouseEvent) der Klasse java.awt.event.KeyListener probiert, doch dann fängt das Fenster nur furchtbar an zu flackern und ich muss das Programm (meistens) über den Taskmanager beenden, weil nichts mehr funktioniert.
Hier der Quellcode, ich hoffe jemand kann mir helfen.
Danke schon mal im Voraus.
JPKI
ich hab folgendes Problem: Ich habe eine eigene Titelleiste programmiert und sie als JPanel in mein Fenster eingefügt. Nur weiß ich nicht, wie ich jetzt das Fenster beim Ziehen mit der Maus auf der Titelleiste verschieben kann. Ich hab es mit der Methode mouseDragged(MouseEvent) der Klasse java.awt.event.KeyListener probiert, doch dann fängt das Fenster nur furchtbar an zu flackern und ich muss das Programm (meistens) über den Taskmanager beenden, weil nichts mehr funktioniert.
Hier der Quellcode, ich hoffe jemand kann mir helfen.
Danke schon mal im Voraus.
JPKI
Code:
/*
* @(#) JPKITitleBar.java
*
* Urheberrechtlich geschützt.
* Alle Rechte vorbehalten.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* Eine Titelleiste für undekorierte JFrames
* (javax.swing.JFrame#setUndecorated(boolean)).
*
* @author JPKI
* @version 1.0
*/
public class JPKITitleBar extends JPanel implements ComponentListener, MouseListener, MouseMotionListener,
WindowListener, Runnable {
public final static int NORMAL = 2000;
public final static int SELECTED = 3000;
public final static int CLICKED = 4000;
private boolean closeable, minimizable, deactivated;
private int closeSelected, minimizeSelected;
private String title;
private JFrame frame;
private Dimension dim;
private Image closebutton, closebutton_selected, closebutton_clicked, closebutton_disabled;
private Image minimizebutton, minimizebutton_selected, minimizebutton_clicked, minimizebutton_disabled;
private Image bar, bar_disabled;
private BufferedImage buffer;
private Graphics offScreen;
public JPKITitleBar(JFrame frame, String title, boolean closeable, boolean minimizable) throws TitleBarException {
try {
setSize(frame.getSize().width,30);
setBorder(new BevelBorder(BevelBorder.LOWERED));
this.frame = frame;
this.title = title;
this.closeable = closeable;
this.minimizable = minimizable;
if (frame == null)
throw new TitleBarException("illegal frame parameter: nulltype found, JFrame requiered");
if (title == null)
title = new String();
if (title.length() > 20)
title = title.substring(0,20);
dim = Toolkit.getDefaultToolkit().getScreenSize();
closebutton = new ImageIcon(getClass().getResource("close_normal.gif")).getImage();
closebutton_selected = new ImageIcon(getClass().getResource("close_selected.gif")).getImage();
closebutton_clicked = new ImageIcon(getClass().getResource("close_clicked.gif")).getImage();
closebutton_disabled = new ImageIcon(getClass().getResource("close_disabled.gif")).getImage();
minimizebutton = new ImageIcon(getClass().getResource("minimize_normal.gif")).getImage();
minimizebutton_selected = new ImageIcon(getClass().getResource("minimize_selected.gif")).getImage();
minimizebutton_clicked = new ImageIcon(getClass().getResource("minimize_clicked.gif")).getImage();
minimizebutton_disabled = new ImageIcon(getClass().getResource("minimize_disabled.gif")).getImage();
bar = new ImageIcon(getClass().getResource("bar.gif")).getImage();
bar_disabled = new ImageIcon(getClass().getResource("bar_disabled.gif")).getImage();
deactivated = false;
closeSelected = NORMAL;
minimizeSelected = NORMAL;
buffer = new BufferedImage(dim.width,30,BufferedImage.TYPE_INT_RGB);
offScreen = buffer.getGraphics();
addMouseListener(this);
addMouseMotionListener(this);
frame.addComponentListener(this);
frame.addWindowListener(this);
new Thread(this).start();
} catch (Exception ex) {
throw new TitleBarException("Unable to initialize JPKITitleBar: " + ex);
}
}
public void componentResized(ComponentEvent e) {
setSize(frame.getSize().width,30);
frame.setLocation((dim.width-frame.getSize().width)/2,(dim.height-frame.getSize().height)/2);
}
public void mousePressed(MouseEvent e) {
if (e.getX() >= getSize().width-28 & e.getX() <= getSize().width-4 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = CLICKED;
minimizeSelected = NORMAL;
}
else if (e.getX() >= getSize().width-54 & e.getX() <= getSize().width-30 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = NORMAL;
minimizeSelected = CLICKED;
}
}
public void mouseReleased(MouseEvent e) {
if (e.getX() >= getSize().width-28 & e.getX() <= getSize().width-4 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = SELECTED;
minimizeSelected = NORMAL;
close();
}
else if (e.getX() >= getSize().width-54 & e.getX() <= getSize().width-30 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = NORMAL;
minimizeSelected = SELECTED;
minimize();
}
else {
closeSelected = NORMAL;
minimizeSelected = SELECTED;
}
}
public void mouseMoved(MouseEvent e) {
if (e.getX() >= getSize().width-28 & e.getX() <= getSize().width-4 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = SELECTED;
minimizeSelected = NORMAL;
}
else if (e.getX() >= getSize().width-54 & e.getX() <= getSize().width-30 & e.getY() >= 3 & e.getY() <= 27) {
closeSelected = NORMAL;
minimizeSelected = SELECTED;
}
else {
closeSelected = NORMAL;
minimizeSelected = NORMAL;
}
}
public void mouseDragged(MouseEvent e) {
frame.setLocation((dim.width-e.getX())/2,(dim.height-e.getY())/2);
}
public void windowActivated(WindowEvent e) {
deactivated = false;
}
public void windowDeactivated(WindowEvent e) {
deactivated = true;
}
public void run() {
while (true) {
try { Thread.sleep(50); } catch (InterruptedException ex) {}
bufferPaint();
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
frame.setTitle(title);
}
public void paintComponent(Graphics g) {
if (buffer != null)
g.drawImage(buffer,0,0,this);
}
private void bufferPaint() {
int j = 0;
for (int i = 0; i <= ((int) getSize().width/120); i++) {
if (deactivated)
offScreen.drawImage(bar_disabled,j,0,this);
else
offScreen.drawImage(bar,j,0,this);
j+=120;
}
offScreen.setColor(Color.white);
offScreen.setFont(new Font("Courier",Font.BOLD,20));
offScreen.drawString(title,4,22);
if (closeable)
if (closeSelected == NORMAL)
offScreen.drawImage(closebutton,getSize().width-28,3,this);
else if (closeSelected == SELECTED)
offScreen.drawImage(closebutton_selected,getSize().width-28,3,this);
else if (closeSelected == CLICKED)
offScreen.drawImage(closebutton_clicked,getSize().width-28,3,this);
else
offScreen.drawImage(closebutton_disabled,getSize().width-28,3,this);
if (minimizable)
if (minimizeSelected == NORMAL)
offScreen.drawImage(minimizebutton,getSize().width-54,3,this);
else if (minimizeSelected == SELECTED)
offScreen.drawImage(minimizebutton_selected,getSize().width-54,3,this);
else if (minimizeSelected == CLICKED)
offScreen.drawImage(minimizebutton_clicked,getSize().width-54,3,this);
else
offScreen.drawImage(minimizebutton_disabled,getSize().width-54,3,this);
repaint();
}
private void close() {
if (frame.getDefaultCloseOperation() == JFrame.EXIT_ON_CLOSE)
System.exit(0);
else if (frame.getDefaultCloseOperation() == WindowConstants.DISPOSE_ON_CLOSE)
frame.dispose();
}
private void minimize() {
frame.setState(Frame.ICONIFIED);
}
public void componentHidden(ComponentEvent e) {
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}