本文目录一览:
如何用Java编写用户注册界面?
界面建议用可视化来做,美观且便捷。下面这个是完全用代码写的,仅供参考。\x0d\x0aimport javax.swing.*;\x0d\x0aimport java.awt.event.*;\x0d\x0aimport java.awt.*;\x0d\x0aimport java.sql.*;\x0d\x0apublic class Register extends JFrame {\x0d\x0a JLabel jl1 = new JLabel("用户名");\x0d\x0a JTextField jt1 = new JTextField();\x0d\x0a JLabel jl2 = new JLabel("邮箱");\x0d\x0a JTextField jt2 = new JTextField();\x0d\x0a JLabel jl3 = new JLabel("密码");\x0d\x0a JPasswordField jpw1 = new JPasswordField();\x0d\x0a JLabel jl4 = new JLabel("密码确认");\x0d\x0a JPasswordField jpw2 = new JPasswordField();\x0d\x0a JButton register = new JButton("注册");\x0d\x0a JButton clean = new JButton("清空");\x0d\x0a public Register(){\x0d\x0a setLayout(new GridLayout(5,2));\x0d\x0a add(jl1);\x0d\x0a add(jt1);\x0d\x0a add(jl2);\x0d\x0a add(jt2);\x0d\x0a add(jl3);\x0d\x0a add(jpw1);\x0d\x0a add(jl4);\x0d\x0a add(jpw2);\x0d\x0a add(register);\x0d\x0a add(clean);\x0d\x0a String name = jt1.getText();\x0d\x0a String email = jt2.getText();\x0d\x0a String pw = jpw1.getText();\x0d\x0a register.addActionListener(new ActionListener(){\x0d\x0a public void actionPerformed(ActionEvent e){\x0d\x0a try{\x0d\x0a Class.forName("com.mysql.jdbc.Driver");\x0d\x0a Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");\x0d\x0a Statement sta = con.createStatement();\x0d\x0a sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)");\x0d\x0a JOptionPane.showMessageDialog(null,"注册成功","提示",JOptionPane.INFORMATION_MESSAGE);\x0d\x0a }\x0d\x0a catch(Exception ex){\x0d\x0a ex.getStackTrace();\x0d\x0a }\x0d\x0a }\x0d\x0a });\x0d\x0a clean.addActionListener(new ActionListener(){\x0d\x0a public void actionPerformed(ActionEvent e){\x0d\x0a jt1.setText("");\x0d\x0a jt2.setText("");\x0d\x0a jpw1.setText("");\x0d\x0a jpw2.setText("");\x0d\x0a }\x0d\x0a });\x0d\x0a }\x0d\x0a public static void main(String[] args){\x0d\x0a Register frame = new Register();\x0d\x0a frame.setTitle("用户注册");\x0d\x0a frame.setLocationRelativeTo(null);\x0d\x0a frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\x0d\x0a frame.setSize(400,400);\x0d\x0a frame.setVisible(true);\x0d\x0a }\x0d\x0a}
用Java语言设计一个界面,
首先:采用什么技术实现
java语言可以使用awt 和swing等技术实现图形界面
推荐使用Swing,因为Swing比AWT更专业,更漂亮,组件更丰富,功能更强大。
2. 其次:分析采用什么布局
边界布局BorderLayout,配合表格布局GridLayout,既简单又美观
3. 最后:分析需求中需要用的组件
学生姓名 学号 显示信息 需要用到文本框JTextField
单选按钮 需要用到组件 JRadioButton
复选框 需要用到组件 JCheckBox
组合框 需要用到组件 JComboBox
图片效果
参考代码如下
//导入所需要的包
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
import java.awt.*;
public class ClassFrame extends JFrame {// 写一个类继承自JFrame 窗体
// 定义组件
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfName, tfNum, allInfo;
private JRadioButton rb1, rb2;
private JCheckBox cb1, cb2, cb3;
private JComboBoxString t1, t2, t3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClassFrame frame = new ClassFrame();// 创建一个窗口实例
frame.setVisible(true);// 让该窗口实例可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 窗口属性的设置,内部组件的初始化
*/
public ClassFrame() {
setTitle("选课ing...");//标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭是退出JVM
setSize(450, 339);// 设置窗体大小
setLocationRelativeTo(null);// 窗体居中
contentPane = new JPanel();// 内容面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));// 设置布局
setContentPane(contentPane);
JPanel panel = new JPanel(new GridLayout(5, 1, 5, 10));//5行1列的表格布局
panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
contentPane.add(panel, BorderLayout.CENTER);//给panel添加边框
JPanel panel_1 = new JPanel();
panel.add(panel_1);
JLabel label = new JLabel("姓名");
panel_1.add(label);
tfName = new JTextField();
panel_1.add(tfName);
tfName.setColumns(10);
JLabel label_2 = new JLabel("学号");
panel_1.add(label_2);
tfNum = new JTextField();
tfNum.setColumns(10);
panel_1.add(tfNum);
rb1 = new JRadioButton("男");
panel_1.add(rb1);
rb1.setSelected(true);//设置单选按钮中,默认选择的按钮
rb2 = new JRadioButton("女");
panel_1.add(rb2);
ButtonGroup bts = new ButtonGroup();//单选按钮需要加入同一个ButonGroup中才能生效
bts.add(rb1);
bts.add(rb2);
JPanel panel_2 = new JPanel();
panel.add(panel_2);
cb1 = new JCheckBox("高等数学");
panel_2.add(cb1);
t1 = new JComboBoxString();
t1.setModel(new DefaultComboBoxModelString(new String[] { "林老师", "赵老师", "孙老师" }));
panel_2.add(t1);
JPanel panel_3 = new JPanel();
panel.add(panel_3);
cb2 = new JCheckBox("世界经济");
panel_3.add(cb2);
t2 = new JComboBoxString();
t2.setModel(new DefaultComboBoxModelString(new String[] { "张老师", "刘老师" }));
panel_3.add(t2);
JPanel panel_4 = new JPanel();
panel.add(panel_4);
cb3 = new JCheckBox("音乐赏析");
panel_4.add(cb3);
t3 = new JComboBoxString();
t3.setModel(new DefaultComboBoxModelString(new String[] { "王老师", "周老师" }));
panel_4.add(t3);
JPanel panel_5 = new JPanel();
panel.add(panel_5);
JButton jbOk = new JButton("确定");
panel_5.add(jbOk);
JButton jbRest = new JButton("重填");
panel_5.add(jbRest);
JPanel panelSouth = new JPanel();
contentPane.add(panelSouth, BorderLayout.SOUTH);
JLabel labe = new JLabel("选课信息");
labe.setHorizontalAlignment(SwingConstants.LEFT);
panelSouth.add(labe);
allInfo = new JTextField();
allInfo.setColumns(30);
panelSouth.add(allInfo);
JPanel panelNorth = new JPanel();
contentPane.add(panelNorth, BorderLayout.NORTH);
JLabel labelTitle = new JLabel("学生选课界面");
labelTitle.setForeground(Color.DARK_GRAY);
labelTitle.setFont(new Font("宋体", Font.BOLD, 20));
panelNorth.add(labelTitle);
//给确定按钮添加事件处理代码
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuilder info = new StringBuilder();
String name = tfName.getText();
String num = tfNum.getText();
String sex;
if (rb1.isSelected()) {
sex = "男";
} else {
sex = "女";
}
info.append(name + num + sex);
if (cb1.isSelected()) {
String c = cb1.getText();
String t = t1.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb2.isSelected()) {
String c = cb2.getText();
String t = t2.getSelectedItem().toString();
info.append(" " + c + t);
}
if (cb3.isSelected()) {
String c = cb3.getText();
String t = t3.getSelectedItem().toString();
info.append(" " + c + t);
}
allInfo.setText(info.toString());//把学生信息和选课信息放到文本框
}
});
//给重填按钮 设置事件处理代码
jbRest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tfName.setText("");
tfNum.setText("");
rb1.setSelected(true);
cb1.setSelected(false);
t1.setSelectedIndex(0);
cb2.setSelected(false);
t2.setSelectedIndex(0);
cb3.setSelected(false);
t3.setSelectedIndex(0);
allInfo.setText("");
}
});
}
}
java可以设计界面吗?
java可以做但jcreator不能画,只能手写代码,强烈建议用eclipse。
1.如果是web界面的话可以用eclipse+myeclipse插件。
2.如果是swt/jface或awt/swing界面的话可以用eclipse+SWTDesigner插件。
用java设计一个简单的界面设计,越简单越好,谢谢
用java设计一个简单的界面可以参考如下实例:
import javax.swing.JFrame;//框架
import javax.swing.JPanel;//面板
import javax.swing.JButton;//按钮
import javax.swing.JLabel;//标签
import javax.swing.JTextField;//文本框
import java.awt.Font;//字体
import java.awt.Color;//颜色
import javax.swing.JPasswordField;//密码框
import java.awt.event.ActionListener;//事件监听
import java.awt.event.ActionEvent;//事件处理
import javax.swing.JOptionPane;//消息窗口public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbluserLogIn;
public JLabel lbluserName;
public JLabel lbluserPWD;
public JTextField txtName;
public JPasswordField pwdPwd;
public JButton btnSub;
public JButton btnReset;
public UserLogIn(){
pnluser = new JPanel();
lbluserLogIn = new JLabel();
lbluserName = new JLabel();
lbluserPWD = new JLabel();
txtName = new JTextField();
pwdPwd = new JPasswordField();
btnSub = new JButton();
btnReset = new JButton();
userInit();
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
this.setSize(300,200);//设置框架大小为长300,宽200
this.setResizable(false);//设置框架不可以改变大小
this.setTitle("用户登录");//设置框架标题
this.pnluser.setLayout(null);//设置面板布局管理
this.pnluser.setBackground(Color.cyan);//设置面板背景颜色
this.lbluserLogIn.setText("用户登录");//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14));//设置标签字体
this.lbluserLogIn.setForeground(Color.RED);//设置标签字体颜色
this.lbluserName.setText("用户名:");
this.lbluserPWD.setText("密 码:");
this.btnSub.setText("登录");
this.btnReset.setText("重置");
this.lbluserLogIn.setBounds(120,15,60,20);//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20);
this.lbluserPWD.setBounds(50,85,60,25);
this.txtName.setBounds(110,55,120,20);
this.pwdPwd.setBounds(110,85,120,20);
this.btnSub.setBounds(85,120,60,20);
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e);
}
}
);
this.btnReset.setBounds(155,120,60,20);
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e);
}
}
);
this.pnluser.add(lbluserLogIn);//加载标签到面板
this.pnluser.add(lbluserName);
this.pnluser.add(lbluserPWD);
this.pnluser.add(txtName);
this.pnluser.add(pwdPwd);
this.pnluser.add(btnSub);
this.pnluser.add(btnReset);
this.add(pnluser);//加载面板到框架
this.setVisible(true);//设置框架可显
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText();
String pwd = String.valueOf(pwdPwd.getPassword());
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
return;
}else if(true){
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE);
return;
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("");
pwdPwd.setText("");
}
public static void main(String[] args){
new UserLogIn();
}
}
java能不能直接做界面窗口
Swing和AWT 这两个是java的包。用它们可以实现界面窗口。如果不知道做法,可以百度一下。java的API帮助文档一定要有,当遇到用Swing和AWT的一些用法不知道时可以查询帮助文档。java窗口和mysql的链接基本和做网站差不多,具体看你是准备做成本机应用程序,还是B/S结构。