2013-06-26 33 views
0

即時通訊使用的各類編碼的一個項目,但是當我按下一個按鈕,叫我的主菜單我卡住了,這是我的恢復代碼:調用擺動JPanels從ActionListener的

我的主類:

public static void main(String[] args) { 

    SwingUtilities.invokeLater(new Runnable() { 
     Dimension res = Toolkit.getDefaultToolkit().getScreenSize(); 

public void run(){ 

    Principal frame = new Principal("Program"); 

    frame.setSize(res.width,res.height); 

    frame.setResizable(false); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setUndecorated(true); 

    frame.setVisible(true); 
    } 
});` 

現在,我的校長:

public class Principal extends JFrame implements ActionListener { 


    private DetallesPrincipal DetallesPrincipal; 

    private ClientesNuevo ClientesNuevo; 

    private ClientesModificar ClientesModificar; 

    Container p; 

    private static final long serialVersionUID = 1L; 

    final JMenuItem Clientes1,Clientes2,Clientes3; 

    final JMenuBar MenuBarMain = new JMenuBar(); 

public Principal (String titulo){ 

     super(titulo); 

      setLayout(new BorderLayout()); 

     final JMenuBar MenuBarMain = new JMenuBar(); 

     MenuBarMain.setBackground(new Color(177,178,182)); 

     final JMenu MenuMainClientes; 

     MenuMainClientes = new JMenu("Clientes"); 



      DetallesPrincipal = new DetallesPrincipal(); 
      ClientesNuevo = new ClientesNuevo(); 

        p = getContentPane(); 

       MenuBarMain.add(MenuMainClientes); 

       Clientes1 = new JMenuItem("Nueva ficha"); 

        MenuMainClientes.add(Clientes1); 

          Clientes1.addActionListener(this); 

       p.add(MenuBarMain, BorderLayout.PAGE_START); 

       p.add(DetallesPrincipal, BorderLayout.CENTER); 

    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 

     if (e.getSource()==Clientes1){ 

      p.removeAll(); 

      p.repaint(); 

      p.add(ClientesNuevo, BorderLayout.CENTER); 

      p.revalidate(); 

      p.repaint(); 
     } 

我ClientesNuevo:

public class ClientesNuevo extends JPanel implements ActionListener { 

    private static final long serialVersionUID = 1L; 

    DetallesPrincipal m; 

    GridBagConstraints gc = new GridBagConstraints(); 

public ClientesNuevo() { 


    Dimension size = getPreferredSize(); 
     size.width = 250; 

     setPreferredSize(size); 

     setBackground(new Color(111,114,123)); 

     TitledBorder Principal2 = BorderFactory.createTitledBorder("Te encuentras en clientes"); 

     Principal2.setTitleColor(new Color(100,100,100)); 

     Principal2.setTitleFont(new Font("Arial",0,24)); 

     Principal2.setTitlePosition(TitledBorder.BOTTOM); 

     Principal2.setTitleJustification(TitledBorder.LEFT); 

     setBorder(Principal2); 


    JLabel Titulo1 = new JLabel("Nueva ficha cliente"); 

     Titulo1.setFont(new Font("Tahoma",0,36)); 

    JLabel Nombre = new JLabel("Nombre/Razón social "); 

    JLabel Apellidos = new JLabel("Apellidos "); 

    JLabel Cif = new JLabel("CIF/DNI "); 

    JLabel Poblacion = new JLabel("Población "); 

    JLabel Provincia = new JLabel("Provincia"); 

    JLabel Cpostal = new JLabel("Código postal"); 

    JLabel Telefono = new JLabel("Teléfono"); 

    JLabel Movil = new JLabel("Móvil"); 

    JLabel Notas = new JLabel("Notas"); 


    JTextField NombreText = new JTextField(25); 

    JTextField ApellidosText = new JTextField(25); 

    JTextField CifText = new JTextField(15); 

    JTextField PoblacionText = new JTextField(15); 

    JTextField ProvinciaText = new JTextField(20); 

    JTextField CpostalText = new JTextField(15); 

    JTextField TelefonoText = new JTextField(20); 

    JTextField MovilText = new JTextField(20); 

    JTextField NotasText = new JTextField(30); 


    JButton Aceptar = new JButton("Aceptar"); 

    JButton Cancelar = new JButton("Cancelar"); 

     Aceptar.addActionListener(this); 


    setLayout(new GridBagLayout()); 

     GridBagConstraints gc = new GridBagConstraints(); 


      gc.gridx = 0; 

      gc.gridy = 0; 

      gc.weighty = 1; 

      add(Titulo1, gc); 


      gc.anchor = GridBagConstraints.LINE_START; 

      gc.gridx = 0; 

      gc.gridy = 1; 

      add(Nombre, gc); 


      gc.gridx = 1; 

      gc.gridy = 1; 

      add(NombreText, gc); 


      gc.gridx = 0; 

      gc.gridy = 2; 

      add(Apellidos, gc); 

      gc.anchor = GridBagConstraints.LINE_START; 

      gc.gridx = 1; 

      gc.gridy = 2; 

      add(ApellidosText, gc); 


      gc.anchor = GridBagConstraints.LINE_START; 

      gc.weightx = 1; 

      gc.weighty = 1; 

      gc.gridx = 0; 

      gc.gridy = 3; 

      add(Cif, gc); 


      gc.anchor = GridBagConstraints.LINE_START; 

      gc.gridx = 1; 

      gc.gridy = 3; 

      add(CifText, gc); 

      gc.gridx = 0; 

      gc.gridy = 4; 

      add(Poblacion, gc); 


      gc.gridx = 1; 

      gc.gridy = 4; 

      add(PoblacionText, gc); 

      gc.gridx = 2; 

      gc.gridy = 4; 

      add(Provincia, gc); 


      gc.gridx = 2; 

      gc.gridy = 7; 

      add(Aceptar, gc); 


      gc.gridx = 3; 

      gc.gridy = 7; 

      add(Cancelar, gc); 
} 



public void actionPerformed(ActionEvent e) { 

       removeAll(); 

       repaint(); 

       m = new DetallesPrincipal(); 

      add(m,gc); 

      revalidate(); 

      repaint(); 

    } 


    } 

嗯,對不起walltext,但我很迷茫,我只是打電話給我的DetallesPrincipal類的組件,有什麼更好的選擇來管理這樣的程序?

+4

1)請在句子的開頭添加一個大寫字母。還要爲單詞I使用大寫字母,並使用JEE或WAR等縮寫詞和首字母縮略詞。這使人們更容易理解和幫助。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 3)不要延長框架或其他頂層容器。而是創建並使用一個實例。 4)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 –

+1

請爲班級學習常見的[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具體用於名稱的情況)方法和屬性名稱並一致使用它們。 –

+0

對不起,我只是一個想學習的初學者。 – user2525239

回答

5

那麼除了你不發佈你的完整源代碼或SSCCE我不能完全測試。在你的代碼

尋找您所使用的removeAll()方法JPanel S於單一JFrame/容器多之間進行切換。

我看到您所犯的錯誤是邏輯錯誤,因爲您試圖在JPanel之間切換,而不是在您的實際JFrame之間切換。

見我建立這個小例子(允許用戶2個JPanel S之間在單個JFrame/容器它實現了一個Singleton design pattern交替,能夠容易地改變用於使MainGUI類的實例給每個JPanelMainGUI產卵所以該JPanel旨意有權訪問單個MainGUI小號swapPanel(JPanel p)法):

enter image description here

enter image description here

import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 


public class Test { 

    public static void main(String[] args) { 
     MainGUI mainGUI = MainGUI.getInstance(); 
    } 
} 
class MainGUI { 

    private JFrame frame; 

    private static class SingletonHolder { 

     public static final MainGUI INSTANCE = new MainGUI(); 
    } 

    public static MainGUI getInstance() { 
     return SingletonHolder.INSTANCE; 
    } 

    private MainGUI() { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       frame = createJFrame(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    private JFrame createJFrame() { 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Test"); 

     frame.add(new Panel1());//initial Jpanel to display 

     frame.pack(); 
     return frame; 
    } 

    public void swapPanel(JPanel p) { 
     frame.getContentPane().removeAll(); 
     frame.add(p); 
     frame.pack(); 
     frame.revalidate(); 
     frame.repaint(); 
    } 
} 

class Panel1 extends JPanel { 

    public Panel1() { 
     JLabel labelPanel1 = new JLabel("Panel 1"); 
     JButton switchPanelButton = new JButton("Goto Panel 2"); 
     switchPanelButton.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       MainGUI.getInstance().swapPanel(new Panel2()); 
      } 
     }); 
     add(labelPanel1); 
     add(switchPanelButton); 
    } 
} 

class Panel2 extends JPanel { 

    public Panel2() { 
     JLabel labelPanel1 = new JLabel("Panel 2"); 
     JButton switchPanelButton = new JButton("Goto Panel 1"); 
     switchPanelButton.addActionListener(new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       MainGUI.getInstance().swapPanel(new Panel1()); 
      } 
     }); 
     add(labelPanel1); 
     add(switchPanelButton); 
    } 
} 

一個更高級別的替代上面是CardLayout的CardLayout類管理共享相同的顯示空間的兩個或更多個部件(通常的JPanel實例)。

enter image description here

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test { 

    private final static String PANEL1 = "panel 1"; 
    private final static String PANEL2 = "panel 2"; 

    public Test() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test(); 
      } 
     }); 
    } 

    private void initComponents() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel panel1 = new JPanel(); 
     panel1.add(new JLabel("Panel 1")); 

     JPanel panel2 = new JPanel(); 
     panel2.add(new JLabel("Panel 2")); 

     //Create the panel that contains the "cards". 
     final JPanel cards = new JPanel(new CardLayout()); 
     cards.add(panel1, PANEL1); 
     cards.add(panel2, PANEL2); 

     //create button to allow chnage to next card 
     JButton buttonNext = new JButton(">"); 
     buttonNext.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       CardLayout cl = (CardLayout) (cards.getLayout());//get cards 
       cl.next(cards); 
      } 
     }); 

     //create button to allow chnage to previous card 
     JButton buttonPrev = new JButton("<"); 
     buttonPrev.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       CardLayout cl = (CardLayout) (cards.getLayout());//get cards 
       cl.previous(cards); 
      } 
     }); 

     //create panel to hold buttons which will allow switching between cards 
     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(buttonPrev); 
     buttonPanel.add(buttonNext); 


     frame.add(cards); 
     frame.add(buttonPanel, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+1

對............ – mKorbel

+0

@ user2525239查看我的最新更新 –

+1

+1很高興見到你回 – MadProgrammer