How to make simple java buttons? [closed]

2019-09-21 03:41发布

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.

标签: java jconsole
3条回答
迷人小祖宗
2楼-- · 2019-09-21 04:23

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楼-- · 2019-09-21 04:27

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".

查看更多
看我几分像从前
4楼-- · 2019-09-21 04:46

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.

查看更多
登录 后发表回答