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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:08:43  来源:igfitidea点击:

How to make simple java buttons?

javajconsole

提问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.
}

回答by Msonic

If you want to create a user interface with Java, you may want to take a look at Swingor any GUI tool for Java.

如果您想使用 Java 创建用户界面,您可能需要查看Swing或任何适用于 Java 的 GUI 工具。

As the others said, buttons and consoles are not meant for each other.

正如其他人所说,按钮和控制台并不适合彼此。