Hoces da prosiris funkcionalnost klase javax.swing.JLabel logicno je da je nasledis i overrajdujes paintComponent(Graphics g) metodu.
Code:
import java.awt.Graphics;
import javax.swing.JLabel;
/*
* Created on Aug 24, 2004*/
public class DecoratedLabel extends JLabel {
/**
*
*/
public DecoratedLabel() {
super();
}
/**
* @param text
*/
public DecoratedLabel(String text) {
super(text);
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0,0,this.getWidth(),this.getHeight());
g.drawLine(0,this.getHeight(),this.getWidth(),0);
}
}
Ovo ne crta elipsu ali to uradi sam.
Treba jos da dodas svoju komponetu na neki panel ovako:
Code:
import javax.swing.JFrame;
/*
* Created on Aug 24, 2004
*/
public class MainFrame extends JFrame {
private javax.swing.JPanel jContentPane = null;
private DecoratedLabel decoratedLabel = null;
public static void main(String[] args) {
}
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300,200);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(new java.awt.BorderLayout());
jContentPane.add(getDecoratedLabel(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes decoratedLabel
*
* @return DecoratedLabel
*/
private DecoratedLabel getDecoratedLabel() {
if (decoratedLabel == null) {
decoratedLabel = new DecoratedLabel();
decoratedLabel.setText("JLabel");
}
return decoratedLabel;
}
}
Javi jel u redu da izbrisem visak.