Java do visual interface using the AWT and SWING packages.
First, let's briefly look at these two packages:
AWT and Swing in Java are used to make visual interface. AWT (Abstract Window Toolkit): Abstract Window Toolkit, including early writing GUI applications. Swing: In order to solve existing problems AWT newly developed graphical interface package. Swing AWT is improved and extended.
The principle
AWT: is achieved through native method calls the operating system, so the Windows system is Windows AWT window style, while on Unix systems is the XWindow style. Has a one to one relationship between the AWT graphics functions and graphics functions provided by the operating system, we call it peers. In other words, when we use AWT components to the graphical user interface, we are actually in use graphics library operating system has to offer. Due to the different functions of the operating system graphics library provided it is not the same, present on a platform features might not exist on another platform. In order to achieve the "compile once, run anywhere" concept claimed by the Java language, AWT has to be achieved at the expense of its platform-independent function, that is, AWT graphics capabilities provided by a variety of general-purpose operating system provides intersection graphics capabilities. Since AWT rely on the local methods to achieve its function, we usually called heavyweight AWT controls controls.
Swing: Lightweight component is called, is not achieved through native methods, so Swing window style is more diverse. However, Swing and there was heaveyweight components. For example JWindow, Dialog, JFrame. Swing written in pure Java, good portability, look the same on different platforms. So lightweight components called Swing components (Swing is written in pure JAVA CODE, therefore JAVA SWING solve the problem because the window class can not be cross-platform, so that the window function is also cross-platform and scalability features, and SWING need not occupy too much system resources, so called lightweight components !!!)
the difference:
- AWT is C / C ++ method based on local program that runs faster; is based on the Swing AWT Java program that runs slower.
- AWT controls may behave differently on different platforms, and Swing performance is consistent across all platforms.
how to choose:
In practice, you should use depends on the type of platform AWT or Swing application deployment. E.g:
- For embedded applications, hardware resources of the target platform is often very limited, and the speed of the application is a critical factor in the project. In this contradictory situation, simple and efficient AWT certainly become the first choice of embedded Java.
- The key factors in common, standards-based Java application PC or workstation, the limitations of the hardware resources of the application is often not caused by the project. Therefore, the standard version of Java, is promoting the use of Swing, that is, to achieve the functionality of the application at the expense of speed.
After understanding these two packages we started on the main course, we write today to achieve simple calculator:
1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.util.Stack; 5 import javax.swing.JApplet; 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; 10 class Main extends JApplet implements ActionListener { 11 private= The textField the JTextField new new the JTextField ( "Enter", 12 is ); 12 is String operator = ""; // operator 13 is String INPUT = ""; // formula input 14 Boolean In Flag = to true ; 15 16 public void the init () // override init method Applet inside . 17 { 18 is textField.setFont ( new new the Font ( "Arial", Font.PLAIN, 50)); // set the font format . 19 textField.setEditable ( to false ); // set the text box is not change 20 Container C = getContentPane(); 21 JButton b[] = new JButton[17]; 22 JPanel panel = new JPanel(); 23 JPanel panel1 = new JPanel(); 24 panel1.add(textField); 25 panel.setLayout(new GridLayout(4, 4, 5, 5)); 26 panel1.setLayout(new FlowLayout(3)); 27 C.add(panel, BorderLayout.CENTER); 28 C.add(panel1, BorderLayout.NORTH); 29 String name[] = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", ".", "0", "=", "/", "C"};//设置按钮 30 for (int i = 0; i < 17; i++)//添加按钮 31 { 32 b[i] = new JButton(name[i]); 33 b[i].setBackground(new Color(192, 192, 192)); 34 b[i].setForeground(Color.BLACK); 37set the color of the keys// B [I] .setForeground (Color.RED);36) (I ==. 3. 4%IF35set the color of the keys// B [I] .setFont ( new new the Font ( "Arial", Font.PLAIN, 16)); // Set Font Format 38 is panel.add (B [I]); // add to the key interface 39 B [I] .addActionListener ( the this ); 40 } 41 is panel1.add (B [16 ]); 42 is B [16] .setPreferredSize ( new new the Dimension (65, 65 )); 43 is 44 is 45 B [13 is ] .setForeground (Color.RED) ; 46 is } 47 48 public void the actionPerformed (the ActionEvent E) { 49 int CNT = 0 ; 50 String the actionCommand = e.getActionCommand (); 51 is IF (actionCommand.equals ( "+") || actionCommand.equals ( "-") || actionCommand.equals ( "*") || the actionCommand. the equals ( "/" )) 52 is iNPUT = + "" + the actionCommand + ""; // set the input, the input pattern into a desired shape 53 is the else IF (actionCommand.equals ( "C" )) 54 is iNPUT = " " ; 55 the else IF (actionCommand.equals (" = ")) // when listening to the equal sign, the process INPUT 56 is { 57 is INPUT = + "=" +Compute (INPUT); 58 textField.setText (INPUT); 59 INPUT = "" ; 60 CNT =. 1 ; 61 is } the else 62 is INPUT + = the actionCommand; // number of multi-digit numbers in order to avoid unneeded spaces 63 is IF ( == 0 CNT ) 64 textField.setText (INPUT); 65 } 66 67 / * used to calculate the data stack * / 68 Private String Compute (String INPUT) // 69 { 70 String STR []; 71 is str = input.split(" "); 72 Stack<Double> s = new Stack<Double>(); 73 double m = Double.parseDouble(str[0]); 74 s.push(m); 75 for (int i = 1; i < str.length; i++) { 76 if (i % 2 == 1) { 77 if (str[i].compareTo("+") == 0) { 78 double help = Double.parseDouble(str[i + 1]); 79 s.push(help); 80 } 81 if (str[i].compareTo("-") == 0) { 82 double help = Double.parseDouble(str[i + 1]); 83 s.push(-help); 84 } 85 if (str[i].compareTo("*") == 0) { 86 double help = Double.parseDouble(str[i + 1]); 87 double ans = s.peek();//取出栈顶元素 88 s.pop();//消栈 89 ans *= help; 90 s.push(ans); 91 } 92 if (str[i].compareTo("/") == 0) { 93 double help = Double.parseDouble(str[i + 1]); 94 double ans = s.peek(); 95 s.pop(); 96 ans /= help; 97 s.push(ans); 98 } 99 } 100 } 101 double ans = 0d; 102 while (!s.isEmpty()) { 103 ans += s.peek(); 104 s.pop(); 105 } 106 String result = String.valueOf(ans); 107 return result; 108 } 109 110 public static void main(String args[]) { 111 JFrame frame = new JFrame("Counter");//创建顶级窗口 112 frame.setResizable(false); 113 Main applet = new Main(); 114 frame.getContentPane().add(applet, BorderLayout.CENTER); 115 applet.init(); // the init method of the applet 1 16 applet.start (); // thread starts 117 frame.setSize (400, 450); // set the window size 1 18 frame.setVisible ( to true ); // Set the visible window 119 } 120 }