本文目錄一覽:
如何用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結構。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/254976.html