package 中華隊數據_亂玩的;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NewJPanel_0816 {
public static void main(String[] args) {
JFrame n2=new JFrame();//建立一個視窗
NewJPanel p2=new NewJPanel(n2);//建立一個面板,並將視窗傳入
n2.add(p2);//視窗中新增面板p2
n2.setVisible(true);//顯示視窗
}
}
--------------------------------------------------------------------
package 中華隊數據_亂玩的;
//面板2中除了按鈕名稱和麵板1不一樣,其他基本功能相同
import javax.swing.JFrame;
//面板1
public class NewJPanel extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
JFrame n2=new JFrame();//建立一個視窗
public NewJPanel(JFrame n2) {
initComponents();
this.n2=n2;//通過建構函式接收之前傳過來的面板
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();//新增按鈕
jButton1.setText("測試1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
NewJPanel2 p1=new NewJPanel2(n2);//新建面板NewJPanel2,並將視窗n2傳入
setVisible(false);//隱藏當前面板
n2.add(p1);//在視窗中新增面板p1
n2.setVisible(true);//顯示面板
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton1)
.addContainerGap(188, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jButton1)
.addContainerGap(187, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
------------------------------------------------
package 中華隊數據_亂玩的;
import javax.swing.JFrame;
public class NewJPanel2 extends javax.swing.JPanel {
/**
* Creates new form NewJPanel
*/
JFrame n2=null;
public NewJPanel2(JFrame n2) {
initComponents();
this.n2=n2;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton1.setText("測試2");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
NewJPanel p=new NewJPanel(n2);
setVisible(false);
n2.add(p);
n2.setVisible(true);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(115, 115, 115)
.addComponent(jButton1)
.addContainerGap(188, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jButton1)
.addContainerGap(187, Short.MAX_VALUE))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}