问题
I want to know how to make a very simple java button that works in console (i'm using crimson editor to do java in class) Im looking for something that when you click it it starts a game.
回答1:
Well in Java you generally 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're going to be more specific about what you mean by "in a console".
回答2:
First off, you'll want to start with a JFrame, not 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 Charles' code to help you get started on a JButton.
JButton button = new JButton();
... set properties here...
button.setText("blah");
Then for an action to fire, add a listener, again from Charles' post.
button.addMouseListener(new MouseListener() {
override methods here to do what you want.
}
回答3:
If you want to create a user interface with Java, you may want to take a look at Swing or any GUI tool for Java.
As the others said, buttons and consoles are not meant for each other.
来源:https://stackoverflow.com/questions/10233711/how-to-make-simple-java-buttons