首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JFrame Singleton

JFrame Singleton
EN

Stack Overflow用户
提问于 2015-01-01 11:58:19
回答 2查看 3.2K关注 0票数 1

我在我的应用程序类中实现了一个单例设计模式。这个类叫做SearchFounisseurUi。它抑制了JFrame Swing类。我还有另一个类,它调用SearchFounisseurUi类。我已经写了下面的代码,但它不适合我。到目前为止这是我的密码。

代码语言:javascript
复制
package VIEW;

import CONTROLLER.SearchFournisseurController;
import MODEL.tableModel.FournisseurTableModel;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.ScrollPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class SearchFounisseurUi extends JFrame{

// Fields
private JTextField nomFournisseur ; 
private JTable resultTable ; 

// Label
private JLabel lblNomFournisseur; 

private JButton chercherButton ; 

// Model view controller 
private FournisseurTableModel fournisseurTableModel ; 
private SearchFournisseurController searchFournisseurController;
// Singleton
private static SearchFounisseurUi instance=null;

private  void SearchFounisseurUi() {

    initComponents();
    buildFrame();
    eventHandling( );
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();

}





/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SearchFounisseurUi frame = SearchFounisseurUi.getInstance();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

private void initComponents() { 
    // Model 
    fournisseurTableModel = new FournisseurTableModel();

    searchFournisseurController = new SearchFournisseurController() ;
    searchFournisseurController.setModel(fournisseurTableModel);
    searchFournisseurController.setUi(this);
    searchFournisseurController.setData();

    // View
    lblNomFournisseur = new JLabel("Nom Fournisseur");        
    nomFournisseur = new JTextField(40);
    chercherButton = new JButton("Chercher");
    resultTable  = new JTable(fournisseurTableModel);

 }

private void eventHandling() {
 }

private void buildFrame() {
    setLayout(new GridBagLayout());
     setPreferredSize(new Dimension(300,400));
    GridBagConstraints gc = new GridBagConstraints();

            // Line 1 
    gc.gridx = 0;
    gc.gridy = 0;
            gc.gridwidth = 2;
            gc.weighty = 1.0;
            gc.fill = GridBagConstraints.BOTH;
    gc.anchor = GridBagConstraints.LINE_START;

            add(new JScrollPane(resultTable),gc);

            // line 2

            gc.gridy++;
            gc.gridwidth  = 1 ;
            gc.weighty = 0.01;
            gc.fill = GridBagConstraints.HORIZONTAL;

            add(lblNomFournisseur,gc);

            gc.gridx++;
            add(nomFournisseur,gc);

            // line 3

            gc.gridx = 0 ;
            gc.gridy++ ; 
            add(chercherButton,gc);


 }

public static SearchFounisseurUi getInstance(){
    if(instance == null)
    instance =  new SearchFounisseurUi();  

    return instance;

}
}

这是我开课的地方:

代码语言:javascript
复制
btnChercherFournisseur.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                SearchFounisseurUi fen  = SearchFounisseurUi.getInstance();

            }
        });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-01 12:01:09

setVisible(true)对象上调用SearchFounisseurUi

这应该能解决你的问题。

代码语言:javascript
复制
    btnChercherFournisseur.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            SearchFounisseurUi fen  = SearchFounisseurUi.getInstance();
            fen.setVisible(true);  /// !!! /// 
        }
    });
票数 1
EN

Stack Overflow用户

发布于 2015-01-01 12:01:10

构造函数没有返回类型。所以从

代码语言:javascript
复制
private  void SearchFounisseurUi() {

将其更改为

代码语言:javascript
复制
private SearchFounisseurUi() {

类似于主要方法,在getInstance方法调用中

代码语言:javascript
复制
instance.setVisible(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27730598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档