package pages.imgs; import javax.swing.*;import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent; public class page373_4 extends JFrame { private JLabel la = new JLabel("changes backgroundcolor by enter"); public page373_4(){ super("Keylistener's character key input example"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(la); // adding a label to a default pan c.addKeyListener(new MyKeyListener()); setSize(250,150); setVisible(true); c.setFocusable(true); // allowing content pan to have a focus,,,??? c.requestFocus();//setting a focus to a pan } class MyKeyListener extends KeyAdapter{ public void keyPressed(KeyEvent e){ int r = (int) (Math.random() * 256); int g = (int) (Math.random() * 256); int b = (int) (Math.random() * 256); switch (e.getKeyChar()){ case '\n' : la.setText("r = " + r + ", g = " + g + ", b = " + b); getContentPane().setBackground(new Color(r,g,b)); break; case 'q' : System.exit(0); } } } public static void main (String []agrs){ new page373_4(); } }/*oh yeah Saturday night~~~~~~~!!!!!!!!!!*/
<HTML> <body> </body> <script> function Calculator() { let methods = { "-" : (a, b) => a - b, "+" : (a, b) => a + b }; //methods is an object which keeps key for operators //and value to return the actual operation values //each returns the result of operation that key(operator) does this.calculate = function (str){ //calculate is one element in the function Calculator //it takes the string and returns the value //in the function element list is delimeted by , not ; let split = str.split(" "), a = +split[0], op = split[1], b = split [2] if(!methods[op] || isNaN(a) || isNaN(b)) { return NaN; // error handling } return methods[op](a,b); } this.addMethod = function(name, func){ methods[name] = func; //this is how to add new key and ele to object } } let powerCalc = new Calculator; powerCalc.addMethod("*&
댓글
댓글 쓰기