GUI卡布局-操作侦听器不起作用[重复]

问题描述 投票:0回答:1
我只有第一个按钮可以工作。直到我将第一个面板更新为具有其他面板(oneTwo,oneThree等)之前,它一直工作良好。我这样做是因为我必须使程序看起来像这样=> Example

但是现在当我单击第一个按钮时,它不再切换到第二张卡。我不知道。

这是我的代码

public class AddressBook implements ActionListener { static CardLayout contentPaneLayout; static JButton B1= new JButton("Create a New Address"), B2= new JButton("Load Contacts"), B3= new JButton("Add New Contacts"), B4= new JButton("View/Delete Contacts"), B5= new JButton("Backup Contacts"), B6= new JButton("Exit"), B7 = new JButton ("Cancel"); static JTextField TF1 = new JTextField(13), TF2 = new JTextField (13); static JLabel L1 = new JLabel ("Use The Buttons Below To Manage Contacts",JLabel.CENTER), L2 = new JLabel ("This is Card 2"), L3 = new JLabel("Username:",JLabel.CENTER), L4 = new JLabel("Number of Contacts:",JLabel.CENTER);; static Container contentPane; public void actionPerformed(ActionEvent e) { Object click = e.getSource(); if (click==B1) contentPaneLayout.show(contentPane,"Card 2"); if (click==B6) System.exit(0); if (click==B7) contentPaneLayout.show(contentPane, "Card 1"); } public static void main(String[] args) { JFrame frm = new JFrame("Address Book"); contentPane = frm.getContentPane(); contentPane.setLayout(new CardLayout()); JPanel one = new JPanel (new GridLayout( 5,5 )); JPanel two = new JPanel (new GridLayout(4,6)); JPanel oneTwo = new JPanel(new FlowLayout()); JPanel oneThree = new JPanel(new GridLayout(2,3,6,5)); JPanel oneFour = new JPanel(new GridLayout()); JPanel oneFive = new JPanel(new GridLayout()); one.add (L1); oneTwo.add (L3); oneTwo.add (TF1); TF1.setEditable(false); TF2.setEditable(false); oneTwo.add(L4); oneTwo.add(TF2); one.add(oneFive); one.add(oneTwo); oneThree.add(B1); oneThree.add(B2);oneThree.add(B3);oneThree.add(B4);oneThree.add(B5);oneThree.add(B6); one.add(oneFour); one.add(oneThree); two.add(L2); two.add(B7); contentPane.add("Card 1", one); contentPane.add("Card 2", two) ; //contentPaneLayout.show(contentPane, "Card 1"); ActionListener AL= new AddressBook(); B1.addActionListener(AL); B7.addActionListener(AL); B6.addActionListener(AL); frm.pack(); frm.setSize(550,250); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } }

java swing actionlistener cardlayout
1个回答
2
投票
您有NullPointerException

contentPaneLayout.show(contentPane, "Card 2");

contentPaneLayout从未分配值。

过度使用static会加剧这种情况,>

一点点的返工,您有一个更好的解决方案

import java.awt.CardLayout; import java.awt.Container; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class AddressBook implements ActionListener { private CardLayout contentPaneLayout; private JButton b1 = new JButton("Create a New Address"), b2 = new JButton("Load Contacts"), b3 = new JButton("Add New Contacts"), b4 = new JButton("View/Delete Contacts"), b5 = new JButton("Backup Contacts"), b6 = new JButton("Exit"), b7 = new JButton("Cancel"); private JTextField tf1 = new JTextField(13), tf2 = new JTextField(13); private JLabel l1 = new JLabel("Use The Buttons Below To Manage Contacts", JLabel.CENTER), l2 = new JLabel("This is Card 2"), l3 = new JLabel("Username:", JLabel.CENTER), l4 = new JLabel("Number of Contacts:", JLabel.CENTER); private Container contentPane; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new AddressBook(); } }); } public AddressBook() { JFrame frm = new JFrame("Address Book"); contentPane = frm.getContentPane(); contentPaneLayout = new CardLayout(); contentPane.setLayout(contentPaneLayout); JPanel one = new JPanel(new GridLayout(5, 5)); JPanel two = new JPanel(new GridLayout(4, 6)); JPanel oneTwo = new JPanel(new FlowLayout()); JPanel oneThree = new JPanel(new GridLayout(2, 3, 6, 5)); JPanel oneFour = new JPanel(new GridLayout()); JPanel oneFive = new JPanel(new GridLayout()); one.add(l1); oneTwo.add(l3); oneTwo.add(tf1); tf1.setEditable(false); tf2.setEditable(false); oneTwo.add(l4); oneTwo.add(tf2); one.add(oneFive); one.add(oneTwo); oneThree.add(b1); oneThree.add(b2); oneThree.add(b3); oneThree.add(b4); oneThree.add(b5); oneThree.add(b6); one.add(oneFour); one.add(oneThree); two.add(l2); two.add(b7); contentPane.add("Card 1", one); contentPane.add("Card 2", two); //contentPaneLayout.show(contentPane, "Card 1"); b1.addActionListener(this); b7.addActionListener(this); b6.addActionListener(this); frm.pack(); frm.setSize(550, 250); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { Object click = e.getSource(); System.out.println("..."); if (click == b1) { contentPaneLayout.show(contentPane, "Card 2"); } if (click == b6) { System.exit(0); } if (click == b7) { contentPaneLayout.show(contentPane, "Card 1"); } } }

重要的变化是在构造函数中...

contentPaneLayout = new CardLayout(); contentPane.setLayout(contentPaneLayout);

© www.soinside.com 2019 - 2024. All rights reserved.