现在我有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();
}
遗憾,它不能正常工作,它说,非静态方法调用setVisible(布尔)不能从静态上下文 – 2011-05-20 05:54:14
@George林引用:是你的'TrainAndConductor'变量全球和方便用这种方法? – 2011-05-20 05:56:05
@George:你必须先创建'TrainAndConductor'面板,然后在对象上调用setVisible方法:'myTrainAndConductorObject.setVisible(true);' – Jonas 2011-05-20 05:56:47