JFrame面板内容跳转

leilei831202 2007-05-28 07:19:41

public class loginPanel extends JPanel //自定义继承JPanel的面板


mainFrame mf = (mainFrame)this.getParent(); //声明,获得该面板的父容器
mf.getContentPane().removeAll(); //移除面板所有组件

mf.getContentPane().add(new loginPanel()); //添加 PANEL组件
mf.repaint(); //重画
mf.validate(); //加载

为什么要报错
...全文
332 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
leilei831202 2007-05-29
  • 打赏
  • 举报
回复
有谁可以回答
leilei831202 2007-05-29
  • 打赏
  • 举报
回复
但是为什么这样会报错呢?
chunyou128 2007-05-29
  • 打赏
  • 举报
回复
顶,解决的话,要付上可以调试的代码哦!
chenweionline 2007-05-29
  • 打赏
  • 举报
回复
类似功能用CardLayout最方便
leilei831202 2007-05-28
  • 打赏
  • 举报
回复
应该是空指针?
但是不太明白为什么会报这个错
leilei831202 2007-05-28
  • 打赏
  • 举报
回复
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
fuke888 2007-05-28
  • 打赏
  • 举报
回复
报的什么错啊
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
package cn; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; //登陆面板 public class Login extends JFrame { Connection conn = null; Statement stmt = null; ResultSet rst = null; public Login() { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载驱动成功。"); String url = "jdbc:mysql://localhost/users"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); System.out.println("连接数据库成功。"); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rst = stmt.executeQuery("SELECT*FROM user"); } catch (Exception e) { System.out.println(e); } setTitle("登陆页面"); JPanel panel = new JPanel(); //JLabel lblName = new JLabel("用户名:"); JTextField txtName = new JTextField(10); //lblName.setHorizontalAlignment(JLabel.CENTER); panel.add(new JLabel("用户名:")); panel.add(txtName); JLabel lblPassword = new JLabel("密码:"); JPasswordField txtPassword = new JPasswordField(10); lblPassword.setHorizontalAlignment(JLabel.CENTER); panel.add(lblPassword); panel.add(txtPassword); add(panel, BorderLayout.CENTER); JPanel panel2 = new JPanel(); JButton btnLogin = new JButton("登陆"); /** * 给登陆按钮添加监听事件 当按钮被点击时时间被触发 */ btnLogin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean b = false; // boolean b = um.Login(txtName.getText(), txtPassword.getText()); try { stmt = conn.createStatement();// 预定义语句 // 数据库查询语句(根据用户名和密码) String sql = "select * from user where User='" + txtName.getText() + "' and Password='" + txtPassword.getText() + "'"; rst = stmt.executeQuery(sql);// 执行查询语句 // rst中有数据,则将标记改为true if (rst.next()) { b = true; } } catch (SQLException e1) { e1.printStackTrace(); } if (b) {// 登陆成功,跳转页面 JOptionPane.showMessageDialog(null, "登陆成功!"); new ProductQueryDemo();// 打开主页 dispose();// 关闭窗口 } else {// 登陆失败 JOptionPane.showMessageDialog(null, "登陆失败!"); } } }); JButton btnReset = new JButton("重置"); /** * 点击重置按钮时,将文本框中的值设置为空 */ btnReset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { txtName.setText(""); txtPassword.setText(""); } }); JButton btnSignin = new JButton("注册"); /** * 点击注册按钮时,调用注册程序 */ btnSignin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Signin(); dispose();// 关闭窗口 } }); panel2.add(btnLogin); panel2.add(btnReset); panel2.add(btnSignin); add(panel2, BorderLayout.PAGE_END); setSize(430, 150); setLocationRelativeTo(null); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Login(); } }
Java程序设计 课程设计报告 设计题目:电子日历设计 班级: 学号: 姓名: 1. 需求分析 设计并实现一个电子日历,当用户在下拉列表中选择年份后,显示某年各个月的日 历。 2. 概要设计 本程序要求实现在点击按钮以及在下拉菜单中进行点击选择时日历页面以及日期信 息行能够正确显示当前选择的日期,所以在源程序中应该在点击按钮时产生ActionEven t事件,修改当前显示日期以及显示当月日历;在菜单中选择年份时能够产生ItemEvent 事件,使得日历直接跳转到所选年份。 3. 详细设计 源程序分为三部分,主程序部分 CalendarMainClass,日历实现部分 CalendarClass, 页面实现部分 CalendarFrame。 主程序CalendarMainClass 声明并创建一个页面实现部分CalendarFrame类的对象,使用 setBounds() 方法设置初始位置以及窗口大小;使用setTitle() 方法设置窗口标题;使用setLocationRelativeTo()方法设置窗口居中显示:使用setVi sible()以及 setDefaultCloseOperation() 方法设置窗口可见和单击窗体右上角的关闭图标后程序会做出怎样的处理。 日历实现部分 CalendarClass 首先创建一个长度为42的字符串数组,用来存放日期;其后判断所选月份的长短, 并存入数组中。 页面实现部分 CalendarFrame 窗口页面的布局设计如下:新建一个JPanel面板 panel,将其设置为 BorderLayout布局,放置于窗口的NORTH区,将按钮上月previousMonth ,按钮下月nextMonth 放置在JPanel的对象pNorth中,并将菜单组件list和pNorth分别放置在panel的NORTH区 ,SOUTH区;新建一个JPanel面板pCenter,将pCenter设置为7行7列的GridLayout布局, 将星期组件和日期组件添加入pCenture中,把pCenture添加入一个ScrollPane的对象sc rollPane当中,将scrollPane放置在窗口的CENTER区;将日期信息组件showMessage 添加入JPanel的对象pSouth中,并将pSouth放置在窗口的SOUTH区。 为按钮上月previousMonth ,按钮下月nextMonth以及下拉菜单设置监视器,重写public void actionPerformed (ActionEvent e),public void itemStateChanged (ItemEvent e)方法,使得在进行相应操作时能够产生相应的时间。 页面实现部分 CalendarFrame 的图形化显示 4. 程序代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Calendar; public class CalendarMainClass { public static void main(String args[]) { CalendarFrame frame =new CalendarFrame(); frame.setBounds(0,0,800,550); frame.setTitle("Calendar"); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class CalendarClass { String day[]; int year=2010,month=0; public void setYear(int year) { this.year=year; } public int getYear() { return year; } public void setMonth(int month) { this.month=month; } public int getMonth() { return month; } public String[] getCalendar() { String a[]=new String[42]; Calendar date=Calendar.getInstance(); date.set(year,month-1,1); int week=date.get(Calendar.DAY_OF_WEEK)-1; int day=0; if(month==1"

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧