*01-GUI(概述)
图形用户界面编程(GUI,)
很少。一般开发者都会用C++或者Delphi。
---GUI
---CLI命令行用户接口
JavaWeb方向用不着这块。
但有一项机制很重要。
awt,swing这两个包
组件!

窗体能独立存在。
面板是窗体里的一个东西。面板不能独立存在。

坐标式布局。
组件放在面板里面。
Java是要手写的。。。
*02-GUI(Frame演示)
开发流程:查看父类,编写子类,

*03-GUI(事件监听机制)




*04-GUI(ActionListener演示)
package cn.itcast.gui.p1.awt; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class FrameDemo { / * @param args */ public static void main(String[] args) { Frame f = new Frame("my frame"); // f.setSize(500, 400); // f.setLocation(400, 200); f.setBounds(400, 200, 500, 400); f.setLayout(new FlowLayout());//设置流式布局 Button but = new Button("一个按钮"); f.add(but);//将按钮添加到窗体中。 f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // System.out.println("closing......."+e); System.exit(0); } }); //在按钮上加上一个监听。 but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println("button run ....."); System.exit(0); } }); f.setVisible(true); System.out.println("over"); } }
讯享网
*05-GUI(鼠标事件)&*06-GUI(键盘事件)
讯享网package cn.itcast.gui.p1.awt; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MouseAndKeyDemo { private Frame f; private TextField tf; private Button but; public MouseAndKeyDemo() { init(); } private void init() { f = new Frame("演示鼠标和键盘监听"); f.setBounds(400,200,500,400); f.setLayout(new FlowLayout()); tf = new TextField(35); but = new Button("一个按钮"); f.add(tf); f.add(but); myEvent(); f.setVisible(true); } private void myEvent() { //给文本框添加键盘监听。 tf.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { // System.out.println("key run..."+KeyEvent.getKeyText(e.getKeyCode())+"::::"+e.getKeyCode()); // int code = e.getKeyCode(); // if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){ // System.out.println("必须是数字"); // e.consume(); // } if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){ System.out.println("enter run ..."); } } }); f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // System.out.println("action run....."); } }); //在按钮上添加一个鼠标监听. but.addMouseListener(new MouseAdapter() { private int count = 1; @Override public void mouseEntered(MouseEvent e) { // System.out.println("mouse enter..."+count++); // tf.setText("mouse enter..."+count++); } @Override public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2) tf.setText("mouse double click..."+count++); // System.out.println("mouse click..."+count++); // System.out.println(e); } }); } / * @param args */ public static void main(String[] args) { new MouseAndKeyDemo(); } }
*07-GUI(Swing演示&装插件)




package cn.itcast.swing.demo; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.WindowConstants; import javax.swing.SwingUtilities; / * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class MySwing extends javax.swing.JFrame { private JButton jButton1; / * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MySwing inst = new MySwing(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public MySwing() { super(); initGUI(); } private void initGUI() { try { getContentPane().setLayout(null); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jButton1 = new JButton(); getContentPane().add(jButton1); jButton1.setText("\u9000\u51fa"); jButton1.setBounds(142, 26, 103, 48); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } pack(); setSize(400, 300); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } private void jButton1ActionPerformed(ActionEvent evt) { // System.out.println("jButton1.actionPerformed, event="+evt); //TODO add your code for jButton1.actionPerformed System.exit(0); } }
*08-GUI(练习-列出目录内容)

讯享网package cn.itcast.swing.demo; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.File; import javax.swing.JButton; import javax.swing.JEditorPane; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; / * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class MyWindow extends javax.swing.JFrame { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private JTextField jTextField1; private JButton jButton1; private JTextArea jTextArea1; private JScrollPane jScrollPane1; / * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MyWindow inst = new MyWindow(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public MyWindow() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); { jTextField1 = new JTextField(); getContentPane().add(jTextField1, "Center"); jTextField1.setBounds(12, 41, 368, 34); jTextField1.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { jTextField1KeyPressed(evt); } }); } { jButton1 = new JButton(); getContentPane().add(jButton1); jButton1.setText("\u8f6c\u5230"); jButton1.setBounds(397, 41, 99, 34); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); } { jScrollPane1 = new JScrollPane(); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(12, 87, 484, 356); { jTextArea1 = new JTextArea(); jScrollPane1.setViewportView(jTextArea1); } } pack(); this.setSize(523, 482); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } private void jButton1ActionPerformed(ActionEvent evt) { showDir(); } / * */ public void showDir() { /* * 通过点击按钮获取文本框中目录, * 将目录中的内容显示在文本区域中。 */ String dir_str = jTextField1.getText(); File dir = new File(dir_str); if(dir.exists() && dir.isDirectory()){ jTextArea1.setText(""); String[] names = dir.list(); for(String name : names){ jTextArea1.append(name+LINE_SEPARATOR); } } } private void jTextField1KeyPressed(KeyEvent evt) { if(evt.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }
*09-GUI(菜单)

*10-GUI(练习)

package cn.itcast.swing.demo; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; / * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class MyMenu extends javax.swing.JFrame { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); private JMenuBar jMenuBar1; private JScrollPane jScrollPane1; private JMenuItem jMenuItem2; private JTextArea jTextArea1; private JMenuItem jMenuItem1; private JMenu jMenu1; private JFileChooser chooser; private JDialog dialog; / * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MyMenu inst = new MyMenu(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public MyMenu() { super(); initGUI(); } private void initGUI() { try { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); { jScrollPane1 = new JScrollPane(); getContentPane().add(jScrollPane1, BorderLayout.CENTER); { jTextArea1 = new JTextArea(); jScrollPane1.setViewportView(jTextArea1); } } { jMenuBar1 = new JMenuBar(); setJMenuBar(jMenuBar1); { jMenu1 = new JMenu(); jMenuBar1.add(jMenu1); jMenu1.setText("\u6587\u4ef6"); { jMenuItem1 = new JMenuItem(); jMenu1.add(jMenuItem1); jMenuItem1.setText("\u6253\u5f00"); jMenuItem1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { jMenuItem1ActionPerformed(evt); } catch (IOException e) { e.printStackTrace(); } } }); } { jMenuItem2 = new JMenuItem(); jMenu1.add(jMenuItem2); jMenuItem2.setText("\u4fdd\u5b58"); jMenuItem2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { try { jMenuItem2ActionPerformed(evt); } catch (IOException e) { e.printStackTrace(); } } }); } } } pack(); this.setSize(610, 402); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } } private void jMenuItem1ActionPerformed(ActionEvent evt) throws IOException { chooser = new JFileChooser(); // FileNameExtensionFilter filter = new FileNameExtensionFilter( // "JPG & GIF Images", "jpg", "gif"); // chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION){ System.out.println("没有选取文件,取消了"); return; } File file = chooser.getSelectedFile(); BufferedReader bufr = new BufferedReader(new FileReader(file)); String line = null; while((line=bufr.readLine())!=null){ jTextArea1.append(line+LINE_SEPARATOR); } bufr.close(); } private void jMenuItem2ActionPerformed(ActionEvent evt) throws IOException { chooser = new JFileChooser(); int returnVal = chooser.showSaveDialog(this); if(returnVal == JFileChooser.CANCEL_OPTION){ System.out.println("没有指定文件,取消了"); return; } File file = chooser.getSelectedFile(); String text = jTextArea1.getText(); BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); bufw.write(text); bufw.close(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/110452.html