点击按钮后链接到其他面板

时间:2011-05-20 05:45:44

标签: java swing user-interface layout actionlistener

现在我有3个Java文件。 我在TrainAndCOnductor上有2个按钮。我的目的是,当我点击第一个按钮时,它将打开我的导体面板。

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 个答案:

答案 0 :(得分:3)

这取决于您的布局的组织方式。您可以使用JTabbedPane或CardLayout来实现此目的。

答案 1 :(得分: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);
}