Pourquoi mon JComponent n'est pas affiché en haut du JFrame arrière ?
Veuillez vérifier le code suivant :
class CounterFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private MyPanel myComponent = new MyPanel();
private JLabel contentPane = new JLabel(new ImageIcon(getClass()
.getResource("background/2.jpg")));
CounterFrame() {
contentPane.setLayout(new GridBagLayout());
setContentPane(contentPane);
add(myComponent);
}
}
class MyPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private Font myFont;
private String target;
private String raised = "200000";
private Image background;
public MyPanel() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D twoD = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
twoD.setRenderingHints(rh);
File f = new File("fonts/event.ttf");
try {
myFont = Font.createFont(Font.TRUETYPE_FONT, f);
myFont = myFont.deriveFont(90f);
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
twoD.setColor(Color.BLACK);
twoD.setFont(myFont);
twoD.drawString(raised,5, 90);
}
}