!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
于田網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
html
head
title無(wú)標(biāo)題文檔/title
script language="javascript"
function fsubmit(obj)
{
obj.submit();
}
function freset(obj){
obj.reset();
}
/script
/head
body bgcolor="#333333"
form id="form1" name="form1" method="post" action="conn.jsp"
table width="745" border="0" align="center"
tr
td width="100"?/td
td?/td
td width="104"?/td
/tr
tr
td?/td
td width="527" height="297" background="img/LoginBg.jpg" bgcolor="#F0F0F0"
table width="358" border="0"
tr
td width="70" height="51"?/td
td width="119"?/td
td width="160"?/td
/tr
tr
td height="46"?/td
tdfont color="#FFFFFF"用戶名:/font/td
tdinput type="text" name="user" style=" width:160px" id="user" //td
/tr
tr
td height="34"?/td
tdfont color="#FFFFFF"密??碼:/font/td
tdinput type="password" name="pswd" style="width:160px" id="pswd" //td
/tr
tr
td height="24"?/td
td?/td
td?/td
/tr
/table
table width="396" border="0"
tr
td width="82" height="30" /td
td width="114"img src="img/loginbutton.jpg" style="cursor:hand;" onclick="javascript:fsubmit(document.form1);"/td
td width="41" /td
td width="141"img src="img/canclebutton.jpg" style="cursor:hand;" onclick="javascript:freset(document.form1);"/td
/tr
/table /td
td?/td
/tr
tr
td?/td
td?/td
td?/td
/tr
/table
/form
/body
/html
%@ page language="java" import="java.util.*" contentType="text/html;charset=GB2312" import="java.sql.*"%
%@ page import="com.microsoft.sqlserver.jdbc.SQLServerDriver" %
%@page import="java.sql.DriverManager"%
HTML
HEAD
/HEAD
body
%
String userName=request.getParameter("user");
String Pswd =request.getParameter("pswd");
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
}
catch(ClassNotFoundException e)
{
out.print("0");
out.print(e.toString());
}
try
{
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=RegUser","sa","123");
Statement stmt = con.createStatement();
String sql="select * from tb_user";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
if(userName.equals(rs.getString(2))Pswd.equals(rs.getString(3)))
{
rs.close();
stmt.close();
con.close();
%
jsp:forward page="index.html" /jsp:forward
%
}
}
rs.close();
stmt.close();
con.close();
%
script type="text/javascript"
if(!confirm("用戶名或密碼錯(cuò)誤,是否要轉(zhuǎn)入注冊(cè)頁(yè)"))
{
location.href="MyHtml.html";
}
else
{
location.href="Register.html";
}
/script
%
}
catch(Exception e)
{
out.print("1");
out.print(e.toString());
}
%
/body
/HTML
首先你還是弄一個(gè)新的panel 來(lái)將“確定”“取消”兩個(gè)按鈕重新排布好吧,由于只有兩個(gè)按鈕這么簡(jiǎn)單我下面的程序用GirdLayout了,
例如:JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
panel.add(bw);
panel.add(bc);
然后再在你的Frame jp 里面加入這個(gè)panel,就可以了。按照你的設(shè)定就是用BorderLayout把它加到中間去吧? 北面的就是你原來(lái)做好的那些部分..(雖然不是很優(yōu)化,不過(guò)先不管了...)
例如:fr.getContentPane().add("North",jp);
fr.getContentPane().add("Center",panel);
整條程序就是:(注意我改了包的名字和類的名字,你要改回來(lái), 這里的參數(shù)也改了一下,符合我自己的審美觀 : fr.setSize(450, 200); )
package src;
/**
* @author Raven Denesis
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Map;
import javax.swing.*;
public class Jpassword {
private JFrame fr = new JFrame("登陸界面");
private JTextField user = new JTextField(20);
private JPasswordField pwd = new JPasswordField(10);
private JTextArea ta = new JTextArea(5,10);
private JButton bw = new JButton("確定");
private JButton bc = new JButton("取消");
Font ft = new Font ("serf",Font.BOLD,28);
JPanel jp = new JPanel (new GridLayout(2,3,10,10));
public static void main(String[] args){
Jpassword than = new Jpassword();
than.go();
}
void go(){
fr.getContentPane().setLayout(new BorderLayout(0,10));
JLabel u1 =new JLabel("用戶名: ",JLabel.LEFT);
jp.add(u1);
jp.add(user);
JLabel pl = new JLabel("用戶密碼: ",JLabel.LEFT);
jp.add(pl);
jp.add(pwd);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
panel.add(bw);
panel.add(bc);
fr.getContentPane().add("North",jp);
fr.getContentPane().add("Center",panel);
u1.setFont(ft);
pl.setFont(ft);
user.setFont(ft);
pwd.setFont(ft);
//user.addActionListener(new ActionListener());
//pwd.addActionListener(new TextHandler());
fr.setSize(450, 250);
fr.setVisible(true);
fr.setLocation(200, 200);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
abstract class TextHandler implements ActionListener
{
int sel;
TextHandler(int sel)
{
this.sel = sel;
}
}
public void actionPerformed(ActionEvent e)
{
String uname,upass;
uname = user.getText();
upass = new String(pwd.getPassword());
ta.setText("用戶名: "+"\n"+"密碼: "+upass);
}
}
運(yùn)行結(jié)果:
這樣的排版你覺(jué)得還可以吧?
還有你的按鈕bw和bc還沒(méi)有加到.addActionListener(new ActionListener()); 里,這樣的話按鈕就算按下去都沒(méi)反應(yīng)...不過(guò)你稍后應(yīng)該會(huì)加的了吧,我想就不用另行說(shuō)了....
按照你的要求編寫的Java的帶取消的警告信息框的完整程序如下
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.JOptionPane;
import?javax.swing.JPanel;
public?class?F?extends?JFrame?implements?ActionListener{
JButton?jb=new?JButton("確定");
JPanel?jp=new?JPanel();
F(){
jb.addActionListener(this);
jp.add(jb);
add(jp);
setSize(200,?200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public?static?void?main(String[]?args)?{
new?F();
}
@Override
public?void?actionPerformed(ActionEvent?ae)?{
if(ae.getSource()==jb){
int?n=JOptionPane.showConfirmDialog(this,?"跳往另一頁(yè)面?",?"跳轉(zhuǎn)警告",JOptionPane.WARNING_MESSAGE,?JOptionPane.OK_CANCEL_OPTION);
if(n==0){
FL?fl=new?FL();//創(chuàng)建新頁(yè)面
this.dispose();//銷毀舊頁(yè)面
}
}
}
}
class?FL?extends?JFrame{
JLabel?jl=new?JLabel("另一頁(yè)面");
JPanel?jp=new?JPanel();
FL(){
jp.add(jl);
add(jp);
setSize(200,?100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
運(yùn)行結(jié)果
public?void?initWin(){?
this.setBounds(300,?300,?500,?400);?
this.addWindowListener(new?WindowAdapter(){?
public?void?windowClosing(WindowEvent?e)?{?
super.windowClosing(e);
int?exi?=?JOptionPane.showConfirmDialog?(null,?"確定退出?",?"友情提示",?JOptionPane.YES_NO_OPTION,?JOptionPane.QUESTION_MESSAGE);
if(exi?==?JOptionPane.YES_OPTION)
{
System.exit?(0);
}
else
{
return;
}
}
});
layoutWin();?
this.setVisible(true);?
}
直接將這個(gè)函數(shù)加入到你所要退出系統(tǒng)的程序代碼之中,雖然說(shuō)這個(gè)函數(shù)有個(gè)BUG ,就是按了返回 他仍然退出系統(tǒng),不過(guò),看了代碼的邏輯應(yīng)該是不會(huì)出現(xiàn)這種情況,所以,有懂的大佬麻煩說(shuō)下~~~共同學(xué)習(xí)0.0 蛤蛤