GUI 图形用户界面
JavaGUI的容器
首层容器:JWindow JFrame(默认BorderLayout) JDialog
中间容器:JPanel(默认FLowlayout)
内容面板:Container
AWT:使用操作系统本身,跨平台时效果不一样
Swing:效果一样,跨平台
Swing 程序建立步骤:
①建立容器
②建立组件
③组件添加到容器
④设置布局
⑤添加事件
Swing 容器
JApplet 浏览器中运行的容器
JFrame 顶层容器,不能包含在其他容器中
JPanel 举行区域,页面
JScrollPane
JDialog
布局管理器:
主要有FLowlayout 从左到右从上到下
BorderLayout EWSN Center
GridLayout(行,列,行宽,列宽);
CardLayout
卡片布局实现过程
JPanel使用卡片布局,添加对应用卡片页面,设置时间实现卡片切换
事件:
步骤:
①建立事件源
②为事件源对象选择合适事件监听器
③为监听器添加合适处理程序
④为监听器与事件源建立联系,绑定,将监听器对象注册到事件源上
定义监听器可选方法:
①GUI程序本身实现监听(不好,违背单一原则,addActionListener(this))
②内部类定义监听器类 new出来,在add
③使用匿名内部类
第二三种成为事件驱动的标准
各种组件使用,注意,得到内容,事件驱动程序如下:
1 package com.Frame; 2 3 import java.awt.Container; 4 import java.awt.Font; 5 import java.awt.GraphicsConfiguration; 6 import java.awt.HeadlessException; 7 import java.awt.Toolkit; 8 9 import javax.swing.JButton; 10 import javax.swing.JCheckBox; 11 import javax.swing.JComboBox; 12 import javax.swing.JFrame; 13 import javax.swing.JLabel; 14 import javax.swing.JPasswordField; 15 import javax.swing.JRadioButton; 16 import javax.swing.JTextArea; 17 import javax.swing.JTextField; 18 19 /** 20 * @Author: YuanqiuChen 21 * @Date:2016-3-11-下午1:09:32 22 * 23 * 24 */ 25 26 public class MyFrame extends JFrame{ 27 28 private int width = 1024; 29 private int height = 1024 * 618 / 1000; 30 private Container contentP; 31 private JLabel label; 32 private JTextField text; 33 private JPasswordField passwordField; 34 private JButton button; 35 private JRadioButton redioBtn; 36 private JCheckBox checkBox; 37 private JComboBox comboBox; 38 private JTextArea textBox; 39 40 41 42 public MyFrame() throws HeadlessException { 43 super(); 44 this.setTitle("haha"); 45 46 this.setSize(width, height); 47 //设置居中 48 this.setLocationRelativeTo(null); 49 50 51 //左上角为原点,距离它为XY 52 Toolkit tool = Toolkit.getDefaultToolkit(); 53 int x = (int)tool.getScreenSize().getWidth(); 54 int y = (int)tool.getScreenSize().getHeight(); 55 this.setLocation((x-this.width)/2, (y-this.height)/2); 56 57 //设置标题图片 58 this.setIconImage(tool.createImage("C:/Users/Administrator/Desktop/11.jpg")); 59 // this.setIconImage(tool.createImage("img/e.png")); 60 61 this.addContent(); 62 this.setVisible(true); 63 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 64 } 65 66 public void addContent(){ 67 68 //容器,获得当前容器的内容面板,组件都要加载内容面板里 69 this.contentP= this.getContentPane(); 70 //布局 71 this.contentP.setLayout(null);//自然布局,即绝对布局,所以要加坐标 72 73 //添加组件 74 //常用组件:JLabel-文本 JTextField-文本框 JPasswordField-密码框 75 //JRadioButtom-单选按钮 JCheckBox-复选框 JComboBox-下拉列表 76 //JTextArea-文本域 JButton-按钮 77 78 //JLabel三个常用方法:显示文本,显示图片,显示边框(可以缩小到线条) 79 //显示背景的时候,可以设置不透明setOpaque为true 80 //☆如果几个都显示在同一位置,(级别相同)显示层次先添加的在顶层,可见,后添加的在底层,背景 81 //如果级别不同,有添加顺序才会显示,轻量级的放在重量级的上面,显示的是轻量级的 82 83 84 this.label = new JLabel("用户名"); 85 this.label.setBounds(100, 100, 100, 135); 86 this.label.setFont(new Font("宋体", Font.PLAIN, 24)); 87 88 this.contentP.add(this.label);//添加文本组件 89 90 91 this.checkBox = new JCheckBox("运动"); 92 this.checkBox.setBounds(350, 120,180, 135); 93 this.contentP.add(checkBox); 94 95 this.contentP.add(checkBox); 96 } 97 98 99 100 }
MyFrame
1 package com.Frame; 2 3 import java.awt.Color; 4 import java.awt.Container; 5 import java.awt.Font; 6 import java.awt.Toolkit; 7 8 import javax.swing.BorderFactory; 9 import javax.swing.ButtonGroup; 10 import javax.swing.JButton; 11 import javax.swing.JCheckBox; 12 import javax.swing.JComboBox; 13 import javax.swing.JFrame; 14 import javax.swing.JLabel; 15 import javax.swing.JPasswordField; 16 import javax.swing.JRadioButton; 17 import javax.swing.JTextArea; 18 import javax.swing.JTextField; 19 20 /** 21 * Date: 2016-3-11-下午1:06:22 Class_name: SimpleFrame.java Package_name: 22 * com.lovo.demo_16 Author: ZhangYue Description: 23 */ 24 public class SimpleFrame extends JFrame { 25 26 private int width; 27 private int height; 28 private Container contentP; 29 private JLabel usernameLabel; 30 private JLabel passwordLabel; 31 private JTextField usernameField; 32 private JPasswordField passwordField; 33 private JRadioButton maleBtn; 34 private JRadioButton femaleBtn; 35 private JCheckBox hobbyCheckBox1; 36 private JCheckBox hobbyCheckBox2; 37 private JCheckBox hobbyCheckBox3; 38 private JComboBox addressCombBox; 39 private JTextArea describeTextArea; 40 private JButton submitBtn; 41 private JButton cancelBtn; 42 43 public SimpleFrame() { 44 this.width = 1024; 45 this.height = 600; 46 this.setTitle("我的第一个GUI窗体"); 47 this.setSize(this.width, this.height); 48 Toolkit tool = Toolkit.getDefaultToolkit(); 49 double width = tool.getScreenSize().getWidth(); 50 double height = tool.getScreenSize().getHeight(); 51 this.setLocation((int) (width - this.width) / 2, 52 (int) (height - this.height) / 2); 53 54 this.setIconImage(tool.createImage("img/logo.png")); 55 56 // this.setLocationRelativeTo(null);//设置窗体居中 57 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 58 59 this.addContent(); 60 61 this.setVisible(true); 62 } 63 64 public void addContent() { 65 this.contentP = this.getContentPane(); 66 this.contentP.setLayout(null); 67 68 //文本 和 输入框 69 this.usernameLabel = new JLabel("用户名"); 70 this.usernameLabel.setBounds(350, 10, 90,35); 71 this.usernameLabel.setFont(new Font("宋体", Font.PLAIN, 16)); 72 this.contentP.add(this.usernameLabel);//添加文本组件 73 74 this.usernameField = new JTextField(); 75 this.usernameField.setBounds(450, 10, 200,35); 76 this.contentP.add(this.usernameField); 77 78 this.passwordLabel = new JLabel("密 码"); 79 this.passwordLabel.setBounds(350, 60, 90,35); 80 this.passwordLabel.setFont(new Font("宋体", Font.PLAIN, 16)); 81 this.contentP.add(this.passwordLabel);//添加文本组件 82 83 //密码框 84 this.passwordField = new JPasswordField(); 85 this.passwordField.setBounds(450, 60, 200,35); 86 this.passwordField.setEchoChar(‘*‘); 87 // this.passwordField.setForeground(Color.RED); 88 this.contentP.add(this.passwordField); 89 90 91 //单选框 92 JLabel genderTitle = new JLabel("性别"); 93 genderTitle.setBounds(350, 110, 80, 30); 94 genderTitle.setFont(new Font("宋体", Font.PLAIN, 16)); 95 this.contentP.add(genderTitle); 96 97 this.maleBtn = new JRadioButton("男"); 98 this.maleBtn.setBounds(440, 110, 50,35); 99 this.contentP.add(this.maleBtn); 100 this.femaleBtn = new JRadioButton("女"); 101 this.femaleBtn.setBounds(500, 110, 50,35); 102 this.contentP.add(this.femaleBtn); 103 ButtonGroup bp = new ButtonGroup(); 104 bp.add(this.maleBtn); 105 bp.add(this.femaleBtn); 106 107 //复选框 108 this.hobbyCheckBox1 = new JCheckBox("运动"); 109 this.hobbyCheckBox2 = new JCheckBox("电影"); 110 this.hobbyCheckBox3 = new JCheckBox("音乐"); 111 this.hobbyCheckBox1.setBounds(350, 160, 80,35); 112 this.hobbyCheckBox2.setBounds(440, 160, 80,35); 113 this.hobbyCheckBox3.setBounds(540, 160, 80,35); 114 115 this.contentP.add(this.hobbyCheckBox1); 116 this.contentP.add(this.hobbyCheckBox2); 117 this.contentP.add(this.hobbyCheckBox3); 118 119 //下拉框 120 this.addressCombBox = new JComboBox(new String[]{"成都", "德阳", "绵阳", "乐山", "资阳"}); 121 this.addressCombBox.addItem("达州"); 122 this.addressCombBox.addItem("自贡"); 123 this.addressCombBox.setSelectedIndex(5); 124 this.addressCombBox.setBounds(350, 210, 120, 25); 125 this.contentP.add(this.addressCombBox); 126 127 //文本域 128 this.describeTextArea = new JTextArea(); 129 this.describeTextArea.setBounds(350, 250, 300, 100); 130 // this.describeTextArea.setColumns(5); 131 // this.describeTextArea.setRows(3); 132 this.describeTextArea.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 133 this.contentP.add(this.describeTextArea); 134 135 //按钮 136 this.submitBtn = new JButton("确定"); 137 this.submitBtn.setBounds(400, 400, 80, 30); 138 this.contentP.add(this.submitBtn); 139 140 this.cancelBtn = new JButton("取消"); 141 this.cancelBtn.setBounds(500, 400, 80, 30); 142 this.contentP.add(this.cancelBtn); 143 144 } 145 146 public static void main(String[] args) { 147 new SimpleFrame(); 148 } 149 150 public int getWidth() { 151 return width; 152 } 153 154 public void setWidth(int width) { 155 this.width = width; 156 } 157 158 public int getHeight() { 159 return height; 160 } 161 162 public void setHeight(int height) { 163 this.height = height; 164 } 165 166 public Container getContentP() { 167 return contentP; 168 } 169 170 public void setContentP(Container contentP) { 171 this.contentP = contentP; 172 } 173 174 public JLabel getUsernameLabel() { 175 return usernameLabel; 176 } 177 178 public void setUsernameLabel(JLabel usernameLabel) { 179 this.usernameLabel = usernameLabel; 180 } 181 182 public JLabel getPasswordLabel() { 183 return passwordLabel; 184 } 185 186 public void setPasswordLabel(JLabel passwordLabel) { 187 this.passwordLabel = passwordLabel; 188 } 189 190 public JTextField getUsernameField() { 191 return usernameField; 192 } 193 194 public void setUsernameField(JTextField usernameField) { 195 this.usernameField = usernameField; 196 } 197 198 public JPasswordField getPasswordField() { 199 return passwordField; 200 } 201 202 public void setPasswordField(JPasswordField passwordField) { 203 this.passwordField = passwordField; 204 } 205 206 public JRadioButton getMaleBtn() { 207 return maleBtn; 208 } 209 210 public void setMaleBtn(JRadioButton maleBtn) { 211 this.maleBtn = maleBtn; 212 } 213 214 public JRadioButton getFemaleBtn() { 215 return femaleBtn; 216 } 217 218 public void setFemaleBtn(JRadioButton femaleBtn) { 219 this.femaleBtn = femaleBtn; 220 } 221 222 public JCheckBox getHobbyCheckBox1() { 223 return hobbyCheckBox1; 224 } 225 226 public void setHobbyCheckBox1(JCheckBox hobbyCheckBox1) { 227 this.hobbyCheckBox1 = hobbyCheckBox1; 228 } 229 230 public JCheckBox getHobbyCheckBox2() { 231 return hobbyCheckBox2; 232 } 233 234 public void setHobbyCheckBox2(JCheckBox hobbyCheckBox2) { 235 this.hobbyCheckBox2 = hobbyCheckBox2; 236 } 237 238 public JCheckBox getHobbyCheckBox3() { 239 return hobbyCheckBox3; 240 } 241 242 public void setHobbyCheckBox3(JCheckBox hobbyCheckBox3) { 243 this.hobbyCheckBox3 = hobbyCheckBox3; 244 } 245 246 public JComboBox getAddressCombBox() { 247 return addressCombBox; 248 } 249 250 public void setAddressCombBox(JComboBox addressCombBox) { 251 this.addressCombBox = addressCombBox; 252 } 253 254 public JTextArea getDescribeTextArea() { 255 return describeTextArea; 256 } 257 258 public void setDescribeTextArea(JTextArea describeTextArea) { 259 this.describeTextArea = describeTextArea; 260 } 261 262 public JButton getSubmitBtn() { 263 return submitBtn; 264 } 265 266 public void setSubmitBtn(JButton submitBtn) { 267 this.submitBtn = submitBtn; 268 } 269 270 public JButton getCancelBtn() { 271 return cancelBtn; 272 } 273 274 public void setCancelBtn(JButton cancelBtn) { 275 this.cancelBtn = cancelBtn; 276 } 277 278 279 280 }
SimpleFrame
1 package com.Frame; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Toolkit; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.awt.event.KeyEvent; 9 import java.awt.event.KeyListener; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.JButton; 13 import javax.swing.JFrame; 14 import javax.swing.JLabel; 15 import javax.swing.JOptionPane; 16 import javax.swing.JTextField; 17 18 19 20 /** 21 * @Author: YuanqiuChen 22 * @Date:2016-3-16-下午10:07:13 23 * 24 * 25 */ 26 27 public class GuessNumUI extends JFrame{ 28 private JLabel la1,la2,laResult; 29 private JButton yes, no, btnAgain; 30 private JTextField inputNum; 31 32 private int randomNum = (int)(Math.random()*50) + 50; 33 private int n = 0; 34 35 public GuessNumUI(){ 36 this.setSize(600,500); 37 this.setTitle("猜数字游戏"); 38 this.setLocationRelativeTo(null);//设置窗体居中 39 40 addContent(); 41 addEventHandler(); 42 43 this.setVisible(true); 44 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 45 } 46 47 private void addContent(){ 48 this.setLayout(null); 49 50 this.la1 = new JLabel(); 51 this.la1.setBounds(0, 0, 580,180); 52 this.la1.setBorder(BorderFactory.createTitledBorder("YourGuess")); 53 this.add(la1); 54 55 this.la2 = new JLabel(); 56 this.la2.setBounds(0, 180, 580,180); 57 this.la2.setBorder(BorderFactory.createTitledBorder("Hint")); 58 this.add(la2); 59 60 this.yes = new JButton("确定"); 61 this.yes.setBounds(200, 390, 60, 30); 62 this.add(yes); 63 64 this.no = new JButton("取消"); 65 this.no.setBounds(300, 390, 60, 30); 66 this.add(no); 67 68 this.btnAgain = new JButton("再来一次"); 69 this.btnAgain.setBounds(400, 390, 100, 30); 70 this.add(btnAgain); 71 72 this.inputNum = new JTextField(); 73 this.inputNum.setFont(new Font("宋体", Font.BOLD, 20)); 74 this.inputNum.setBounds(160, 30, 300, 50); 75 this.add(inputNum); 76 77 this.laResult = new JLabel("Let‘s Play!"); 78 this.laResult.setFont(new Font("宋体", Font.BOLD, 20)); 79 this.laResult.setBounds(200, 230, 300, 50); 80 this.add(laResult); 81 } 82 83 //添加事件 84 public void addEventHandler(){ 85 //添加监听器 86 this.yes.addActionListener( 87 new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 guess(); 90 inputNum.setText(""); 91 } 92 } 93 ); 94 95 this.no.addActionListener( 96 new ActionListener() { 97 public void actionPerformed(ActionEvent e) { 98 System.out.println(randomNum); 99 System.exit(0); 100 } 101 } 102 ); 103 104 this.btnAgain.addActionListener( 105 new ActionListener() { 106 public void actionPerformed(ActionEvent e) { 107 yes.setEnabled(true); 108 randomNum = (int)(Math.random()*50) + 50; 109 n = 0; 110 inputNum.setText(""); 111 laResult.setText("再来一次!Come On!"); 112 } 113 } 114 ); 115 116 //把按回车键监听器注册到事件源上 117 EnterKeyEvent enterKeyEvent = new EnterKeyEvent(); 118 this.addKeyListener(enterKeyEvent); 119 this.inputNum.addKeyListener(enterKeyEvent); 120 this.yes.addKeyListener(enterKeyEvent); 121 122 } 123 124 private class EnterKeyEvent implements KeyListener{ 125 public void keyTyped(KeyEvent e) {} 126 public void keyPressed(KeyEvent e) { 127 if(e.getKeyCode() == KeyEvent.VK_ENTER){ 128 if(yes.isEnabled()){ 129 guess(); 130 } 131 inputNum.setText(""); 132 } 133 } 134 public void keyReleased(KeyEvent e) {} 135 136 } 137 138 public void guess(){ 139 System.out.println(randomNum); 140 String inputStr = this.inputNum.getText().trim(); 141 int num; 142 if(!inputStr.matches("[0-9]+")){ 143 this.laResult.setText("请输入正整数"); 144 return; 145 }else{ 146 num = Integer.parseInt(inputStr); 147 } 148 149 if(num < 50 || num > 99){ 150 this.laResult.setText("请输入50-99的数"); 151 return; 152 } 153 154 if(num == randomNum){ 155 this.laResult.setText("猜对了,随机数是:" + randomNum); 156 n++; 157 this.yes.setEnabled(false); 158 159 // long now = System.currentTimeMillis(); 160 // while((System.currentTimeMillis() - now) < 3000){ 161 // 162 // } 163 164 try { 165 Thread.sleep(3000); 166 } catch (InterruptedException e) { 167 // TODO Auto-generated catch block 168 e.printStackTrace(); 169 } 170 171 // int choice = JOptionPane.showConfirmDialog(null, "确认退出"); 172 // if(choice == 0){} 173 // System.exit(0); 174 175 return; 176 }else{ 177 n++; 178 if(num < randomNum){ 179 this.laResult.setText("猜小了"); 180 }else{ 181 this.laResult.setText("猜大了"); 182 } 183 } 184 185 if(n >= 7){ 186 this.laResult.setText(n + "次机会已用完,随机数是:" + randomNum); 187 this.yes.setEnabled(false); 188 } 189 } 190 191 public static void main(String[] args) { 192 new GuessNumUI(); 193 } 194 195 }
GuessNumUI
1 package com.Frame; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.GridLayout; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.util.Random; 11 12 import javax.swing.BorderFactory; 13 import javax.swing.JButton; 14 import javax.swing.JFrame; 15 import javax.swing.JLabel; 16 import javax.swing.JPanel; 17 import javax.swing.JTextField; 18 import javax.swing.border.Border; 19 20 import com.Frame.test.Delay; 21 22 /** 23 * @Author: YuanqiuChen 24 * @Date:2016-3-17-上午9:22:57 25 * 26 *加入三个面板,直接设置边框 27 * 比自己写的好处,加入三个面板,面板设置边框 28 * 29 * 换颜色 30 */ 31 32 public class ChangeColor extends JFrame{ 33 34 private static final long serialVersionUID = 1L; 35 36 37 private JPanel mianBan1, mianBan2, mianBan3; 38 39 private JButton changeColor, ziDongChangeColor; 40 private JLabel laResult; 41 42 private boolean flag = false; 43 44 ChangeColorThread t = new ChangeColorThread(); 45 46 public JButton getZiDongChangeColor() { 47 return ziDongChangeColor; 48 } 49 50 public void setZiDongChangeColor(JButton ziDongChangeColor) { 51 this.ziDongChangeColor = ziDongChangeColor; 52 } 53 54 public boolean getFlag() { 55 return flag; 56 } 57 58 public void setFlag(boolean flag) { 59 this.flag = flag; 60 } 61 62 63 public ChangeColor(){ 64 this.setSize(600,500); 65 this.setTitle("换颜色"); 66 this.setLocationRelativeTo(null);//设置窗体居中 67 68 addContent(); 69 addEventHandler(); 70 71 72 73 this.setVisible(true); 74 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 75 } 76 77 private void addContent() { 78 this.mianBan1 = new JPanel(); 79 this.mianBan1.setLayout(null); 80 this.mianBan1.setBorder(BorderFactory.createTitledBorder("YourGuess")); 81 82 this.mianBan1.setBackground(new Color(200,220,240)); 83 84 85 86 this.mianBan2 = new JPanel(); 87 this.mianBan2.setBorder(BorderFactory.createTitledBorder("Hint")); 88 this.laResult = new JLabel("Let‘s Play!"); 89 this.laResult.setFont(new Font("宋体", Font.BOLD, 20)); 90 this.mianBan2.add(laResult); 91 92 93 94 this.mianBan3 = new JPanel(); 95 this.mianBan3.setBorder(BorderFactory.createTitledBorder("")); 96 97 98 this.changeColor = new JButton("换颜色"); 99 this.mianBan3.add(changeColor); 100 101 this.ziDongChangeColor = new JButton("自动换颜色"); 102 this.mianBan3.add(ziDongChangeColor); 103 104 this.setLayout(new GridLayout(3,1)); 105 this.add(mianBan1);this.add(mianBan2);this.add(mianBan3); 106 } 107 108 private void addEventHandler() { 109 this.changeColor.addActionListener(new ActionListener() { 110 @Override 111 public void actionPerformed(ActionEvent e) { 112 mianBan1.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 113 mianBan2.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 114 mianBan3.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 115 116 } 117 }); 118 119 120 this.ziDongChangeColor.addActionListener(new ActionListener() { 121 private Delay d = new Delay(); 122 public void actionPerformed(ActionEvent e) { 123 124 if(flag){ 125 flag = false; 126 // t.stop(); 127 }else{ 128 flag = true; 129 // t.start(); 130 new ChangeColorThread().start(); 131 132 } 133 System.out.println(flag); 134 } 135 }); 136 137 138 } 139 140 public void delay(int i){ 141 // long now = System.currentTimeMillis(); 142 // while(System.currentTimeMillis() - now < i){ 143 // } 144 // for(int j = 0; j < i*10; j++ ){ 145 // System.out.println(11111); 146 // } 147 } 148 149 public void changeColor(){ 150 mianBan1.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 151 mianBan2.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 152 mianBan3.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 153 } 154 155 public static void main(String[] args) { 156 new ChangeColor(); 157 } 158 159 public void paint(Graphics g) { 160 Random r = new Random(); 161 mianBan1.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 162 mianBan2.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 163 mianBan3.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); 164 165 } 166 167 //在Java的界面里边,不能出现太多的for循环,线程等待等,否则就会跟新显示不对,直接卡死,或者只显示最后一个图像 168 //必须放在线程里边才对 169 class ChangeColorThread extends Thread { 170 //问题,开始时为true,最后再false,可以运行一段时间结束 171 //开始为false,切换为真,不可以运行,因为顺序运行 172 public void run() { 173 while (flag) { 174 System.out.println("thread"+flag); 175 repaint(); 176 try { 177 Thread.sleep(1000); 178 } catch (InterruptedException e) { 179 e.printStackTrace(); 180 } 181 } 182 } 183 } 184 }
ChangeColor
1 package com.Frame; 2 3 import java.awt.BorderLayout; 4 import java.awt.GridLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.KeyEvent; 8 import java.awt.event.KeyListener; 9 10 import javax.print.attribute.standard.OutputDeviceAssigned; 11 import javax.swing.JButton; 12 import javax.swing.JFrame; 13 import javax.swing.JLabel; 14 import javax.swing.JOptionPane; 15 import javax.swing.JPanel; 16 import javax.swing.JPasswordField; 17 import javax.swing.JTextField; 18 19 /** 20 * @Author: YuanqiuChen 21 * @Date:2016-3-15-下午10:02:56 22 * 23 * 24 */ 25 26 public class Login extends JFrame{ 27 28 private JLabel laAccount, lapassword; 29 private JTextField textAccount; 30 private JPasswordField textPassword; 31 private JButton btnLogin; 32 private JPanel mianBan1, mianBan2, mianBan3; 33 34 public Login(){ 35 init(); 36 this.pack(); 37 // this.setSize(500,500); 38 39 this.setTitle("登录"); 40 this.setLocationRelativeTo(null); 41 this.setResizable(false); 42 this.setVisible(true); 43 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44 45 } 46 47 private void init(){ 48 this.laAccount = new JLabel("账号:"); 49 this.lapassword = new JLabel("密码:"); 50 this.textAccount = new JTextField("测试账号:123,密码123",20); 51 this.textPassword = new JPasswordField(20); 52 this.btnLogin = new JButton("登录"); 53 54 this.setLayout(new GridLayout(3,1)); 55 this.mianBan1 = new JPanel();mianBan2 = new JPanel();mianBan3 = new JPanel(); 56 57 this.mianBan1.add(laAccount);this.mianBan1.add(textAccount); 58 this.mianBan2.add(lapassword);this.mianBan2.add(textPassword); 59 this.mianBan3.add(btnLogin); 60 61 62 //面板可以添加到面板里边 63 // mianBan1.add(mianBan2); 64 65 this.add(mianBan1);this.add(mianBan2);this.add(mianBan3); 66 67 68 //添加事件 69 addEventHandler(); 70 } 71 72 //验证登录 73 private void check(){ 74 if(textAccount.getText().trim().equals("123") && textPassword.getText().trim().equals("123")){ 75 mianBan1.setVisible(false); 76 mianBan2.setVisible(false); 77 mianBan3.setVisible(false); 78 79 80 JOptionPane.showMessageDialog(mianBan1, "登录成功", "登录提示", JOptionPane.INFORMATION_MESSAGE); 81 // System.out.println("haha"); 82 }else{ 83 JOptionPane.showMessageDialog(null, "登录失败", "登录提示", JOptionPane.WARNING_MESSAGE); 84 // System.out.println("cuo"); 85 } 86 87 } 88 89 //添加事件处理 90 private void addEventHandler(){ 91 this.textPassword.addKeyListener(new KeyListener() { 92 public void keyTyped(KeyEvent e) {} 93 public void keyReleased(KeyEvent e) {} 94 95 public void keyPressed(KeyEvent e) { 96 if(e.getKeyCode() == KeyEvent.VK_ENTER){ 97 check(); 98 } 99 } 100 }); 101 //添加监听器 102 this.btnLogin.addActionListener( 103 new ActionListener() { 104 public void actionPerformed(ActionEvent e) { 105 // this.textAccount.getText();//这里不能加this表示当前对象,不对 106 check(); 107 } 108 } 109 ); 110 } 111 112 public static void main(String[] args) { 113 new Login(); 114 } 115 116 }
Login
布局:
1 package com.gui_layout; 2 3 import java.awt.BorderLayout; 4 import java.awt.Container; 5 import java.awt.FlowLayout; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JLabel; 10 11 /** 12 * Date: 2016-3-16-上午9:38:46 13 * Class_name: BorderLayoutTest.java 14 * Package_name: com.lovo.demo_16 15 * Author: ZhangYue 16 * Description: 17 */ 18 public class BorderLayoutTest extends JFrame{ 19 20 public BorderLayoutTest(){ 21 this.setSize(900,600); 22 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 this.setTitle("边界布局"); 24 this.setLocationRelativeTo(null);//设置窗体居中 25 26 this.addContent(); 27 28 this.setVisible(true); 29 } 30 31 32 public void addContent(){ 33 Container con = this.getContentPane(); 34 con.setLayout(new BorderLayout()); 35 36 JButton btn1 = new JButton("确定1"); 37 JButton btn2 = new JButton("确定2"); 38 JButton btn3 = new JButton("确定3"); 39 JButton btn4 = new JButton("确定4"); 40 JButton btn5 = new JButton("确定5"); 41 42 con.add(btn1, BorderLayout.EAST); 43 con.add(btn2, BorderLayout.WEST); 44 con.add(btn3, BorderLayout.NORTH); 45 con.add(btn4, BorderLayout.SOUTH); 46 con.add(btn5, BorderLayout.CENTER); 47 } 48 49 50 public static void main(String[] args) { 51 new BorderLayoutTest(); 52 } 53 }
BorderLayoutTest
1 package com.gui_layout; 2 3 import java.awt.Container; 4 import java.awt.FlowLayout; 5 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 10 /** 11 * Date: 2016-3-16-上午9:15:36 12 * Class_name: FlowLayoutTest.java 13 * Package_name: com.lovo.demo_16 14 * Author: ZhangYue 15 * Description: 16 */ 17 public class FlowLayoutTest extends JFrame{ 18 19 public FlowLayoutTest(){ 20 this.setSize(900,600); 21 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 this.setTitle("流布局"); 23 this.setLocationRelativeTo(null);//设置窗体居中 24 25 this.addContent(); 26 27 this.setVisible(true); 28 } 29 30 31 public void addContent(){ 32 Container con = this.getContentPane(); 33 34 con.setLayout(new FlowLayout()); 35 36 JLabel label = new JLabel("Test"); 37 JButton btn = new JButton("根据元素内容确定大小"); 38 JButton btn2 = new JButton("确定"); 39 JButton btn3 = new JButton("确定"); 40 JButton btn4 = new JButton("确定"); 41 JButton btn5 = new JButton("确定5"); 42 43 con.add(label); 44 con.add(btn); 45 con.add(btn2); 46 con.add(btn3); 47 con.add(btn4); 48 con.add(btn5); 49 } 50 51 52 public static void main(String[] args) { 53 new FlowLayoutTest(); 54 } 55 56 }
FlowLayoutTest
1 package com.gui_layout; 2 3 import java.awt.BorderLayout; 4 import java.awt.Container; 5 import java.awt.GridLayout; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 10 /** 11 * Date: 2016-3-16-上午10:25:29 12 * Class_name: GridLayoutTest.java 13 * Package_name: com.lovo.demo_16 14 * Author: ZhangYue 15 * Description: 16 */ 17 public class GridLayoutTest extends JFrame { 18 19 public GridLayoutTest(){ 20 this.setSize(900,600); 21 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 this.setTitle("网格布局"); 23 this.setLocationRelativeTo(null);//设置窗体居中 24 25 this.addContent(); 26 27 this.setVisible(true); 28 } 29 30 31 public void addContent(){ 32 Container con = this.getContentPane(); 33 con.setLayout(new GridLayout(2,3)); 34 35 JButton btn1 = new JButton("确定1"); 36 JButton btn2 = new JButton("确定2"); 37 JButton btn3 = new JButton("确定3"); 38 JButton btn4 = new JButton("确定4"); 39 JButton btn5 = new JButton("确定5"); 40 JButton btn6 = new JButton("确定6"); 41 JButton btn7 = new JButton("确定7"); 42 JButton btn8 = new JButton("确定8"); 43 JButton btn9 = new JButton("确定9"); 44 45 con.add(btn1); 46 con.add(btn2); 47 con.add(btn3); 48 con.add(btn4); 49 con.add(btn5); 50 con.add(btn6); 51 con.add(btn7); 52 con.add(btn8); 53 con.add(btn9); 54 } 55 56 57 public static void main(String[] args) { 58 new GridLayoutTest(); 59 } 60 61 }
GridLayoutTest
录入学生信息,写入文件,也可以显示学生信息:
1 package com.io.showStudentInfo; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.GridLayout; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.util.HashSet; 9 import java.util.Set; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.JButton; 13 import javax.swing.JComboBox; 14 import javax.swing.JFrame; 15 import javax.swing.JLabel; 16 import javax.swing.JPanel; 17 import javax.swing.JScrollBar; 18 import javax.swing.JScrollPane; 19 import javax.swing.JTextArea; 20 import javax.swing.JTextField; 21 22 import com.io.showStudentInfo.Student; 23 24 /** 25 //所有面板和首层容器都设置绝对布局,然后面板里边所有东西都设置位置和大小,面板也设置位置和大小,绝对不会错 26 * @Author: YuanqiuChen 27 * @Date:2016-3-18-下午8:13:57 28 * 29 *录入学生信息,写入文件,也可以显示学生信息 30 * 31 */ 32 33 public class ShowStudentInfo extends JFrame{ 34 35 private JButton saveInfo, showInfo; 36 private JLabel laName, laAge, laGender, laScore; 37 private JTextField textName, textAge, textScore; 38 private JTextArea showText; 39 private JComboBox boxGender; 40 41 private Set<Student> allStu = new HashSet<Student>(); 42 43 public ShowStudentInfo(){ 44 init(); 45 // this.pack(); 46 this.setSize(350, 600); 47 48 this.setTitle("学生信息录入系统"); 49 this.setLocationRelativeTo(null); 50 this.setResizable(false); 51 this.setVisible(true); 52 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 53 54 } 55 56 //初始化 57 private void init(){ 58 addContent(); 59 addEventHandler(); 60 } 61 62 private void addContent() { 63 JPanel mianBan1 = new JPanel(); 64 mianBan1.setLayout(null); 65 66 saveInfo = new JButton("录入信息"); 67 saveInfo.setBounds(60, 200, 100, 35); 68 saveInfo.setFont(new Font("宋体", Font.PLAIN, 16)); 69 70 showInfo = new JButton("显示信息"); 71 showInfo.setBounds(180, 200, 100, 35); 72 showInfo.setFont(new Font("宋体", Font.PLAIN, 16)); 73 74 laName = new JLabel("姓名:"); 75 laName.setBounds(40, 40, 80, 35); 76 laName.setFont(new Font("宋体", Font.PLAIN, 20)); 77 78 laAge = new JLabel("年龄:"); 79 laAge.setBounds(40, 80, 80, 35); 80 laAge.setFont(new Font("宋体", Font.PLAIN, 20)); 81 82 laGender = new JLabel("性别:"); 83 laGender.setBounds(40, 160, 80, 35); 84 laGender.setFont(new Font("宋体", Font.PLAIN, 20)); 85 86 laScore = new JLabel("成绩:"); 87 laScore.setBounds(40,120, 80, 35); 88 laScore.setFont(new Font("宋体", Font.PLAIN, 20)); 89 90 textName = new JTextField(); 91 textName.setBounds(120,40, 175, 35); 92 textName.setFont(new Font("宋体", Font.PLAIN, 20)); 93 94 textAge = new JTextField(); 95 textAge.setBounds(120,80, 175, 35); 96 textAge.setFont(new Font("宋体", Font.PLAIN, 20)); 97 98 textScore = new JTextField(); 99 textScore.setBounds(120,120, 175, 35); 100 textScore.setFont(new Font("宋体", Font.PLAIN, 20)); 101 102 boxGender = new JComboBox(new String[]{"男","女"}); 103 boxGender.setBounds(120,160, 75, 35); 104 boxGender.setFont(new Font("宋体", Font.PLAIN, 20)); 105 106 //文本域 107 showText = new JTextArea(); 108 // showText.setBounds(10, 250, 320, 300);// 109 showText.setFont(new Font("宋体", Font.PLAIN, 20)); 110 showText.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 111 112 //设置不可以直接编辑JTextArea里的内容,可以复制 113 showText.setEditable(false); 114 115 //设置自动换行 116 // showText.setLineWrap(true); 117 118 //实现滚动,后边两个是初始化视图显示水平和垂直滚动条 119 // JScrollPane mianBan2=new JScrollPane(showText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 120 JScrollPane mianBan2=new JScrollPane(showText); 121 mianBan2.setBounds(10, 250, 320, 300); 122 123 124 mianBan1.add(laName);mianBan1.add(textName); 125 mianBan1.add(laAge);mianBan1.add(textAge); 126 mianBan1.add(laGender);mianBan1.add(boxGender); 127 mianBan1.add(laScore);mianBan1.add(textScore); 128 mianBan1.add(saveInfo);mianBan1.add(showInfo); 129 130 // mianBan1.setBounds(0, 0, 340, 250); 131 132 // this.setLayout(null); 133 this.add(mianBan2); 134 this.add(mianBan1); 135 136 } 137 138 private void addEventHandler() { 139 this.saveInfo.addActionListener(new ActionListener() { 140 public void actionPerformed(ActionEvent e) { 141 showText.setText(""); 142 if(textName.getText().trim().equals("")){ 143 showText.setText("请输入姓名!"); 144 return; 145 } 146 if(!(textAge.getText().trim().matches("[1-9]{1}[0-9]?"))){ 147 showText.setText("请输入有效年龄!(1-99)"); 148 return; 149 } 150 if(!(textScore.getText().trim().matches("(0|100|[1-9]{1}[0-9]?)"))){ 151 showText.setText("请输入有效成绩!(0-100)"); 152 return; 153 } 154 String gender = (String)boxGender.getSelectedItem(); 155 156 allStu = StudentStream.readStudents("file/student.txt"); 157 158 allStu.add(new Student(textName.getText(), Integer.parseInt(textAge.getText()), gender, Integer.parseInt(textScore.getText()))); 159 StudentStream.saveStudents(allStu, "file/student.txt"); 160 showText.setText("存入成功"); 161 162 }}); 163 164 this.showInfo.addActionListener(new ActionListener() { 165 public void actionPerformed(ActionEvent e) { 166 allStu = StudentStream.readStudents("file/student.txt"); 167 showText.setText(""); 168 169 if(allStu.isEmpty()){ 170 showText.setText("还没有任何信息"); 171 return; 172 } 173 174 showText.setText("姓名\t年龄\t性别\t成绩\n"); 175 for(Student su : allStu){ 176 showText.append(su.toString() + "\n"); 177 } 178 }}); 179 180 } 181 182 public static void main(String[] args) { 183 new ShowStudentInfo(); 184 } 185 186 }
ShowStudentInfo
1 package com.io.showStudentInfo; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable{ 6 7 private String name; 8 private int age; 9 private String gender; 10 private int score; 11 12 13 public Student() { 14 super(); 15 } 16 17 18 public Student(String name, int age, String gender, int score) { 19 super(); 20 this.name = name; 21 this.age = age; 22 this.gender = gender; 23 this.score = score; 24 } 25 26 27 public int getScore() { 28 return score; 29 } 30 31 32 public void setScore(int score) { 33 this.score = score; 34 } 35 36 37 public String getName() { 38 return name; 39 } 40 public void setName(String name) { 41 this.name = name; 42 } 43 public int getAge() { 44 return age; 45 } 46 public void setAge(int age) { 47 this.age = age; 48 } 49 public String getGender() { 50 return gender; 51 } 52 public void setGender(String gender) { 53 this.gender = gender; 54 } 55 56 public String toString(){ 57 58 return this.getName() + "\t" + this.getAge() + "\t" + this.getGender() + "\t" + this.getScore(); 59 60 } 61 62 63 64 }
Student
1 package com.io.showStudentInfo; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.ObjectInputStream; 11 import java.io.ObjectOutputStream; 12 import java.io.OutputStream; 13 import java.util.HashSet; 14 import java.util.Set; 15 16 import com.io.showStudentInfo.Student; 17 18 /** 19 * Date: 2016-3-18-下午4:00:53 20 * Class_name: StudentStream.java 21 * Package_name: com.lovo.demo_17_2 22 * Author: ZhangYue 23 * Description: 24 */ 25 public class StudentStream { 26 27 public static void saveStudents(Set<Student> allStu, String filePath){ 28 try { 29 30 File file= new File(filePath); 31 if(!file.exists()){ 32 file.createNewFile(); 33 } 34 35 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); 36 37 out.writeObject(allStu); 38 39 out.close(); 40 } catch (FileNotFoundException e) { 41 e.printStackTrace(); 42 } catch (IOException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 } 47 48 @SuppressWarnings("unused") 49 public static Set<Student> readStudents(String filePath){ 50 51 try { 52 53 //三种方法,判断文件是否为空 54 if(new File(filePath).length() == 0){ 55 return new HashSet<Student>(); 56 } 57 // if(new FileInputStream(new File(filePath)).read() == -1){ 58 // return new HashSet<Student>(); 59 // } 60 // if(new FileInputStream(new File(filePath)).available() == 0){ 61 // return new HashSet<Student>(); 62 // } 63 64 65 ObjectInputStream inObj = new ObjectInputStream(new FileInputStream(filePath)); 66 67 Object obj = inObj.readObject(); 68 69 @SuppressWarnings("unchecked") 70 Set<Student> stu = (Set<Student>)obj; 71 72 inObj.close(); 73 return stu; 74 } catch (FileNotFoundException e) { 75 e.printStackTrace(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } catch (ClassNotFoundException e) { 79 e.printStackTrace(); 80 } 81 82 return null; 83 } 84 }
StudentStream
时间: 2024-10-19 15:21:40