0
private Component createContent() {
        final Image image = requestImage();

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, null);
            }
        };

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        for (String label : new String[]{"BlackJack", "Poker", "Go-Fish"}) {
            JButton button = new JButton(label);
            button.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(Box.createRigidArea(new Dimension(15, 15)));
            panel.add(button);
        }

        panel.setPreferredSize(new Dimension(500, 500));

        return panel;
    }

This is the example im using. I have the rest of the code just displays the window and background image. I am trying to which the image and buttons and button placement when the user clicks on a button.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"..the rest of the code.."* Good that you did not dump the entire code, but.. 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Mar 01 '20 at 02:10
  • BTW 1) `g.drawImage(image, 0, 0, null);` should be `g.drawImage(image, 0, 0, this);`, as **every** `JComponent` is an `ImageObserver`. 2) `panel.setPreferredSize(new Dimension(500, 500));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Mar 01 '20 at 02:12
  • Zachary Tanner Perian - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 23 '20 at 10:14

2 Answers2

1

Well, you have to add some Action Listeners to your buttons that call the method they trigger.

button.addActionListener(listener)

Being the listener object an implementation of the ActionListener interface (You must define de actionPerfomed(ActionEvent e) method)

https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

Remember that a switch statement in Java could only function with primitives and since JDK 7 String literals, but thats it.

Hope this helps.

Huatxu
  • 111
  • 5
1

A. Let the class, where you have written this method, implement ActionListener

B. Add the ActionListener to each button as follows:

for (String label : new String[]{"BlackJack", "Poker", "Go-Fish"}) {
    JButton button = new JButton(label);
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    button.addActionListener(this);
    panel.add(Box.createRigidArea(new Dimension(15, 15)));
    panel.add(button);
}

C. Define the following method in this class:

@Override
public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
    case "BlackJack":
        // TODO
        break;

    case "Poker":
        // TODO
        break;
    case "Go-Fish":
        // TODO
        break;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110