package jalview.appletgui; import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Insets; import javax.swing.JLabel; import javax.swing.JPanel; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TitledPanel extends JPanel { private String title; private Insets insets = new Insets(10, 10, 10, 10); public TitledPanel() { this(""); } public TitledPanel(String title) { this.setTitle(title); } public Insets getInsets() { return insets; } public void paint(Graphics g) { super.paint(g); g.setColor(getForeground()); g.drawRect(5, 5, getWidth() - 10, getHeight() - 10); int width = g.getFontMetrics().stringWidth(getTitle()); g.setColor(getBackground()); g.fillRect(10, 0, width, 10); g.setColor(getForeground()); g.drawString(getTitle(), 10, 10); } public static void main(String[] args) { JFrame f = new JFrame("TitledPanel Tester"); TitledPanel p = new TitledPanel("Title of Panel"); p.add(new JLabel("Label 1")); p.add(new JLabel("Label 2")); p.add(new JLabel("Label 3")); f.add(p); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setBounds(300, 300, 300, 300); f.setVisible(true); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }