• 郝斌_GUI


    85事件处理

    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    class A implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("hello");
    	}
    }
    
    public class Test {
    	public static void main(String[] args) {
    
    		Frame f = new Frame("haha");
    		Button bn = new Button("ok");
    		f.add(bn);
    		A aa = new A();
    
    		bn.addActionListener(aa);
    
    		f.pack();
    		f.setVisible(true);
    
    		// 适配器,设置窗体可以关闭
    		f.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    }
    

    87十个按钮的设计

    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Panel;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class Test {
    	public static void main(String[] args) {
    		Frame f = new Frame();
    		f.setSize(300, 300);
    		f.setLayout(new GridLayout(2, 1));// 2行1列
    
    		Panel p1 = new Panel();
    		p1.setLayout(new BorderLayout());
    		Panel p1_1 = new Panel();
    		p1_1.setLayout(new GridLayout(2, 1));// 2行1列
    		Button bn1 = new Button("BUTTON1");
    		Button bn2 = new Button("BUTTON2");
    		Button bn3 = new Button("BUTTON3");
    		Button bn4 = new Button("BUTTON4");
    		p1.add(bn1, BorderLayout.WEST);
    		p1_1.add(bn3);
    		p1_1.add(bn4);
    		p1.add(p1_1, BorderLayout.CENTER);
    		p1.add(bn2, BorderLayout.EAST);
    
    		Panel p2 = new Panel();
    		p2.setLayout(new BorderLayout());
    		Panel p2_1 = new Panel();
    		p2_1.setLayout(new GridLayout(2, 2));// 2行2列
    		Button bn5 = new Button("BUTTON5");
    		Button bn6 = new Button("BUTTON6");
    		Button bn7 = new Button("BUTTON7");
    		Button bn8 = new Button("BUTTON8");
    		Button bn9 = new Button("BUTTON9");
    		Button bn10 = new Button("BUTTON10");
    		p2.add(bn5, BorderLayout.WEST);
    		p2_1.add(bn7);
    		p2_1.add(bn8);
    		p2_1.add(bn9);
    		p2_1.add(bn10);
    		p2.add(p2_1, BorderLayout.CENTER);
    		p2.add(bn6, BorderLayout.EAST);
    
    		f.add(p1);
    		f.add(p2);
    		f.pack();
    		f.setVisible(true);
    
    		// 适配器,设置窗体可以关闭
    		f.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    }
    

    88三个文本框的相加运算示例

    89内部类 匿名类

    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    class TF {
    	public static TextField tf1, tf2, tf3;
    
    	public void launch() {
    		Frame f = new Frame();
    		tf1 = new TextField(30);
    		tf2 = new TextField(30);
    		tf3 = new TextField(30);
    		Button bn = new Button("=");
    		Label lb = new Label("+");
    
    		f.setLayout(new FlowLayout());
    		f.add(tf1);
    		f.add(lb);
    		f.add(tf2);
    		f.add(bn);
    		f.add(tf3);
    
    		bn.addActionListener(new MyMoniter());
    
    		f.pack();
    		f.setVisible(true);
    
    		// 适配器,设置窗体可以关闭
    		f.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    	}
    
    	class MyMoniter implements ActionListener {
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			int num1 = Integer.parseInt(tf1.getText());
    			int num2 = Integer.parseInt(tf2.getText());
    			int num3 = num1 + num2;
    
    			Integer it = new Integer(num3);
    			String str3 = it.toString();
    
    			tf3.setText(str3);
    		}
    	}
    }
    
    public class Test {
    	public static void main(String[] args) {
    		new TF().launch();
    	}
    }
    
  • 相关阅读:
    windows下wchar_t* 转char*
    VS2010的调试参数/Zi /DEBUG
    fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
    使用opencv传中文文件崩溃
    【20160924】GOCVHelper综述
    编译ITK
    几款开源图像处理软件评测研究
    新注册域名greenopen.site,向专业道路进军
    openmp在图像处理上面的运用
    实现multbandblend
  • 原文地址:https://www.cnblogs.com/denggelin/p/6363700.html
Copyright © 2020-2023  润新知