我无法通过面板。
这是一行:
cardLayout.show( cards, String.valueOf( "EXCEL_PANEL" ) );
我打电话给
EXCEL_PANEL
显示方法,它没有出现。我觉得这是一个盲目的错误。
我到底想要什么-
整个程序将运行在相同的框架中,但是我被困了一个星期,遇到了一个我无法意识到的相同问题。
还有一个问题。有谁知道如何通过按“登录”按钮来创建事件,而不继续执行此块中的其余操作?因为这仅仅是开始,而且很烦人。
这是我的cardlayout类代码:
public class CardLayoutManager {
JPanel cards;
final static String LOGIN_PANEL = "Card with Login elements";
final static String EXCEL_PANEL = "Card with Excel load file element";
public void addComponentToPane(Container pane) throws IOException {
LoginPanel login_p = new LoginPanel();
ExcelPanel excel_p = new ExcelPanel();
cards = new JPanel( new CardLayout() );
cards.add( login_p, LOGIN_PANEL );
cards.add( excel_p, EXCEL_PANEL );
pane.add( cards, BorderLayout.CENTER );
CardLayout cardLayout = (CardLayout) (cards.getLayout());
cardLayout.show( cards, String.valueOf( login_p ) );
login_p.login_btn.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
boolean identification = login_p.LoginCheck();
if (!identification) {
JOptionPane.showMessageDialog( login_p, "Sorry, this user does not have access to the system.", "warning", JOptionPane.WARNING_MESSAGE );
login_p.ClearFields();
} else {
cardLayout.show( cards, String.valueOf( "EXCEL_PANEL" ) );
}
} catch (SQLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} );
}
private static void createAndShowGUI() throws IOException {
USTCFrame frame = new USTCFrame();
CardLayoutManager demo = new CardLayoutManager();
demo.addComponentToPane( frame.getContentPane() );
frame.setVisible( true );
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" );
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put( "swing.boldMetal", Boolean.FALSE );
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (IOException e) {
e.printStackTrace();
}
}
} );
}
}
最佳答案
您没有说USTCFrame
,LoginPanel
和ExcelPanel
是什么类。
所以我假设USTCFrame
是JFrame
的子类LoginPanel
和ExcelPanel
是JPanel
的子类。
你的台词
cards = new JPanel( new CardLayout() );
cards.add( login_p, LOGIN_PANEL );
cards.add( excel_p, EXCEL_PANEL );
看起来很合理。但是那你的线
cardLayout.show( cards, String.valueOf( login_p ) );
没有道理。相反,它应该是
cardLayout.show( cards, LOGIN_PANEL );
因为
cards.show(...)
的第二个参数应该是您在
cards.add(...)
中使用的字符串。出于同样的原因,
cardLayout.show( cards, String.valueOf( "EXCEL_PANEL" ) );
你应该写
cardLayout.show( cards, EXCEL_PANEL );