我为Base创建了一个JFrame
表单,并为页面替换目的创建了JPane
l。我已经创建了菜单栏与登录按钮。我在JPanel
上创建了LoginPage。
现在我想链接我的LoginPage登录按钮的基础框架。请帮帮我。我试过一个密码。当我点击登录按钮,什么也没发生。
- 登录页面名称:LoginPage.java
- 替换面板:panelWelcomePanel
- 登录按钮名称:menuLogin
private void menuLoginActionPerformed(java.awt.event.ActionEvent evt) {
LoginPage login = new LoginPage();
panelWelcomePanel.removeAll();
validate();
panelWelcomePanel.add(login);
validate();
}
登录页面代码:I am using NetBeans.
public class LoginPage extends javax.swing.JPanel {
/**
* Creates new form LoginPage
*/
public LoginPage() {
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() {
labeluName = new javax.swing.JLabel();
labeluPass = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
btnLogin = new javax.swing.JButton();
btnReset = new javax.swing.JButton();
loginBackground = new javax.swing.JLabel();
setMaximumSize(new java.awt.Dimension(800, 500));
setMinimumSize(new java.awt.Dimension(800, 500));
setLayout(null);
labeluName.setFont(new java.awt.Font("Serif", 3, 24)); // NOI18N
labeluName.setForeground(new java.awt.Color(204, 204, 255));
labeluName.setText("UserName :");
add(labeluName);
labeluName.setBounds(250, 200, 120, 40);
labeluPass.setFont(new java.awt.Font("Serif", 3, 24)); // NOI18N
labeluPass.setForeground(new java.awt.Color(204, 204, 255));
labeluPass.setText("Password :");
add(labeluPass);
labeluPass.setBounds(250, 260, 120, 50);
add(jTextField1);
jTextField1.setBounds(390, 200, 200, 30);
add(jPasswordField1);
jPasswordField1.setBounds(390, 270, 200, 30);
btnLogin.setForeground(new java.awt.Color(51, 51, 51));
btnLogin.setText("Login");
add(btnLogin);
btnLogin.setBounds(280, 390, 100, 40);
btnReset.setForeground(new java.awt.Color(51, 51, 51));
btnReset.setText("Reset");
add(btnReset);
btnReset.setBounds(450, 390, 100, 40);
loginBackground.setIcon(new javax.swing.ImageIcon("C:\Users\boparai\Desktop\LOGIN.jpg")); // NOI18N
loginBackground.setMaximumSize(new java.awt.Dimension(800, 500));
loginBackground.setMinimumSize(new java.awt.Dimension(800, 500));
loginBackground.setPreferredSize(new java.awt.Dimension(800, 500));
add(loginBackground);
loginBackground.setBounds(0, 0, 800, 500);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton btnLogin;
private javax.swing.JButton btnReset;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel labeluName;
private javax.swing.JLabel labeluPass;
private javax.swing.JLabel loginBackground;
// End of variables declaration
}
- 不要使用空布局。始终使用合适的布局管理器。
- 正如已经建议的,考虑使用
CardLayout
- 当从已经可见的父组件中添加/删除子组件时,在删除/添加子组件的代码之后调用
parent#revalidate()
和parent#repaint()
(按此顺序)