How to make simple java buttons?

I want to know how to make a very simple java button that works in the console (I use a raspberry editor to create java in the class). I'm looking for something that when I click on it, the game starts.

-2
source share
3 answers

Well, in Java you usually create buttons using

JButton button = new JButton(); ... set properties here... button.setText("blah"); 

Then

 button.addMouseListener(new MouseListener() { override methods here to do what you want. } 

But you will be more specific about what you mean by "in the console."

+2
source

First, you want to start with a JFrame, not a console:

 JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); 

Then use the Charles code to help you get started with JButton.

 JButton button = new JButton(); ... set properties here... button.setText("blah"); 

Then, to activate the action, add a listener, again from Charles's message.

 button.addMouseListener(new MouseListener() { override methods here to do what you want. } 
+1
source

If you want to create a user interface with Java, you can take a look at Swing or any GUI tool for Java.

As others have said, buttons and consoles are not designed for each other.

0
source

All Articles