初到華信

最近在華信上的幾節課讓我受益匪淺,斌哥指導我們學習了界面設計,事件機制。理論與實踐結合的學習方式讓我初步瞭解了Java基礎與Java編碼規範。通過與幾位指導教師的交流溝通開闊了視野,看清了自己的位置,以及對自己就業的方向又多了一個可能的選擇,儘管未來未必一定會從事Android與Java的開發,不過對學長們的就業經驗來看這的確是一個很好的就業方向。

三節課的學習也讓我獨立的編寫了200多行代碼,實現了QQ,五子棋的簡單界面和按鈕,鼠標事件的初步設計。

//QQ代碼:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class QQ2{


JTextField fieldName; 
JPasswordField fieldPwd;
JFrame loginFrame;
        JFrame mainFrame;

//顯示登陸界面的方法
public void showLoginFrame(){


loginFrame = new JFrame();
loginFrame.setSize(300,200);
loginFrame.setTitle("QQ界面");
loginFrame.setDefaultCloseOperation(3);
loginFrame.setLocationRelativeTo(null);
FlowLayout layout = new FlowLayout(1,10,20);
loginFrame.setLayout(layout);


JLabel lableName = new JLabel("用戶名");
JLabel lablePwd = new JLabel("密    碼");
fieldName = new JTextField(18); 
fieldPwd = new JPasswordField(18);
JButton buttonRegiste = new JButton("註冊");
JButton buttonLogin = new JButton("登陸");


loginFrame.add(lableName);
loginFrame.add(fieldName);
loginFrame.add(lablePwd);
loginFrame.add(fieldPwd);
loginFrame.add(buttonRegiste);
loginFrame.add(buttonLogin);

//創建動作監聽器對象
ActionListener listener = new ActionListener(){
//監控按鈕被點擊的操作
public void actionPerformed(ActionEvent e){
//登陸業務處理
//1.獲取用戶輸入的信息
String name = fieldName.getText();
String pwd = fieldPwd.getText();
//驗證信息(判斷)
if(name.equals(pwd)){
loginFrame.setVisible(false);
showMainFrame();
}else{
JOptionPane.showMessageDialog(null,"對不起,登陸失敗!");
}
}
};
//對登陸按鈕進行監聽
buttonLogin.addActionListener(listener);


loginFrame.setVisible(true);
}




//顯示主界面的方法
public void showMainFrame(){


mainFrame = new JFrame();
mainFrame.setSize(300,600);
mainFrame.setTitle("主界面");
mainFrame.setLocationRelativeTo(null);
FlowLayout layout = new FlowLayout(1,100,20);
mainFrame.setLayout(layout);


       for(int i=1;i<=10;i++){
final JButton buttonFriend = new JButton("好友"+i);
mainFrame.add(buttonFriend);
        //創建監聽器對象
      ActionListener listener = new ActionListener(){


         public void actionPerformed(ActionEvent e){
          String friendName = buttonFriend.getText();
          showTalkFrame(friendName);
        
       }
          
    };
     //對好友按鈕進行監聽
     buttonFriend.addActionListener(listener);
  }
      mainFrame.setVisible(true);
}


//顯示聊天主界面
public void showTalkFrame(String friend){


final JFrame talkFrame = new JFrame();
talkFrame.setSize(400,450);
talkFrame.setTitle("與"+friend+"正在聊天中...");
talkFrame.setLocationRelativeTo(null);


//創建線性佈局對象及其佈局
FlowLayout layout = new FlowLayout();
talkFrame.setLayout(layout);


//創建組件對象
final JTextArea textAreaShow = new JTextArea(10,30);
final JTextArea textAreaSent = new JTextArea(8,30);
JButton buttonSent = new JButton("發送");
JButton buttonExit = new JButton("關閉");


talkFrame.add(textAreaShow);
talkFrame.add(textAreaSent);
talkFrame.add(buttonExit);
talkFrame.add(buttonSent);


               //創建事件監聽器
      
        ActionListener listener = new ActionListener(){
        
  public void actionPerformed(ActionEvent e){
    String flag = e.getActionCommand();
if(flag.equals("發送")){
  String text = textAreaSent.getText();
textAreaShow.append(text+"\n");
textAreaSent.setText("");


}
else if(flag.equals("關閉")){
    talkFrame.setVisible(false);
        }


     }           


};


buttonSent.addActionListener(listener);
buttonExit.addActionListener(listener);


talkFrame.setVisible(true);
}


//主方法
public static void main (String[] args){


QQ2 qq = new QQ2();
qq.showLoginFrame();

}


}


//五子棋代碼:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FiveChess{


  Graphics g;

public void mainUI(){
     
JFrame chessFrame = new JFrame("五子棋");
chessFrame.setLayout(new FlowLayout());
chessFrame.setSize(530,470);
chessFrame.setLocationRelativeTo(null);
chessFrame.setDefaultCloseOperation(3);

//paint()畫圖
  JPanel qipanPanel = new JPanel(){


public void paint(Graphics g){


super.paint(g);

for(int i=0;i<10;i++){
g.drawLine(40,40*(i+1),400,40*(i+1));
}
for(int i=0;i<10;i++){
g.drawLine(40*(i+1),40,40*(i+1),400);
}
}


   };



    JPanel functionPanel = new JPanel(); 
     
qipanPanel.setPreferredSize(new Dimension(420,420));
qipanPanel.setBackground(Color.red);

        functionPanel.setPreferredSize(new Dimension(80,420));
functionPanel.setBackground(Color.blue);


        //將兩個面板添加到窗體裏面
chessFrame.add(qipanPanel);
chessFrame.add(functionPanel);


         JButton start = new JButton("開始");
         JButton renren = new JButton("人人");
         JButton renji = new JButton("人機");
         JButton guanyu = new JButton("關於");
         JButton Exit =new JButton("退出");
         
  functionPanel.setLayout(new FlowLayout(1,2,40));
        functionPanel.add(start);
functionPanel.add(renren);
functionPanel.add(renji);
functionPanel.add(guanyu);
functionPanel.add(Exit);

         //創建鼠標監聽器對象
MouseListener listener = new MouseListener(){



//監控進入
public void mouseEntered(MouseEvent e){

}
//監控退出
public void mouseExited(MouseEvent e){

}
//監控單擊
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
g.fillOval(x-10,y-10,20,20);

}
//監控按下
public void mousePressed(MouseEvent e){

}
//監控釋放
public void mouseReleased(MouseEvent e){

}

};


        qipanPanel.addMouseListener(listener);


chessFrame.setVisible(true);


g = qipanPanel.getGraphics();
}


public static void main(String[] args){


FiveChess chess = new FiveChess();   


chess.mainUI();


}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章