lookorange
lookorange

Reputation: 31

Adding a JLabel to my clock face isn't working

I tried, adding the flowlayout as seen in other answers, but it still doesn't work.

I've also tried moving around my JLabel code into a constructor but that doesn't work either.

public class Paint {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Paint");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        Draw draw = new Draw();
        frame.add(draw);
        frame.setVisible(true);
    }
}


public class Draw extends JPanel{

    public Draw(){
        JLabel one = new JLabel("12",JLabel.CENTER);
        setLayout(new FlowLayout());
        add(one);
    }

    public void paint(Graphics g){
        g.drawOval(70, 60, 190, 190);

        g.setColor(Color.BLACK);
        g.drawLine(90, 160, 170, 160);
        g.drawLine(120, 190,170 , 160);

        g.setColor(Color.GRAY);
        g.fillOval(155, 153, 20, 20);
    }
}

Upvotes: 0

Views: 50

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

  • Change paint to override paintComponent
  • Call super.paintComponent before doing any custom painting

enter image description here

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Paint {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Paint");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        Draw draw = new Draw();
        frame.add(draw);
        frame.setVisible(true);
    }

    public static class Draw extends JPanel {

        public Draw() {
            JLabel one = new JLabel("12", JLabel.CENTER);
            setLayout(new FlowLayout());
            add(one);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            g.drawOval(70, 60, 190, 190);

            g.setColor(Color.BLACK);
            g.drawLine(90, 160, 170, 160);
            g.drawLine(120, 190, 170, 160);

            g.setColor(Color.GRAY);
            g.fillOval(155, 153, 20, 20);
        }
    }
}

Upvotes: 1

Related Questions