十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
import javax.swing.*;
成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,先為洱源等服務(wù)建站,洱源等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為洱源企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class GUI extends JFrame implements ActionListener {//實(shí)現(xiàn)監(jiān)聽(tīng)接口
JTextField field = new JTextField(15);
double num1=0,num2;
char op;
public GUI(){
setLayout(new BorderLayout(10,10));
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1.add(new JLabel("計(jì)算框:"));
p1.add(field);
add(p1,BorderLayout.NORTH);
JPanel p2 = new JPanel(new GridLayout(4,1));
//添加按鈕
JButton bt1 = new JButton("1");
JButton bt2 = new JButton("2");
JButton bt3 = new JButton("3");
JButton bt4 = new JButton("4");
JButton bt5 = new JButton("5");
JButton bt6 = new JButton("6");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt0 = new JButton("0");
JButton btDel = new JButton("Del");
JButton btEqual = new JButton("=");
p2.add(bt1);p2.add(bt2);
p2.add(bt3); p2.add(bt4);
p2.add(bt5); p2.add(bt6);
p2.add(bt7);p2.add(bt8);
p2.add(bt9);p2.add(bt0);
p2.add(btDel);p2.add(btEqual);
add(p2,BorderLayout.CENTER);
JPanel p3 = new JPanel(new GridLayout(0,1));
JButton bta = new JButton("+");
JButton btb = new JButton("-");
JButton btc = new JButton("*");
JButton btd = new JButton("/");
p3.add(bta);p3.add(btb);
p3.add(btc);p3.add(btd);
add(p3,BorderLayout.EAST);
//添加監(jiān)聽(tīng)器
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
bta.addActionListener(this);
btb.addActionListener(this);
btc.addActionListener(this);
btd.addActionListener(this);
btDel.addActionListener(this);
btEqual.addActionListener(this);
}
//事件的響應(yīng)
public void actionPerformed(ActionEvent e){
String str =e.getActionCommand();
String s1 = field.getText();
if(str!="+"str!="-"str!="*"str!="/"str!="="str!="Del"){//為數(shù)字按鈕時(shí)
field.setText(s1+str);
}
else if(str=="Del"){//刪除鍵
field.setText("");
str=null;
}
else if(str=="="){//等號(hào)按鈕
s1 =field.getText();
String t2 ="";
int flag=0;//標(biāo)記操作符的位置
if(s1.indexOf("+")!=-1)
flag = s1.indexOf("+");
if(s1.indexOf("-")!=-1)
flag = s1.indexOf("-");
if(s1.indexOf("*")!=-1)
flag = s1.indexOf("*");
if(s1.indexOf("/")!=-1)
flag = s1.indexOf("/");
for(int i=flag+1;is1.length();i++)
{
t2+=s1.charAt(i);//獲得第二個(gè)操作數(shù)
}
num2 =Double.parseDouble(t2);//將第二個(gè)操作數(shù)轉(zhuǎn)換為double
switch(op){
case '+': num1+=num2;break;
case '-': num1-=num2;break;
case '*': num1*=num2;break;
case '/': num1/=num2;break;
}
field.setText(s1+"="+num1+"");//顯示在文本框里
}
else{//加減乘除符號(hào)
num1 = Double.parseDouble(field.getText());
field.setText(s1+str);
op = str.charAt(0);//獲得操作符
}
}
public static void main(String[] args){
GUI frame = new GUI();
frame.setTitle("簡(jiǎn)易計(jì)算器");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
//frame.pack();
frame.setVisible(true);
}
}
不懂再問(wèn)
在Eclipse中查看JDK類庫(kù)的源代碼
設(shè)置:
1.點(diǎn) “window”- "Preferences" - "Java" - "Installed JRES"
2.此時(shí)"Installed JRES"右邊是列表窗格,列出了系統(tǒng)中的 JRE 環(huán)境,選擇你的JRE,然后點(diǎn)邊上的 "Edit...", 會(huì)出現(xiàn)一個(gè)窗口(Edit JRE)
3.選中rt.jar文件的這一項(xiàng):“c:\program files\java\jre_1.5.0_06\lib\rt.jar”?
點(diǎn) 左邊的“+” 號(hào)展開(kāi)它
4.展開(kāi)后,可以看到“Source Attachment:(none)”,點(diǎn)這一項(xiàng),點(diǎn)右邊的按鈕“Source Attachment...”, 選擇你的JDK目錄下的 “src.zip”文件
5.一路點(diǎn)"ok",結(jié)束。
dt.jar是關(guān)于運(yùn)行環(huán)境的類庫(kù),主要是swing的包?
tools.jar是關(guān)于一些工具的類庫(kù)?
rt.jar包含了jdk的基礎(chǔ)類庫(kù),也就是你在java doc里面看到的所有的類的class文件
使用:
可以在 Java 源代碼編輯器或代碼片段編輯測(cè)試窗中選擇類型、方法或字段的名稱,然后對(duì)元素的定義打開(kāi)編輯器。
在 Java 編輯器中,選擇類型、方法或字段的名稱。您也可以僅僅在名稱中單擊一次。?
執(zhí)行下列其中一項(xiàng)操作:?
1.從菜單欄中,選擇瀏覽 打開(kāi)聲明?
2.從編輯器的彈出菜單中,選擇打開(kāi)聲明?
3.按 F3 鍵,如下圖
概述
具體框架使用jframe,文本框組件:JTextField;密碼框組件:JPasswordField;標(biāo)簽組件:JLabel;復(fù)選框組件:JCheckBox;單選框組件:JRadioButton;按鈕組件JButton。
登錄界面:
代碼實(shí)例
import javax.swing.*;
import java.awt.*; ? //導(dǎo)入必要的包
public class denglu extends JFrame{
JTextField jTextField ;//定義文本框組件
JPasswordField jPasswordField;//定義密碼框組件
JLabel jLabel1,jLabel2;
JPanel jp1,jp2,jp3;
JButton jb1,jb2; //創(chuàng)建按鈕
public denglu(){
jTextField = new JTextField(12);
jPasswordField = new JPasswordField(13);
jLabel1 = new JLabel("用戶名");
jLabel2 = new JLabel("密碼");
jb1 = new JButton("確認(rèn)");
jb2 = new JButton("取消");
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//設(shè)置布局
this.setLayout(new GridLayout(3,1));
jp1.add(jLabel1);
jp1.add(jTextField);//第一塊面板添加用戶名和文本框
jp2.add(jLabel2);
jp2.add(jPasswordField);//第二塊面板添加密碼和密碼輸入框
jp3.add(jb1);
jp3.add(jb2); //第三塊面板添加確認(rèn)和取消
// ? ? ? ?jp3.setLayout(new FlowLayout()); ?//因?yàn)镴Panel默認(rèn)布局方式為FlowLayout,所以可以注銷(xiāo)這段代碼.
this.add(jp1);
this.add(jp2);
this.add(jp3); ?//將三塊面板添加到登陸框上面
//設(shè)置顯示
this.setSize(300, 200);
//this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("登陸");
}
public static void main(String[] args){
new denglu();
}
}
拓展內(nèi)容
java swing包
Swing 是一個(gè)為Java設(shè)計(jì)的GUI工具包。
Swing是JAVA基礎(chǔ)類的一部分。
Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。
Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫(xiě)成,所以同Java本身一樣可以跨平臺(tái)運(yùn)行,這一點(diǎn)不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各種操作系統(tǒng)默認(rèn)的特有主題),然而不是真的使用原生平臺(tái)提供的設(shè)備,而是僅僅在表面上模仿它們。這意味著你可以在任意平臺(tái)上使用JAVA支持的任意面板。輕量級(jí)組件的缺點(diǎn)則是執(zhí)行速度較慢,優(yōu)點(diǎn)就是可以在所有平臺(tái)上采用統(tǒng)一的行為。
概念解析:
JFrame?– java的GUI程序的基本思路是以JFrame為基礎(chǔ),它是屏幕上window的對(duì)象,能夠最大化、最小化、關(guān)閉。
JPanel?– Java圖形用戶界面(GUI)工具包swing中的面板容器類,包含在javax.swing 包中,可以進(jìn)行嵌套,功能是對(duì)窗體中具有相同邏輯功能的組件進(jìn)行組合,是一種輕量級(jí)容器,可以加入到JFrame窗體中。。
JLabel?– JLabel 對(duì)象可以顯示文本、圖像或同時(shí)顯示二者??梢酝ㄟ^(guò)設(shè)置垂直和水平對(duì)齊方式,指定標(biāo)簽顯示區(qū)中標(biāo)簽內(nèi)容在何處對(duì)齊。默認(rèn)情況下,標(biāo)簽在其顯示區(qū)內(nèi)垂直居中對(duì)齊。默認(rèn)情況下,只顯示文本的標(biāo)簽是開(kāi)始邊對(duì)齊;而只顯示圖像的標(biāo)簽則水平居中對(duì)齊。
JTextField?–一個(gè)輕量級(jí)組件,它允許編輯單行文本。
JPasswordField?– 允許我們輸入了一行字像輸入框,但隱藏星號(hào)(*) 或點(diǎn)創(chuàng)建密碼(密碼)
JButton?– JButton 類的實(shí)例。用于創(chuàng)建按鈕類似實(shí)例中的 "Login"。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登錄");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("確定");
private JButton cancel = new JButton("取消");
public Login(){
frame.setSize(300,200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
//頂部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系統(tǒng)管理員登錄"));
c.add(titlePanel,"North");
//中部表單
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("用戶名:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("密 碼:");
l2.setBounds(50, 60, 50, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
username.setBounds(110,20,120,20);
password.setBounds(110,60,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel,"Center");
//底部按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
new Login();
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陸賬戶", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陸密碼", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系統(tǒng)登錄");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 錄");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("關(guān) 閉");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帳號(hào)不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密碼不可為空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:數(shù)據(jù)校驗(yàn) success = executeQuery(sql);
if (success) {
// TODO: 如果數(shù)據(jù)校驗(yàn)成功 顯示主界面 并關(guān)閉登錄界面
JOptionPane.showMessageDialog(this, "成功登錄", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帳號(hào)或密碼錯(cuò)誤!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密碼框選中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 輸入密碼后回車(chē)相當(dāng)于點(diǎn)擊了登錄按鈕
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望對(duì)你有幫助
在Eclipse中查看Java類庫(kù)的源代碼,可以通過(guò)Ctrl+鼠標(biāo)左鍵的方式來(lái)完成;在下載JDK并安裝的時(shí)候,會(huì)有一個(gè)src.zip文件,此文件就是Java類庫(kù)的源碼。但是有時(shí)候,會(huì)出現(xiàn)源碼未找到的問(wèn)題,此時(shí)可以通過(guò)對(duì)其設(shè)置來(lái)解決。
以JDK1.8為例,查看一下src.zip文件
如果發(fā)現(xiàn)找不到源代碼,則進(jìn)行設(shè)置
點(diǎn)擊Attach Source按鈕,彈出視圖,把JDK下的src.zip文件傳入點(diǎn)擊OK即可
此時(shí)按住Ctrl+鼠標(biāo)左鍵指向Java類庫(kù)當(dāng)中的類,就可以查看類的源代碼
以O(shè)bject類為例演示