2016-03-19 17 views
1

私は顧客注文を処理するアプリケーションを作成しています(javaで)。 私のプログラムには3つのJFrameウィンドウがあります(はい、複数のフレームを使用するのは良い考えではありませんが、実際には互いに接続されていません)。複数のJFrame

  1. メイン1:ここではあなたは(顧客またはオペレータ)であるかを選択
  2. カスタマーJFrame
  3. オペレータJFrame

お客様は、オーダー(メインフレーム>顧客フレーム>は、完全な落札後。順序(ボタン)私はこのような何かをしています:

customerframe.dispose(); 
customerframe.revalidate(); 
customerframe.repaint(); 
reloadframe(); ///a method which reinitializes the frame (Note: I am doing a frame=new JFrame() here) 
mainframe.setVisible(true); 

私はCを選択します再びお客様はcustomerframeを開きますが、問題はリスナーがもう働かないということです。彼らは何とか古いフレームや何かに接続していると思います。

私はそれが今、いくつかの時間のために動作させるためにしようとしている...

+0

私はすでにそれを読んでいるパネルを切り替えることができます。 –

答えて

4

あなたはより多くの情報のためthisリンクを参照してくださいJFrames複数使用すべきではありません。

は代わりに、私はここに示されたように、あなたがCardLayoutを使用することをお勧め:

import javax.swing.*; 
import java.awt.*; 

public class MainFrame 
{ 
    static JPanel homeContainer; 
    static CardLayout cl; 

    JPanel homePanel; 
    JPanel otherPanel; 


    public MainFrame() 
    { 
     JFrame mFrame = new JFrame("CardLayout Example"); 
     JButton showOtherPanelBtn = new JButton("Show Other Panel"); 
     JButton backToHomeBtn = new JButton("Show Home Panel"); 

     cl = new CardLayout(5, 5); 
     homeContainer = new JPanel(cl); 
     homeContainer.setBackground(Color.black); 

     homePanel = new JPanel(); 
     homePanel.setBackground(Color.blue); 
     homePanel.add(showOtherPanelBtn); 

     homeContainer.add(homePanel, "Home"); 

     otherPanel = new JPanel(); 
     otherPanel.setBackground(Color.green); 
     otherPanel.add(backToHomeBtn); 

     homeContainer.add(otherPanel, "Other Panel"); 

     showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel")); 
     backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home")); 

     mFrame.add(homeContainer); 
     cl.show(homeContainer, "Home"); 
     mFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     mFrame.setLocationRelativeTo(null); 
     mFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     mFrame.pack(); 
     mFrame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(MainFrame::new); 
    } 
} 

Cardlayoutを使用すると、代わりのJPanelの間で切り替えます。このようにして、新しいコンテンツを表示するだけで新しいJFrameを作成する必要はありません。基本的には、ある種類のコンテナ(JFrameでもかまいませんか、JPanelでもかまいません)を作成してから、作成した他のパネルを追加すると、名前を付けられます。 cardLayout.show(container, "Name");

+0

それで複数のJFramesで動作させることは不可能ですか? –

+1

不可能ではありません。ただお勧めしません。長期間に渡ってカードレイアウトを使う方がはるかに簡単です。アクションリスナーで1行のコードでビューを切り替えることができます。 – Jonah

+0

私はありがとう、私はカードレイアウトに切り替えます。 –