Java / Swing GUI最佳实践(从代码的角度) [英] Java/Swing GUI best practices (from a code standpoint)

查看:192
本文介绍了Java / Swing GUI最佳实践(从代码的角度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此维基相比,我正在寻找适当的从编码的角度来实现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和其他visual编辑器,但是在我编码的时候,我有一堆代码,我不知道它的一半,所以我打算学习手动代码摆动,我知道基本的控制

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆