Java/Swing GUI 最佳实践(从代码角度) [英] Java/Swing GUI best practices (from a code standpoint)
问题描述
与本维基相比,我正在寻找合适的从编码的角度实现 Swing GUI 控件的方法.
As a contrast to this wiki, I am looking for the proper way to implement Swing GUI controls from a coding standpoint.
我一直在寻求学习 Java 及其 GUI 工具,但我发现一个接一个的互联网教程将所有内容都放在 main
中,我知道这是不对的.
I have been on a quest to learn Java and its GUI tools but I find internet tutorial after internet tutorial that throws everything in main
and I know this isn't right.
我也尝试过 RAD 系统,如 Netbeans 和其他可视化"编辑器,但是当我开始编码时,我有一大堆代码,我不知道它的一半功能,所以我打算学习手工编写代码摇摆,我知道基本的控件和布局,但想以正确的方式去做.
I've also tried RAD systems like Netbeans and other "visual" editors but by the time I get to coding I've got a heap of code that I don't know half of what it does, so I'm intent on learning to hand code swing, and I know the basic controls and layout, but want to do it the right way.
是否有我遗漏的模型或标准?
Is there a model or standard I'm missing?
示例问题...
我是否扩展 JFrame 并创建我自己的框架对象?(我假设是)
do I extend JFrame and create my own frame object? (I would assume yes)
我是否将主菜单封装在该框架对象中?还是我自己创建?等等...
do I encapsulate the main menu inside that frame object? or do I create its own? etc...
如何将视图"逻辑与应用程序"逻辑分开?
How to I separate "View" logic from "Application" logic?
基本上,我正在寻找关于如何组织 GUI 代码的行业标准.
推荐答案
由于似乎对什么是最佳实践"存在一些争论,我将向您提供我发现的最适合我的方法,以及我的推理:
Since there seems to be some argument about what constitutes "best practices", I'll give you what I have found works best for me, and my reasoning:
1.每个窗口都应该扩展 JFrame 或 JDialog(取决于窗口的类型).这使得无需每次都指定特定对象即可轻松控制窗口的属性.不过,这更像是一般情况,因为众所周知,我会采用两种方式.
1. Each window should extend either JFrame or JDialog (depending on the type of window). This makes it easy to control the properties of the window without specifying a specific object every time. This is more of the general case, though, as I have been known to do it both ways.
2.main()
方法应该在一个单独的类中.这增加了能够在其他地方使用您的窗口类的可能性,因为它们不依赖于特定的实现.从技术上讲,它没有什么区别,但应用程序启动代码不属于窗口.
2.
The main()
method should be in a separate class. This increases the likelihood of being able to use your window classes elsewhere, as they are not tied to specific implementations. Technically it doesn't make a difference, but application startup code just doesn't belong in a window.
3.侦听器应该在匿名内部类中.您的顶级类不应实现任何侦听器.这可以防止黑客从任何地方调用侦听器方法,除了它们所附加的对象.
3. Listeners should be in anonymous inner classes. Your top-level class should not implement any listeners. This prevents hacks like calling the listener methods from anywhere except the object to which they are attached.
这是一个简单的应用程序,带有单个框架来演示这些做法:
Here is a simple application with a single frame to demonstrate these practices:
public class Main {
public static void main(String[] args) {
final String text = args[0];
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final MyWindow wnd = new MyWindow(text);
wnd.setVisible(true);
}
});
}
}
public class MyWindow extends JFrame {
public MyWindow(String text) {
super("My Window");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
MyWindow.this.setVisible(false);
MyWindow.this.dispose();
}
});
final JButton btn = new JButton(text);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(MyWindow.this, "Button Pressed", "Hey", JOptionPane.INFORMATION_MESSAGE);
}
});
setLayout(new FlowLayout());
add(btn);
pack();
}
}
这篇关于Java/Swing GUI 最佳实践(从代码角度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!