Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Sunday, May 12, 2013

Java Simple AWT Swing On Notepad++




.

  1. copy and paste the following code into a Notepad++ window
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Container;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Draw4Ovals extends JPanel {
     
      Color color;
     
      public Draw4Ovals(Color color) {    
        this.color = color;
        this.setBackground(Color.BLACK);
        this.setOpaque(true);    //needed to ensure panel background is set
      }
     
      public void paintComponent(Graphics g) {
        super.paintComponent(g);    //needed to ensure that panel background is set
        int width = getWidth();
        int height = getHeight();
        g.setColor(color);
        g.fillOval(0, 0, width, height);
      }
     
      public static void main(String args[]) {
        JFrame frame = new JFrame("Oval Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        Container content = frame.getContentPane();
        content.setBackground(new Color(180,230,255));    
        content.setLayout(new GridLayout(2, 2, 5 ,5));
        Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
        for(Color c: colors) {
          Draw4Ovals panel = new Draw4Ovals(c);     
          content.add(panel);
        }
        //validate must be done on the component that has the .add() and the layoutManager
        content.validate();    
        frame.setSize(300, 200);
        frame.setLocation(50,100);
        frame.setVisible(true);    
      }
    }
  2. Save this file as “Draw4Ovals.java”.  Upper-case is important. Once it is saved, you’ll see that Notepad++ does syntax highlighting.
  3. Select the Macros menu, then click on the Java-compile macro near the bottom. The results should look like this:java-compile .
  4. Now select the macro “Java-run”. You should see this:
    java-run .
  5. If, in addition, you want to test an applet,
      1. paste the following code
      2. save it as “TestApplet.java”
      3. run “Java-compile”
      4. run “Java-applet-HTML”
      5. now either view the file in a browser, or else run “Java-applet-viewer”
        import java.awt.*; 
        import java.awt.event.*; 
        // import java.applet.Applet; 
        import javax.swing.*;
         
        public class TestApplet extends JApplet implements ActionListener { 
         Button b; 
         JLabel jl;
         public void init(){ 
          setLayout(new FlowLayout()); 
          b = new Button("push me"); 
          b.addActionListener(this); 
          add(b);
          jl = new JLabel("Welome to my applet");
          add(jl);
         } 
         
         public void actionPerformed(ActionEvent e){ 
          jl.setText("You clicked?");  
         }
        }
  6. Note: ♦Applets are no longer used as much as other types of web-programming.
    ♦The Java-applet-HTML script just makes a very simple HTML page. If you want to put an applet in a webpage, you should actually make a decent webpage.

-----

No comments:

Post a Comment