How to make simple java buttons? [closed]

烈酒焚心 提交于 2019-12-13 09:40:04

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!