java 如何制作简单的java按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10233711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make simple java buttons?
提问by user1336648
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 按钮(我正在使用 crimson 编辑器在课堂上做 java)我正在寻找一些当你点击它时它会启动游戏的东西。
回答by Charles
Well in Java you generally create buttons using
在 Java 中,您通常使用以下方法创建按钮
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".
但是您将更具体地了解“在控制台中”的含义。
回答by Chad
First off, you'll want to start with a JFrame, not console:
首先,您需要从 JFrame 开始,而不是控制台:
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.
然后,使用 Charles 的代码来帮助您开始使用 JButton。
JButton button = new JButton();
... set properties here...
button.setText("blah");
Then for an action to fire, add a listener, again from Charles' post.
然后要触发一个动作,添加一个监听器,同样来自 Charles 的帖子。
button.addMouseListener(new MouseListener() {
override methods here to do what you want.
}