-1

Second button (b2) is not visible on the JFrame.

All the 3 componenets l1,l2 and b2 are visible on JFrame but JButton b2 isn't.

        public void Joptionpane(int a)
        {
        l1=new JLabel("Your score is :" + a);
        l2=new JLabel("Do you want to continue ?");
        b1= new JButton("Yes");
        b2= new JButton("NO");


        add(l1);
        add(l2);
        add(b1);
        add(b2);

       l1.setBounds(150,10,400,50);
       l2.setBounds(150,60,400,50);
       b1.setBounds(200,130,100,50);
       b2.setBounds(300,130,100,50);

I have set the Layout to null

        setVisible(true);
        setLayout(null);
        setTitle("Flappy Bird");
        setSize(700,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AwsmAsim
  • 3
  • 3
  • 3
    Don't use null layouts and `setBounds`. Learn and use the layout managers – Hovercraft Full Of Eels Mar 25 '20 at 14:55
  • 2
    When you ask a question post a proper [mre]. – camickr Mar 25 '20 at 15:02
  • A score notification should be a `JOptionPane` rather than a `JFrame` itself. And if that's the case, [this answer](https://stackoverflow.com/questions/41904362/multiple-joptionpane-input-dialogs/41904856#41904856) on the `singleDialogInformation` part might be of help – Frakcool Mar 25 '20 at 15:46

2 Answers2

2

From the code, it seems the app. needs something like this:

enter image description here

That is a JOptionPane, which is well suited to the task, as opposed to a JFrame, which isn't.

The code for that follows:

String text = "<html><p>Your score is: 42."
        + "<p>Do you want to continue?";
int result = JOptionPane.showConfirmDialog(
        null, text, "Flappy Bird", JOptionPane.YES_NO_OPTION);
if (result==JOptionPane.YES_OPTION) {
    System.out.println("Continue!");
    // TODO! Start another game
} else {
    System.out.println("Exit!");
    System.exit(0);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-2
    setVisible(true);
    setLayout(null);

Perhaps you should set the layout to null before you tell the frame to draw everything ...

And as we are on it: set the size before making the panel visible, too.

Sorry, but that are all no brainers. You can not fry an egg in the pan before you break the shell, but that is what you are doing.

    setLayout(null);
    setSize(700,300);
    setVisible(true);
Angel O'Sphere
  • 2,642
  • 20
  • 18