2011-03-24 2 views
0
import java.awt.LayoutManager; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class LayoutTest extends JFrame { 

    public LayoutTest() { 
     initComponents(); 
    } 

    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jButton = new JButton(); 

     jPanel1.setBackground(new java.awt.Color(204, 204, 255)); 
     jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); 

     jButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       LayoutManager tempGroupLayout = jPanel1.getLayout(); 
       jPanel1.setLayout(layoutManager2); 
       layoutManager = layoutManager2; 
       layoutManager2 = tempGroupLayout; 
      } 
     }); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). 
       addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup(). 
       addContainerGap(286, Short.MAX_VALUE). 
       addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); 
     jPanel1Layout.setVerticalGroup(
       jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). 
       addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)); 
     layoutManager = jPanel1Layout; 

     javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1); 
     jPanel1Layout2.setHorizontalGroup(
       jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). 
       addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout2.createSequentialGroup(). 
       addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); 
     jPanel1Layout2.setVerticalGroup(
       jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING). 
       addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)); 
     layoutManager2 = jPanel1Layout2; 
     getContentPane().add(jPanel1); 
     pack(); 
    } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new LayoutTest().setVisible(true); 
      } 
     }); 
    } 
    private JButton jButton; 
    private JPanel jPanel1; 
    private LayoutManager layoutManager; 
    private LayoutManager layoutManager2; 
} 

이 코드는 작동하지 않습니다. 이유가 무엇입니까? 그러나 매번 actionListener에 레이아웃을 만들면 작동합니다.LayoutManager 저장

+0

'import javax.swing.X'를 사용하면 X를 직접 사용할 수 있습니다 (예 :'javax.swing.BorderLayout' 대신에'BorderLayout'). – MByD

답변

1

layoutmanager를 전환 한 후 validate()에 대한 호출을 포함하면됩니다.

public void actionPerformed(ActionEvent e) { 
    LayoutManager tempGroupLayout = jPanel1.getLayout(); 
    jPanel1.setLayout(layoutManager2); 
    getContentPane().validate(); // <= added line 
    layoutManager = layoutManager2; 
    layoutManager2 = tempGroupLayout; 
} 

참고 : 게시 된 코드에 일부 줄이 없습니다 (예 : jButton 초기화).

+0

작동하지 않습니다 – itun

+0

@itun "작동하지 않습니다"? 컴파일되지 않습니까? 버튼이 보이지 않나요? 클릭하면 레이아웃이 변경되지 않습니까? – Howard

+0

버튼을 눌러도 레이아웃이 바뀌지 않습니다 – itun