Java/Swing GUI best practices (from a code standpo

2019-01-30 04:32发布

问题:

As a contrast to this wiki, I am looking for the proper way to implement Swing GUI controls from a coding standpoint.

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.

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?

example questions...

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?

Basically, I'm looking for what the industry standard is, on how to organize GUI code.

回答1:

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. 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. 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. 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();
    }
}


回答2:

I agree with all of Jonathan's points.

  1. Each window should extend either JFrame or JDialog...

  2. The main() method should be in a separate class...

  3. Listeners should be in anonymous inner classes...

I would also like to add the following:

4.) Use GridBagLayout (GBL) judiciously. GBL is a very powerful Layout Manager, difficult to learn, but very powerful.

5.) Consider hand coding all your UI. I'm personally not a fan of the code that is produced by visual editors. But, with that said I have not used a visual editor in several years. They might be better at this point.

6.) Use JPanels judiciously. Look at your ui and determine which components should behave the same as the screen size changes and then group those components together on a JPanel. Consider using JPanels inside of JPanels to get your correct resizing behavior.

I normally take a slightly different approach on having my components handle events then Jonathan does, but I believe his approach is a bit cleaner then mine.

Also, really study the use of MVC and Layered Architecture. It is truly best not to be mixing UI and Business Logic together.