0

I'm trying to create a name generator that generates names by random. Now when i click the button, it generates a name, but it shows up in the Eclipse console and not in the same window as the button, which is what I want it to do. Eventually I would like to be able to have a specific picture for for the button and for the background (window/frame). Also, as a bonus I'm hoping to add a bit of music to it that can play in the background once you open the app. Now, I'm fairly new to java and I've only watched a couple of tutorials, but it's gotten me this far and hopefully I can learn a thing or two from one of you guys. So please be kind, Best regards leal.

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

public class ClanNameGenerator {

public static void main (String[] args){

    JFrame frame = new JFrame("ExETesT");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,350);
    JPanel panel = new JPanel ();
    frame.add(panel);
    JButton button = new JButton("Click me");
    panel.add(button);
    button.addActionListener(new Action());

}
    static class Action implements ActionListener{

        public void actionPerformed (ActionEvent e){
            String[] names = {"test", "Zero", "Club", "Moonkys", "znakes", "SeamOnster", "dnktwhm", "Rambo", "OmG", "siste"};
            String[] names2 = {"Ylos", "zzzzz", "sdsd", "OK"};
            String[] names3 = {"Hei", "ok", "jadd", "så drar vi", "det var det"};

            int random = (int) (Math.random()*names.length);
            int random2 = (int) (Math.random()*names2.length);
            int random3 = (int) (Math.random()*names3.length);

            System.out.println("Your clan name is: "  + names[random] +" "+ names2[random2] +" "+ names3[random3]);


        }
    }
}
Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
leal
  • 1
  • 1
  • 1

3 Answers3

1

if you do something like this, the name will appear in JFrame:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class ClanNameGenerator {

private static JLabel label;
public static void main (String[] args){

    JFrame frame = new JFrame("ExETesT");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,350);
    JPanel panel = new JPanel ();
    frame.add(panel);
    JButton button = new JButton("Click me");
    panel.add(button);
    label = new JLabel();
    panel.add(label);
    button.addActionListener(new Action());

}
    static class Action implements ActionListener{

        public void actionPerformed (ActionEvent e){
            String[] names = {"test", "Zero", "Club", "Moonkys", "znakes", "SeamOnster", "dnktwhm", "Rambo", "OmG", "siste"};
            String[] names2 = {"Ylos", "zzzzz", "sdsd", "OK"};
            String[] names3 = {"Hei", "ok", "jadd", "så drar vi", "det var det"};

            int random = (int) (Math.random()*names.length);
            int random2 = (int) (Math.random()*names2.length);
            int random3 = (int) (Math.random()*names3.length);

            label.setText("Your clan name is: "  + names[random] +" "+ names2[random2] +" "+ names3[random3]);

        }
    }
}
Lucas Z.
  • 435
  • 3
  • 11
  • Ok, thanks a lot guys. I've been searching everywhere trying to find something similar, so that's why I had to give it a try here. I wasn't trying to get anyone to write it for me, just wanted to know what I was doing wrong. Now you guys have given me multiple options that I can try out. So thanks again for all your help. It's much appreciated. – leal Aug 13 '14 at 21:09
0

There's a lot going on in your question without you actually asking any questions. As for your text problem though, System.out.println(); will always print to the console, wherever it may be. To add it to the JFrame, you have a variety of options. The one you probably want is to use a JLabel, unless you have a lot of text, in which case a JTextArea may be better suited, or if you intend to do something a little fancier than regular text you may want to draw it directly using the Graphics class and drawString(String).

How to add text to JFrame?

As for the other parts of your... "question," do some research and try things before asking for help on the Stack. This community is for assisting you when things don't work, not for writing your projects for you.

Community
  • 1
  • 1
Kevin
  • 302
  • 2
  • 11
  • Ok, thanks a lot guys. I've been searching everywhere trying to find something similar, so that's why I had to give it a try here. I wasn't trying to get anyone to write it for me, just wanted to know what I was doing wrong. Now you guys have given me multiple options that I can try out. So thanks again for all your help. It's much appreciated. – leal Aug 13 '14 at 21:09
0

Add a JLabel to your UI, then call the setText method of that JLabel object.

jLabelObject.setText("Your clan name is: "  + 
    names[random] +" "+ names2[random2] +" "+ names3[random3);
kag0
  • 5,624
  • 7
  • 34
  • 67
  • Ok, thanks a lot guys. I've been searching everywhere trying to find something similar, so that's why I had to give it a try here. I wasn't trying to get anyone to write it for me, just wanted to know what I was doing wrong. Now you guys have given me multiple options that I can try out. So thanks again for all your help. It's much appreciated. – leal Aug 13 '14 at 21:08