2011-05-20 51 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

相关问题