import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class OwnTitleFrameDemo extends JDialog {
private JLabel messageLabel;
private JButton button;
private JPanel buttonPanel;
private int xPos, yPos;
public OwnTitleFrameDemo(Frame parent, String title, String message) {
super(parent, true);
setUndecorated(true);
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
messageLabel = new JLabel(message, SwingConstants.CENTER);
messageLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPanel.add(messageLabel, BorderLayout.CENTER);
JPanel titlePanel = new JPanel(new BorderLayout());
titlePanel.setBorder(BorderFactory.createEmptyBorder(0,3,0,0));
titlePanel.setBackground(UIManager.getDefaults().getColor("InternalFrame.activeTitleBackground"));
JLabel titleBar = new JLabel("<html>[b]"+title+"[/b]</html>");
titleBar.setForeground(UIManager.getDefaults().getColor("InternalFrame.activeTitleForeground"));
titleBar.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point window = OwnTitleFrameDemo.this.getLocation();
OwnTitleFrameDemo.this.setLocation(window.x+e.getX()-xPos, window.y+e.getY()-yPos);
}
});
titleBar.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
}
});
titlePanel.add(titleBar, BorderLayout.CENTER);
CloseButton closeButton = new CloseButton();
titlePanel.add(closeButton, BorderLayout.EAST);
button = new JButton("OK");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeDialog();
}
});
buttonPanel = new JPanel();
buttonPanel.setBorder(new EmptyBorder(new Insets(0, 0, 10, 0)));
buttonPanel.add(button);
contentPanel.add(titlePanel, BorderLayout.NORTH);
contentPanel.add(buttonPanel, BorderLayout.SOUTH);
add(contentPanel);
pack();
setLocationRelativeTo(parent);
setVisible(true);
}
private void closeDialog() {
setVisible(false);
dispose();
}
public void setMessage(String message) {
messageLabel.setText(message);
}
class CloseButton extends JButton {
CloseButton() {
super();
setPreferredSize(new Dimension(15, 15));
setMargin(new Insets(0, 0, 0, 0));
setFocusPainted(false);
setContentAreaFilled(false);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeDialog();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED.darker());
g.fillRect(2, 2, getSize().width-4, getSize().height-4);
g.setColor(Color.WHITE);
g.drawRect(2, 2, getSize().width-4, getSize().height-4);
g.drawLine(5, 5, getSize().width-5, getSize().height-5);
g.drawLine(getSize().width-5, 5, 5, getSize().height-5);
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new OwnTitleFrameDemo(null, "OwnTitleFrameDemo", "Du siehst dir gerade eine Demonstration eines JDialogs mit eigener Titelleiste an.");
}
}