2011-05-20 96 views
1

現在我有3個Java文件是。 我在TrainAndCOnductor上有兩個按鈕。我的目的是,當我點擊1st按鈕時,它將打開我的The Conductor面板。鏈接到其他面板一旦我點擊了按鈕

public class JavaApplication1 { 

    public static void main(String[] args) { 
     TheConductor form = new TheConductor(); 
     form.setVisible(true); 
    } 
} 
//Train And Conductor 
    public class TrainAndConductor extends javax.swing.JFrame { 

    /** Creates new form TrainAndConductor */ 
    public TrainAndConductor() { 
     initComponents(); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("Train Driver"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("Conductor"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE) 
      .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jButton2)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     add(TheConductor); 
     TheConductor.setVisible(true); 

//The Conductor 
public class TheConductor extends javax.swing.JFrame { 

    /** Creates new form TheConductor */ 
    public TheConductor() { 
     initComponents(); 
    } 

回答

2

試試這個:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    secondPanel.setVisible(true); 
} 

,如果你是不是之前再加入它,你也必須添加它想:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    add(secondPanel); 
    secondPanel.setVisible(true); 
} 

試試這個:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    TheConductor conduct = new TheConductor(); 
    add(conduct); 
    conduct.setVisible(true); 
} 
+0

遺憾,它不能正常工作,它說,非靜態方法調用setVisible(布爾)不能從靜態上下文 – 2011-05-20 05:54:14

+0

@George林引用:是你的'TrainAndConductor'變量全球和方便用這種方法? – 2011-05-20 05:56:05

+0

@George:你必須先創建'TrainAndConductor'面板,然後在對象上調用setVisible方法:'myTrainAndConductorObject.setVisible(true);' – Jonas 2011-05-20 05:56:47

3

這取決於你的佈局是如何組織的。您可以使用JTabbedPane或CardLayout來實現此目的。

+1

另請參閱此'CardLayout' [示例](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing/5655843#5655843)。 – trashgod 2011-05-20 05:53:20

+1

@Andrew Thompson:自從我編輯了鏈接之後,並感謝您的注意。 :-) – trashgod 2011-05-20 06:25:45

相關問題