我有一个名为AddAccounts
的类class AddAccounts extends JPanel {
JPanel panelCont; //Panel deck
CardLayout cl;
public AddAccounts() {
panelCont=new JPanel();
cl = new CardLayout();
panelCont.setLayout(cl);//set Panel Layout to CardLayout
setPreferredSize(new Dimension(1013, 513));//Set Default Size
/* Add Panels to the main window or integrate the panels*/
panelCont.add(new Panel1(), "1");
panelCont.add(new Panel2(),"2");
panelCont.add(new Panel3(),"3");
cl.show(panelCont, "1");
add(panelCont);
}
public void goNext() {
cl.show(panelCont, "2");
// cl.nxet(panelCont);
System.out.println("method called"); //for debugging purpose
}
public void showFirstPanel(){
cl.show(panelCont, "1");
}
}
两个名为 Panel1和panel2()的外部(单独文件)类。我想在 panel1 中按下按钮时更改卡片(从panel1到panel2 ),这就是我创建上面列出的方法goNext()的原因, 但问题是我无法更换卡片。这里我如何尝试在Panel1中调用goNext()方法
@Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == nextBtn) {
setMainCategory((String)mainCat.getSelectedItem());
}
}
public void setMainCategory(String mainCategory){
this.mainCategory=mainCategory;
new AddAccounts().goNext();
//System.out.println("From set Method: "+this.mainCategory);
}
一切正常, System.out.println("方法称为"); //用于调试目的在控制台窗口上执行并打印,但面板(卡)不会更改。 请帮助..如何使它工作。
@haraldK
你说的是绝对正确的。但它并没有帮助我,我试图实现的概念是我有一个名为mainPanel的JFrame,而在mainPanel中我有一个JSplitPanel
JSplitPane rootPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, userPanel, mainContentPane);
mainContentPane包含
JTabbedPane mainContentPane = new JTabbedPane();
mainContentPane.setPreferredSize(new Dimension(900, 550));
mainContentPane.setTabPlacement(JTabbedPane.TOP);
mainContentPane.addTab("Your Database", new UserRecords());
mainContentPane.addTab("Add Accounts", new AddAccounts());//here is the AddAccounts JPanel
mainContentPane.addTab("Update Existing Data", new EditRecords());
mainContentPane.setSelectedIndex(1);
我在Panel1,Panel2上放置按钮,而不是在父Frame AddAccounts上放置按钮。这里方法goNext属于AddAccounts,我想访问它以在Panel1中按下按钮时更改面板(从1到2)。我不能直接调用goNext()方法,因为它是AddAccounts类的方法而不是Panel1。并且new AddAccounts().goNext();
没有帮助
目录结构:
mypackage的
-UserPanel(MainPanelFrame,userPanelJPanel,mainContentPaneTabbedPane)
-AddAccounts(goNext())
-Panel1(nextBtn)
-Panel2(nextBtn)
答案 0 :(得分:3)
问题是不 <LinearLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="400dp">
代码,而是采用CardLayout
方法:
setMainCategory
每次调用方法时,都会在此创建new AddAccounts().goNext();
的新实例。在此实例上翻转面板(这就是您看到调试输出的原因),但组件永远不会添加到父级(如AddAccounts
或JFrame
),因此从未显示过。方法退出后,生成的组件就会被丢弃。
相反,您需要向父组件添加一个JPanel
,而在AddAccounts
方法中,您只需在此实例上调用setMainCategory
。
这是一个功能齐全的程序,演示了我上面尝试解释的所有内容:
goNext()
答案 1 :(得分:0)
试图创建一个功能性示例。希望这会对你有所帮助。
import java.awt.CardLayout;
/**
*
* @author user
*/
public class MyFrame extends javax.swing.JFrame {
/**
* Creates new form MyFrame
*/
public MyFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
deck = new javax.swing.JPanel();
firstCard = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
secondCard = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
deck.setLayout(new java.awt.CardLayout());
firstCard.setLayout(new java.awt.BorderLayout());
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("First");
firstCard.add(jLabel1, java.awt.BorderLayout.CENTER);
deck.add(firstCard, "card2");
secondCard.setLayout(new java.awt.BorderLayout());
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Second");
secondCard.add(jLabel2, java.awt.BorderLayout.CENTER);
deck.add(secondCard, "card3");
getContentPane().add(deck, java.awt.BorderLayout.CENTER);
jButton1.setLabel("Go Next");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, java.awt.BorderLayout.PAGE_START);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
((CardLayout)deck.getLayout()).next(deck);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel deck;
private javax.swing.JPanel firstCard;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel secondCard;
// End of variables declaration
}