0

I am creating a small program. The program should consist of the following things:

First it opens a window with a picture and text. It also has a Button "Let's Go!" which opens a JOptionPane with some dialog and 4 different Buttons. (It's suposed to be a quiz, so a Question and 4 different answers) When you click the wrong answer it says something like "Wrong answer, try again" If you click the right answer it say: "Right answer, feel free to procede" or something like that and the next Question opens up.

So far so good. I am pretty new to Java and this is my first "big" programm and I already got a bit stuck. The Problem I am currently facing is: How can I place a JButton in my JFrame with the Picture and the Text? I created one bit it is either not displayed or below the Picture. Here is my code so far:

import java.awt.*;
import javax.swing.*;

public class HBA extends JFrame {
public HBA() {
    setSize(1100, 720);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout (new FlowLayout(FlowLayout.CENTER));

    Icon icon = new ImageIcon("HappyBirthday.jpg");
    JLabel label1 = new JLabel("Happy Birthday Anna!", JLabel.CENTER);
    JLabel label2 = new JLabel("Und viel Erfolg mit diesem Geschenk! ;)", JLabel.CENTER);
    JLabel label3 = new JLabel(icon);
    JButton OK = new JButton("Let's Go!");
    Font schrift = new Font("ComicSans", Font.BOLD, 24);
    label1.setFont(schrift);
    label2.setFont(schrift);
    label1.setForeground(Color.black);
    label2.setForeground(Color.black);

    getContentPane().add(label1);
    getContentPane().add(label2);
    getContentPane().add(label3);
    getContentPane().setBackground(Color.white);
    getContentPane().add(OK);

}
public static void main(String[] args) {
    new HBA().setVisible(true);

}

}

Beside the JButton Problem: Can someone give me an advice how to build up from this and create the JOptionPane windows?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Erik Ro
  • 9
  • 1
  • Adding a background to a frame can be done in at least one of two ways, you could use a JLabel, applying the image to the label as per normal, adding a layout manager to the label and adding the rest of your content to it and adding the label to your frame. The problem is, if the content exceeds the size of the image, they won't be laid out properly. The other approach would be to create a custom component, from something like a JPanel and use its paintComponent method to paint the image. You can then add your other components to this panel as per any other component – MadProgrammer Oct 28 '14 at 19:24
  • Ehm... okay. I am currently using a Flow.Layout. So... what do I need to edit where to integrate the Button? That's basiclly my question... How can I integrate the button and how do it place it in the bottom, center of the JFrame? – Erik Ro Oct 28 '14 at 19:45
  • Rollback: Problem solved. I found the button. The JFrame Size was not big enough. I set it to 1100, 850 now and the button is displayed proberly now as well. Still: Can someone give me a hint on how to integrate the JOptionPanes? I read that I have to create a Listener for the Button to make it do something. – Erik Ro Oct 28 '14 at 19:49
  • [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html), [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to Write an Action Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer Oct 28 '14 at 20:02
  • [This example](http://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503) demonstrates who to draw an image onto a `JPanel`, you can then use this `JPanel` as the bases for the rest of your programming, adding content to it per normal and then adding the panel to your frame – MadProgrammer Oct 28 '14 at 20:05
  • *"The JFrame Size was not big enough. I set it to 1100, 850 now and the button is displayed proberly now as well"* Consider using `pack` over `setSize` on the `JFrame`, this will automatically determine the frame size based on the requirements of it's content – MadProgrammer Oct 28 '14 at 20:06
  • Thanks for your help, but this topic can be closed. I'm doing it completly different now. – Erik Ro Oct 29 '14 at 19:37

0 Answers0