import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
public class TitleBarFrame extends JWindow {
private JLabel titleLabel;
private JLabel messageLabel;
private JPanel contentPanel;
private JButton button;
private JPanel buttonPanel;
private int xPos, yPos;
public TitleBarFrame(String title) {
contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
messageLabel = new JLabel("Du siehst dir gerade eine Demonstration eines JWindows mit eigener Titelleiste an.", SwingConstants.CENTER);
messageLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPanel.add(messageLabel, BorderLayout.CENTER);
titleLabel = new JLabel(title);
try {
titleLabel.setIcon(new ImageIcon(new URL("http://www.famfamfam.com/lab/icons/silk/icons/user_comment.png")));
}
catch(MalformedURLException e) {
e.printStackTrace();
}
titleLabel.setOpaque(true);
titleLabel.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
titleLabel.setBackground(UIManager.getDefaults().getColor("InternalFrame.activeTitleBackground"));
titleLabel.setForeground(UIManager.getDefaults().getColor("InternalFrame.activeTitleForeground"));
titleLabel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point window = TitleBarFrame.this.getLocation();
TitleBarFrame.this.setLocation(window.x+e.getX()-xPos, window.y+e.getY()-yPos);
}
});
titleLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
}
});
button = new JButton("OK");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); //muss in einem realen Programm auskommentiert werden!
setVisible(false);
dispose();
}
});
buttonPanel = new JPanel();
buttonPanel.setBorder(new EmptyBorder(new Insets(0, 0, 10, 0)));
buttonPanel.add(button);
contentPanel.add(titleLabel, BorderLayout.NORTH);
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
add(contentPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new TitleBarFrame("<html>[b]TitleBarFrame[/b]</html>");
}
}