在Java Swing中使用Singleton

时间:2015-11-25 06:26:58

标签: java swing singleton

我认为使用Singleton模式就是这样的。我不想要严肃的方法或类或任何东西。我在这里是什么样的。

private void btnVehicleActionPerformed(java.awt.event.ActionEvent evt) {}

这是我的事件 - >动作 - > jButton btnVehicle actionPerformed 。当我按下按钮时,InternalFrame Form被加载到jDesktopPane。当我按两次两个表格加载。如果我可以使用Singleton,那么它是加载单帧的最佳解决方案,如果已经加载,则将Frame置于前端。我试过,实际上这不起作用,单按钮。

Registration_Vehicles rv;
private void btnVehicleActionPerformed(java.awt.event.ActionEvent evt) { 

    if(rv==null){
        rv = new Registration_Vehicles();
        jDesktopPane2.add(rv);
    }        
    else{
    rv.toFront();
    }
}

任何人都可以通过单身或其他任何方式为此提供更好的解决方案..

1 个答案:

答案 0 :(得分:0)

这是一个小程序,可以选择现有的内部框架或创建一个新框架(取决于用户指定的框架编号是否已存在):

import java.awt.FlowLayout;
import java.beans.PropertyVetoException;
import javax.swing.*;

public class SingletonInSwing {
    private JDesktopPane desktopPane;
    private JTextField frameNumberTextField;

    public static void main(final String[] arguments) {
        new SingletonInSwing().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        desktopPane = new JDesktopPane();
        desktopPane.add(createControlPanel());

        // Add one internal frame to get started.
        addInternalFrame("1");

        frame.setContentPane(desktopPane);
        frame.setVisible(true);
    }

    private JInternalFrame createControlPanel() {
        final JPanel controlPanel = new JPanel(new FlowLayout());
        frameNumberTextField = new JTextField(6);
        final JButton addFrameButton = new JButton("Add internal frame");
        controlPanel.add(new JLabel("Frame number:"));
        controlPanel.add(frameNumberTextField);
        controlPanel.add(addFrameButton);

        frameNumberTextField.addActionListener(actionEvent -> addInternalFrame());
        addFrameButton.addActionListener(actionEvent -> addInternalFrame());

        final JInternalFrame controlFrame = new JInternalFrame("Control frame");
        controlFrame.add(controlPanel);
        controlFrame.setBounds(10, 10, 760, 90);
        controlFrame.setVisible(true);

        return controlFrame;
    }

    private void addInternalFrame() {
        addInternalFrame(frameNumberTextField.getText());
    }

    private void addInternalFrame(final String frameNumber) {
        final String frameTitle = "test " + frameNumber;

        // Search for existing frame.
        JInternalFrame existingFrame = null;
        for (final JInternalFrame internalFrame : desktopPane.getAllFrames()) {
            if (frameTitle.equals(internalFrame.getTitle())) {
                existingFrame = internalFrame;
                break;
            }
        }

        // Select existing or create new frame.
        if (existingFrame != null) {
            try {
                existingFrame.setSelected(true);
            } catch (final PropertyVetoException e) {
                e.printStackTrace();
            }
        } else {
            final int frameCount = desktopPane.getAllFrames().length;
            final JInternalFrame internalFrame = new JInternalFrame(frameTitle);
            internalFrame.setBounds(20 * frameCount, 110 + 40 * frameCount, 400, 200);
            internalFrame.setVisible(true);
            desktopPane.add(internalFrame);
            internalFrame.moveToFront();
        }
    }
}

这里有一个关于JDesktopPaneJInternalFrame的精彩教程:The Java Tutorials - How to Use Internal Frames