1 import java.awt.*; 2 import javax.swing.*; 3 import java.awt.event.*; 4 public class TestLogin{ 5 public static void main(String [] args){ 6 JFrame frame = new LoginFrame(); 7 frame.setVisible(true); 8 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 9 } 10 } 11 12 class LoginFrame extends JFrame{ 13 public LoginFrame() { 14 this.setTitle("Login Frame"); 15 this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 16 17 18 setFrame(); 19 addListener(); 20 } 21 22 //设置界面布局 23 public void setFrame(){ 24 panInput.setLayout(new GridLayout(2,2)); 25 panInput.add(labUser); 26 panInput.add(txtUser); 27 panInput.add(labPass); 28 panInput.add(txtPass); 29 30 panBtn.setLayout(new FlowLayout()); 31 panBtn.add(btnLogin); 32 panBtn.add(btnRegist); 33 panBtn.add(btnCancel); 34 35 this.setLayout(new BorderLayout()); 36 this.add(panInput, BorderLayout.CENTER); 37 this.add(panBtn, BorderLayout.SOUTH); 38 } 39 //添加事件监听 40 public void addListener() { 41 btnLogin.addActionListener(new ActionListener() 42 { 43 public void actionPerformed(ActionEvent e) { 44 System.out.println("等待验证"); 45 String name = txtUser.getText(); 46 String pass = txtPass.getText(); 47 if(name.equals("asdf") && pass.equals("asdf")) { 48 System.out.println("验证通过等待登陆"); 49 } 50 } 51 } 52 ); 53 } 54 55 private JLabel labUser = new JLabel("用户名"); 56 private JLabel labPass = new JLabel("密码"); 57 private JTextField txtUser = new JTextField(); 58 private JPasswordField txtPass = new JPasswordField(); 59 60 private JButton btnLogin = new JButton("登陆"); 61 private JButton btnRegist = new JButton("注册"); 62 private JButton btnCancel = new JButton("取消"); 63 64 private JPanel panInput = new JPanel(); 65 private JPanel panBtn = new JPanel(); 66 private static final int DEFAULT_WIDTH = 250; 67 private static final int DEFAULT_HEIGHT = 150; 68 }
1 // TestMain 聊天界面及事件处理 2 import java.awt.*; 3 import javax.swing.*; 4 import java.awt.event.*; 5 import java.io.*; 6 public class TestMain{ 7 public static void main(String [] args){ 8 JFrame frame = new MainFrame(); 9 frame.setVisible(true); 10 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11 } 12 } 13 14 class MainFrame extends JFrame{ 15 public MainFrame() { 16 this.setTitle("Main Frame"); 17 this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 18 19 setFrame(); 20 readChatMessage(); 21 addListener(); 22 } 23 24 //设置界面布局 25 public void setFrame(){ 26 panInput.setLayout(new GridLayout(1,2)); 27 panInput.add(comBox); 28 panInput.add(btnSend); 29 30 panUser.setLayout(new GridLayout(2,1)); 31 panUser.add(txtMess); 32 panUser.add(panInput); 33 34 this.setLayout(new BorderLayout()); 35 this.add(panUser, BorderLayout.NORTH); 36 this.add(scrollPane, BorderLayout.CENTER); 37 } 38 //添加事件监听 39 public void addListener() { 40 txtMess.addKeyListener( new KeyAdapter() 41 { 42 public void keyPressed(KeyEvent e) { 43 if(e.getKeyCode() == e.VK_ENTER) { 44 txtContent.append(txtMess.getText() + "\n"); 45 saveChatMessage(); 46 txtMess.setText(""); 47 } 48 } 49 } 50 ); 51 52 btnSend.addActionListener(new ActionListener() 53 { 54 public void actionPerformed(ActionEvent e) { 55 txtContent.append(txtMess.getText() + "\n"); 56 saveChatMessage(); 57 txtMess.setText(""); 58 } 59 } 60 ); 61 } 62 63 //读取聊天记录 64 public void readChatMessage() { 65 try{ 66 File inFile = new File("F://mydata//chat03.txt"); 67 FileReader fr = new FileReader(inFile); 68 BufferedReader br = new BufferedReader(fr); 69 while(br.ready()) { 70 txtContent.append(br.readLine() + "\n"); 71 } 72 } 73 catch (IOException e){e.printStackTrace();} 74 } 75 //保存聊天记录 76 public void saveChatMessage() { 77 try{ 78 File outFile = new File("F://mydata//chat03.txt"); 79 FileWriter fw = new FileWriter(outFile,true); //如果不加true则直接替换 80 PrintWriter pw = new PrintWriter(fw); 81 pw.println(txtMess.getText()+ "\n"); 82 pw.flush();//如果不写flush 则无法将缓冲区的数据写入 83 } 84 catch (IOException e){e.printStackTrace();} 85 } 86 private JTextField txtMess = new JTextField(); 87 private JComboBox comBox = new JComboBox(); 88 private JButton btnSend = new JButton("发送"); 89 private JTextArea txtContent = new JTextArea(); 90 private JScrollPane scrollPane = new JScrollPane(txtContent); 91 92 private JPanel panInput = new JPanel(); 93 private JPanel panUser = new JPanel(); 94 95 private static final int DEFAULT_WIDTH = 400; 96 private static final int DEFAULT_HEIGHT = 250; 97 }
这两个一个是登陆界面一个是消息界面
java中的界面设置
窗体 Frame 面板Panel 各组件 xx 文本框 标签 菜单栏 按钮...
窗体上加载面板 面板上 增添组件 绘图等 (此处应有大图, 图呢...)
界面部分
JFrame frame = new XXFrame(); //创建一个实现了JFrame类的对象
设置窗体可见性 setVisible(true) 设置窗体默认可关闭选项 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
然后就是把具体的窗体以及组件 进行设置 添加事件监听
熟悉java的几个组件 如 文本框 JTextField 文本标签 按钮
接下来就是布局了 (布局管理器 LayoutManger)
常见的几个布局 GridLayout() GridLayout() 表格布局
FlowLayout() Flow 流式布局 就是按照顺序水平放置各组件 (默认居中哦)
BorerLayout 边框布局 东西南北中
用法: xx.setLayout(new BorderLayout()); xx.add(xx, BorderLayout.XX);
简言之 设置布局管理器, 然后按你设定布局管理器的方式把组件放上去就成
读取本地记录方面
IO流读取记录
文件记录是得在硬盘上,程序搁内存运行,要获取到文件记录,必须得作如下操作
1. 找到它 (定位数据源) File inFile = new File(String path);
2. 建立通道 (得有联系呀) FileInputStream / FileReader xx = new FileInputStream/FileReader(inFile);
3. 进行操作呗 (读写数据啊) xx.read(xx); while(br.ready()) ...
File inFile = new File(“F:\\mydata\\chat03.txt”); // 搁这里头定的是啥位呢 硬盘中指定的位置 File(String path)
\ Java中表示啥,反斜杠?转义字符 所以呢 文件分隔符用\\表示 或用File. separator 的方式
FileReader 直接用字符流读取文件信息 然后呢用BufferedReader 包装一下,就搁他默认的用。。。
原文:http://www.cnblogs.com/jiayouhgl/p/5316231.html