使用 WindowBuilder 作为 Java 程序的 GUI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17467686/
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
Using WindowBuilder as GUI for Java Program
提问by user2358330
I am a beginner in Java. I have devloped a GUI using WindowBuilder in Eclipse. I want to use this GUI to get input from user and pass it on to my Java program which performs some operations. Basically I want to use the GUI as a substitute for Console in Eclipse for entering the input. How do I do this?
我是 Java 的初学者。我在 Eclipse 中使用 WindowBuilder 开发了一个 GUI。我想使用这个 GUI 来获取用户的输入并将其传递给我执行一些操作的 Java 程序。基本上我想使用 GUI 代替 Eclipse 中的控制台来输入输入。我该怎么做呢?
Please point me to some tutorials or examples which can help. Thanks!
请向我指出一些可以提供帮助的教程或示例。谢谢!
回答by Devolus
For a start you can i.e. look herefor an example how to implement buttons. You can browse the site also for other components.
首先,您可以在这里查看如何实现按钮的示例。您还可以浏览该站点以查找其他组件。
The general concept is that you create your GUI with Window Builder visually. Then you can attach action handlers, which are called when the object is triggered. So for example in ordeer to perform some action when a button is pressed you do something like this:
一般概念是您使用 Window Builder 以可视方式创建 GUI。然后,您可以附加动作处理程序,在触发对象时调用这些处理程序。因此,例如为了在按下按钮时执行某些操作,您可以执行以下操作:
in the main code:
在主代码中:
createGUI(this);
In the gui code:
在gui代码中:
class MyGui
{
private JButton jButton;
private MyButtonListener mListener;
public void createGUI(MyButtonListener oListener)
{
mListener = oListener;
createGUIElements();
}
private createGUIElements()
{
jButton = new JButton();
jButton.setText("MyButton");
jButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
mListener.onButtonClicked(e);
}
});
}
}
Or an alternative approach where you directly create the action listener in the main application and jsut pass it to the GUi element.
或者另一种方法,您直接在主应用程序中创建动作侦听器并将其传递给 GUI 元素。
class MyGui
{
private JButton jButton;
private ActionListener mListener;
public void createGUI(ActionListener oListener)
{
mListener = oListener;
createGUIElements();
}
private createGUIElements()
{
jButton = new JButton();
jButton.setText("MyButton");
jButton.addActionListener(mListener);
}
}
The same basically applies to most other controls as well, so you can attach an action handler on a combobox, checkbox, etc..
这基本上也适用于大多数其他控件,因此您可以在组合框、复选框等上附加动作处理程序。
So to get started, just create a simple window with a single button and try to implement something when the button is pressed.
因此,开始时,只需创建一个带有单个按钮的简单窗口,并尝试在按下按钮时执行某些操作。