Swing的基本操作
作者:互联网
1 package GUI_experience; 2 import java.awt.*; 3 import java.awt.Container; 4 import java.awt.FlowLayout; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import javax.imageio.stream.ImageInputStream; 8 import javax.swing.*; 9 import java.awt.event.FocusEvent; 10 import java.awt.event.FocusListener; 11 import java.net.*; 12 /**class myFrame extends Frame{ 13 myFrame(String s){ 14 super(s); 15 this.setBackground(Color.blue); 16 this.setSize(300,200); 17 this.setVisible(true); 18 } 19 public static void main(String []args){ 20 myFrame mfr=new myFrame("First GUI project"); 21 } 22 }*/ 23 24 /**class MyFrame extends Frame{ 25 static int id=0; 26 MyFrame(int x,int y,int w,int h,Color myColor){ 27 super("MyFrame first project "+(++id)); 28 this.setBackground(myColor); 29 this.setBounds(x,y,w,h); 30 this.setLayout(null); 31 this.setVisible(true); 32 } 33 public static void main(String[]args){ 34 MyFrame mf=new MyFrame(200,300,200,200,Color.gray); 35 } 36 }*/ 37 38 /** 39 * 窗体示例JFrame 40 * 通常继承自JFrame类来使用而不是在类的main函数中创建一个JFrame对象 41 * 42 */ 43 class Demo2 extends JFrame{ 44 Demo2(){ 45 46 setVisible(true);//Visible 47 48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭按键,直接关闭 49 //setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//关闭按键等于没用 50 //setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//关闭但是不停止程序 51 //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//释放程序资源 52 setBounds(300,500,300,200);//Location and Size 53 //setSize(300,200); 54 //setLocation(300,500); 55 setTitle("This is a Frame."); 56 57 Container c = getContentPane();//获取窗体容器 58 c.setBackground(Color.white); 59 JLabel jl=new JLabel("Right here is a frame."); 60 c.add(jl); 61 // c.remove(jl); 62 c.validate();//验证容器中的组件,等于刷新一下 63 // f.setContentPane(c);//重新载入容器,与上式等效,但是上式更好 64 setResizable(false); 65 System.out.println(getX()); 66 System.out.println(getY()); 67 } 68 69 public static void main(String[] args) { 70 new Demo2(); 71 72 } 73 } 74 75 /** 76 * JDialog对话框示例 77 * 78 */ 79 class Demo3 extends JDialog{ 80 Demo3(JFrame frame){//在对话框的参数列表中就要包含父窗体 81 super(frame,"Title of DIALOG",true);//父窗体对象,对话框标题,是否阻塞父窗体 82 Container c=getContentPane(); 83 JLabel jl=new JLabel("这是一个标签"); 84 jl.setForeground(Color.RED);//设置字体颜色为红色 85 //jl.setText("");更改标签内容 86 //jl.getText();获取标签内容 87 jl.setFont(new Font("宋体",Font.BOLD,15));//设置字体 88 c.add(jl);//添加一段标签 89 90 JLabel pic=new JLabel("这是一个展示图片的标签"); 91 // URL url=Demo3.class.getResource("pycharm_inter.png");//获取图片URL路径,要求图片和此文件在同一包内 92 // Icon icon = new ImageIcon(url); 93 Icon icon = new ImageIcon("src/GUI_experience/pycharm_inter.png");//直接写出图片相应路径也能得到图片 94 // jl.setSize(20,20);//设置标签大小并不能改变图片大小 95 pic.setIcon(icon);//添加图片 96 c.add(pic); 97 setBounds(100,100,500,400); 98 99 } 100 101 public static void main(String[] args) { 102 JFrame f =new JFrame("Father Frame"); 103 f.setBounds(50,50,300,300); 104 Container c=f.getContentPane(); 105 JButton btn =new JButton("Push out a DIALOG"); 106 c.setLayout(new FlowLayout()); 107 c.add(btn); 108 f.setVisible(true); 109 f.setDefaultCloseOperation(EXIT_ON_CLOSE); 110 /** 111 * 重要部分:给按钮添加监听(使按钮生效) 112 * 113 */ 114 btn.addActionListener(new ActionListener() { 115 public void actionPerformed(ActionEvent actionEvent) { 116 Demo3 d =new Demo3(f); 117 d.setVisible(true);//对话框最好在main中设置窗体可见 118 } 119 }); 120 } 121 122 } 123 124 class Exp_absoluteLayout extends JFrame{ 125 Exp_absoluteLayout(){ 126 setBounds(300,300,200,150); 127 setDefaultCloseOperation(EXIT_ON_CLOSE); 128 Container c = getContentPane(); 129 130 c.setLayout(null);//布局参数设置为null即为绝对布局,此时组件大小和位置只由设定的坐标决定(与窗体大小无关) 131 JButton jb1 = new JButton("Button_1"), jb2 = new JButton("Button_2"); 132 jb1.setBounds(10,30,120 ,30); 133 jb2.setBounds(60,70,100,20); 134 c.add(jb1); 135 c.add(jb2); 136 137 setVisible(true); 138 139 140 } 141 public static void main(String[] args) { 142 new Exp_absoluteLayout(); 143 } 144 } 145 146 class Exp_FlowLayout extends JFrame{ 147 Exp_FlowLayout(){ 148 setVisible(true); 149 setDefaultCloseOperation(EXIT_ON_CLOSE); 150 setBounds(300,300,200,150); 151 152 Container c = getContentPane(); 153 for(int i=0;i<10;i++){ 154 c.add(new JButton("Button_"+i)); 155 } 156 // setLayout(new FlowLayout());//默认居中对齐 157 setLayout(new FlowLayout(FlowLayout.RIGHT,20,20));//设置为右对齐,水平间距20,竖直间距20 158 } 159 160 public static void main(String[] args) { 161 new Exp_FlowLayout(); 162 } 163 } 164 165 class Exp_BorderLayout extends JFrame{ 166 Exp_BorderLayout(){ 167 setBounds(100,100,350,200); 168 setDefaultCloseOperation(EXIT_ON_CLOSE); 169 170 171 Container c = getContentPane(); 172 c.setLayout(new BorderLayout()); 173 JButton jb1=new JButton("中"), 174 jb2=new JButton("东"), 175 jb3=new JButton("西"), 176 jb4=new JButton("南"), 177 jb5=new JButton("北"); 178 c.add(jb1,BorderLayout.CENTER); 179 c.add(jb2,BorderLayout.EAST); 180 c.add(jb3,BorderLayout.WEST); 181 c.add(jb4,BorderLayout.SOUTH); 182 c.add(jb5,BorderLayout.NORTH); 183 c.setVisible(true); 184 setVisible(true);//tip:只有将设置可见性放在最后才能保证所有组件都可见 185 } 186 187 public static void main(String[] args) { 188 new Exp_BorderLayout(); 189 } 190 } 191 192 class Exp_GridLayout extends JFrame{ 193 Exp_GridLayout(){ 194 setBounds(100,100,300,300); 195 setDefaultCloseOperation(EXIT_ON_CLOSE); 196 197 Container c = getContentPane(); 198 // c.setLayout(new GridLayout(7,3,5,5));//网格布局,7行3列,水平间距5,竖直间距5 199 c.setLayout(new GridLayout(6,4));//紧密无缝隙排列 200 for(int i=0;i<23;i++){//如果组件数量超过网格布局,每当不足时则多出一列 201 c.add(new JButton("Button_"+i)); 202 } 203 setVisible(true); 204 } 205 206 public static void main(String[] args) { 207 new Exp_GridLayout(); 208 } 209 } 210 211 class Exp_GridBagFLayout { 212 JFrame f = new JFrame(); 213 Container c= f.getContentPane(); 214 215 void createFrame(){ 216 f.setSize(800,600); 217 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 218 c.setLayout(new GridBagLayout()); 219 f.setLocationRelativeTo(null); 220 } 221 void init(){ 222 GridBagConstraints g1 = new GridBagConstraints(); 223 c.add(new JButton("Btn1"),g1); 224 225 GridBagConstraints g2 = new GridBagConstraints(); 226 g2.gridx=1; 227 g2.gridy=1; 228 c.add(new JButton("Btn2"),g2); 229 230 GridBagConstraints g3 = new GridBagConstraints(); 231 g3.gridx=2; 232 g3.gridy=2; 233 c.add(new JButton("Btn3"),g3); 234 } 235 void createButton(){ 236 for(int i=0;i<9;i++){ 237 GridBagConstraints g1 = new GridBagConstraints(); 238 g1.gridx=0; 239 g1.gridy=i;//标定位置(左上角) 240 c.add(new JButton("Btn"),g1); 241 242 GridBagConstraints g2 = new GridBagConstraints(); 243 g2.gridx=i; 244 g2.gridy=0; 245 c.add(new JButton("Btn"),g2); 246 } 247 } 248 249 void init2(){ 250 GridBagConstraints g1 = new GridBagConstraints(); 251 g1.gridx=1; 252 g1.gridy=1; 253 c.add(new JButton("Btn1"),g1); 254 255 GridBagConstraints g2 = new GridBagConstraints(); 256 g2.gridx=2; 257 g2.gridy=2; 258 g2.gridwidth=2; 259 g2.gridheight=1;//占据位置:两行一列 260 c.add(new JButton("Btn2"),g2); 261 262 GridBagConstraints g3 = new GridBagConstraints(); 263 g3.gridx=4; 264 g3.gridy=4; 265 g3.gridwidth=2; 266 g3.gridwidth=2; 267 c.add(new JButton("Btn3"),g3); 268 } 269 void init3(){ 270 GridBagConstraints g1 = new GridBagConstraints(); 271 g1.gridx=1; 272 g1.gridy=1; 273 g1.gridwidth=2; 274 g1.gridheight=2; 275 g1.fill=GridBagConstraints.NONE; 276 c.add(new JButton("NONE"),g1); 277 278 GridBagConstraints g2 = new GridBagConstraints(); 279 g2.gridx=3; 280 g2.gridy=1; 281 g2.gridheight=2; 282 g2.gridwidth=2; 283 g2.fill=GridBagConstraints.HORIZONTAL;//填充方式:水平填充 284 c.add(new JButton("HORIZONTAL"),g2); 285 286 GridBagConstraints g3 = new GridBagConstraints(); 287 g3.gridx=5; 288 g3.gridy=1; 289 g3.gridwidth=2; 290 g3.gridheight=2; 291 g3.fill=GridBagConstraints.VERTICAL;//填充方式:竖直填充 292 c.add(new JButton("VERTICAL"),g3); 293 294 GridBagConstraints g4 = new GridBagConstraints(); 295 g4.gridx=7; 296 g4.gridy=1; 297 g4.gridwidth=2; 298 g4.gridheight=2; 299 g4.fill=GridBagConstraints.BOTH;//填充方式:完全填充 300 c.add(new JButton("BOTH"),g4); 301 } 302 void init4(){ 303 GridBagConstraints g1 = new GridBagConstraints(); 304 g1.gridx=1; 305 g1.gridy=1; 306 g1.gridwidth=2; 307 g1.gridheight=2; 308 g1.fill=GridBagConstraints.NONE; 309 g1.anchor=GridBagConstraints.NORTHWEST;//占据方位:所占据区域的左上角(西北角) 310 c.add(new JButton("."),g1); 311 } 312 void init5(){ 313 GridBagConstraints g1 = new GridBagConstraints(); 314 g1.gridx=1; 315 g1.gridy=1; 316 g1.gridwidth=2; 317 g1.gridheight=2; 318 g1.insets = new Insets(0,0,10,10);//偏移像素值 319 c.add(new JButton("."),g1); 320 } 321 void init6(){ 322 GridBagConstraints g1 = new GridBagConstraints(); 323 g1.gridx=1; 324 g1.gridy=1; 325 g1.gridwidth=2; 326 g1.gridheight=2; 327 g1.ipadx=10; 328 g1.ipady=10;//首选位置 329 c.add(new JButton("NONE"),g1); 330 } 331 void init7(){ 332 GridBagConstraints g1 = new GridBagConstraints(); 333 g1.gridx=1; 334 g1.gridy=1; 335 g1.gridwidth=2; 336 g1.gridheight=2; 337 g1.weightx=10; 338 g1.weighty=10;//当窗体大小足够大时占据的空间(如果不够大则迁就窗体进行回缩 339 c.add(new JButton("NONE"),g1); 340 } 341 public static void main(String[] args) { 342 Exp_GridBagFLayout eg = new Exp_GridBagFLayout(); 343 eg.createFrame(); 344 eg.createButton(); 345 eg.init7(); 346 eg.f.setVisible(true); 347 } 348 } 349 350 class Demo4 extends JFrame{ 351 Demo4(){ 352 setBounds(100,100,190,120); 353 setDefaultCloseOperation(EXIT_ON_CLOSE); 354 Container c = getContentPane(); 355 c.setLayout(null); 356 357 JComboBox<String> cb = new JComboBox<String>();//建立下拉列表,可以使用泛型确定条目类型 358 cb.addItem("身份证"); 359 cb.addItem("学生证"); 360 cb.addItem("工作证"); 361 362 String [] items = {"1","2","3"};//利用字符串数组创建下拉列表 363 JComboBox<String> cb2 = new JComboBox<>(items); 364 365 JComboBox<String> cb3 = new JComboBox<>(); 366 String [] item = {"学生证","驾驶证","毕业证"}; 367 ComboBoxModel cm = new DefaultComboBoxModel<>(item);//建立下拉列表模型并向其中加入元素 368 cb3.setModel(cm);//利用下拉列表模型创建下拉列表 369 370 cb3.setEditable(true);//设定下拉框中值可编辑 371 cb3.setBounds(10,10,80,21); 372 c.add(cb3); 373 374 JButton jb = new JButton("打印"); 375 jb.addActionListener(new ActionListener() { 376 377 public void actionPerformed(ActionEvent actionEvent) { 378 System.out.println("索引:"+cb3.getSelectedIndex()); 379 System.out.println("值:"+cb3.getSelectedItem()); 380 } 381 }); 382 jb.setBounds(100,10,60,20); 383 c.add(jb); 384 385 386 setVisible(true); 387 } 388 389 public static void main(String[] args) { 390 Demo4 d4 = new Demo4(); 391 } 392 393 } 394 395 class Exp_Jlist extends JFrame{ 396 Exp_Jlist(){ 397 setBounds(100,100,217,167); 398 setDefaultCloseOperation(EXIT_ON_CLOSE); 399 400 Container c = getContentPane(); 401 c.setLayout(null); 402 403 404 String items [] ={"1","2","3","4","5","6","7","8"}; 405 // JList<String> jl = new JList<>(items); 406 407 DefaultListModel<String> mod = new DefaultListModel<>();//创建数据模型 408 for(String tmp:items){ 409 mod.addElement(tmp);//向数据模型中添加元素 410 } 411 mod.addElement("AnotherElem"); 412 JList<String> jl = new JList<>(); 413 jl.setModel(mod);//为创建的列表框提供模型,方便更改元素 414 415 // jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//选择模式:只能选择1个元素 416 // jl.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);//只能选择相邻元素 417 jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//选择不受限制(按ctrl) 418 419 JButton jb = new JButton("确认"); 420 jb.setBounds(120,90,70,25); 421 jb.addActionListener(new ActionListener() { 422 public void actionPerformed(ActionEvent actionEvent) { 423 java.util.List<String> str = jl.getSelectedValuesList();//获取列表框中元素 424 for(String st:str){ 425 System.out.println(st); 426 } 427 } 428 }); 429 c.add(jb); 430 431 JScrollPane js = new JScrollPane(jl);//添加滚动条 432 js.setBounds(10,10,100,100);//如果添加了滚动条,列表框的大小由滚动条决定 433 c.add(js); 434 setVisible(true); 435 } 436 437 public static void main(String[] args) { 438 new Exp_Jlist(); 439 } 440 } 441 442 class Exp_JTextField extends JFrame{ 443 Exp_JTextField(){ 444 setBounds(100,100,250,100); 445 setDefaultCloseOperation(EXIT_ON_CLOSE); 446 Container c =getContentPane(); 447 c.setLayout(new FlowLayout()); 448 449 JTextField jt = new JTextField("aaa",20);//创建文本框,同时可以创建默认文本和长度 450 jt.setColumns(20);//也可以通过此方法设置长度 451 jt.setText("aaa");//也可以通过此方法设置文本 452 jt.setFont(new Font("宋体",Font.PLAIN,20));//设置字体 453 c.add(jt); 454 455 JButton jb = new JButton("确认"); 456 jb.addActionListener(new ActionListener() { 457 @Override 458 public void actionPerformed(ActionEvent actionEvent) { 459 System.out.println(jt.getText());//获取文本框中的文本 460 jt.setText("");//点击完按钮之后清空文本 461 jt.requestFocus();//获取光标 462 } 463 }); 464 c.add(jb); 465 setVisible(true); 466 } 467 468 public static void main(String[] args) { 469 new Exp_JTextField(); 470 } 471 } 472 473 class Exp_JPasswordField extends JFrame{ 474 Exp_JPasswordField(){ 475 setBounds(100,100,500,100); 476 setDefaultCloseOperation(EXIT_ON_CLOSE); 477 Container c = getContentPane(); 478 c.setLayout(new FlowLayout()); 479 480 JPasswordField jp = new JPasswordField(20); 481 jp.setColumns(20); 482 jp.setFont(new Font("Arial",Font.BOLD,20)); 483 jp.setEchoChar('*');//设置显式字符,默认为圆点 484 jp.addActionListener(new ActionListener() { 485 public void actionPerformed(ActionEvent actionEvent) { 486 char ch[] = jp.getPassword();//获取密码的字符数组 487 String str = new String (ch); 488 System.out.println(str); 489 } 490 }); 491 c.add(jp); 492 493 setVisible(true); 494 } 495 496 public static void main(String[] args) { 497 new Exp_JPasswordField(); 498 } 499 } 500 501 class Exp_JTextArea extends JFrame{ 502 Exp_JTextArea(){ 503 setBounds(100,100,400,400); 504 setDefaultCloseOperation(EXIT_ON_CLOSE); 505 Container c = getContentPane(); 506 c.setLayout(new FlowLayout()); 507 JTextArea area = new JTextArea("这是一个文本域");//创建一个带默认文本的文本域,不同于文本框,文本域支持多行输入 508 area.setText("文本域"); 509 area.setRows(5);//设定行 510 area.setColumns(20);//设定列数 511 area.append("添加内容"); 512 area.insert("插入",3); 513 area.setFont(new Font("楷体",Font.PLAIN,20)); 514 515 JScrollPane js = new JScrollPane(area);//给文本域提供滚动条 516 c.add(js); 517 518 setVisible(true); 519 } 520 521 public static void main(String[] args) { 522 new Exp_JTextArea(); 523 } 524 } 525 526 class JustAExampleForActionListener extends JFrame{//动作事件监听器示例 527 JustAExampleForActionListener(){ 528 setBounds(100,100,500,500); 529 setDefaultCloseOperation(EXIT_ON_CLOSE); 530 Container c =getContentPane(); 531 532 JTextField jt = new JTextField(); 533 JLabel jl = new JLabel("这是一个标签"); 534 535 JScrollPane js =new JScrollPane(jt); 536 js.setBounds(10,10,300,30); 537 538 jt.addActionListener(new ActionListener() { 539 @Override 540 public void actionPerformed(ActionEvent actionEvent) { 541 Object obj =actionEvent.getSource();//使用ActionEvent对象获取值 542 JTextField jtTmp = (JTextField)obj; 543 System.out.println(jtTmp.getText()); 544 jl.setText("文本框单击了回车"); 545 } 546 }); 547 548 c.add(js); 549 c.add(jl); 550 setVisible(true); 551 } 552 553 public static void main(String[] args) { 554 new JustAExampleForActionListener(); 555 } 556 } 557 558 class JustAnExampleForFocusListener extends JFrame{//焦点事件监听器示例 559 JustAnExampleForFocusListener(){ 560 setBounds(100,100,500,400); 561 setDefaultCloseOperation(EXIT_ON_CLOSE); 562 Container c = getContentPane(); 563 c.setLayout(null); 564 565 JTextField jt1 = new JTextField(); 566 jt1.setBounds(10,10,60,20); 567 568 JTextField jt2 = new JTextField(); 569 jt2.setBounds(10,60,60,20); 570 571 JTextField jt3 = new JTextField(); 572 jt3.setBounds(10,110,60,20); 573 574 jt1.addFocusListener(new MyFocusListener() ); 575 jt2.addFocusListener(new MyFocusListener() ); 576 jt3.addFocusListener(new MyFocusListener() ); 577 c.add(jt1); 578 c.add(jt2); 579 c.add(jt3); 580 setVisible(true); 581 } 582 class MyFocusListener implements FocusListener{//自定义的焦点事件监听类 583 public void focusGained(FocusEvent e){//得到焦点事件 584 JTextField jtTmp = (JTextField)e.getSource();//获取触发事件的组件 585 jtTmp.setBorder(BorderFactory.createLineBorder(Color.GREEN));//给得到焦点的文本框提供绿色边框 586 } 587 public void focusLost(FocusEvent e){//失去焦点事件 588 JTextField jtTmp = (JTextField)e.getSource(); 589 jtTmp.setBorder(BorderFactory.createLineBorder(Color.red)); 590 } 591 } 592 593 public static void main(String[] args) { 594 new JustAnExampleForFocusListener(); 595 } 596 }
标签:setBounds,void,Swing,jl,new,基本操作,100,public 来源: https://www.cnblogs.com/hfut-freshguy/p/11621890.html