Guiexample.java

Allikas: Lambda
// A simple window containing a working button, plus some text fields
// doing some drawing when you push the button

// NB! We have several layers in visual app here, each containing the lower ones:
//  - frame (JFrame, ie a window)
//    - toppanel (ordinary JPanel) with BorderLayout
//      - buttonpanel (ordinary Jpanel), at NORTH
//      - drawpanel (our DrawPanel, which is a variation of JPanel class), at CENTER

import javax.swing.*;        
import java.awt.*;
import java.awt.event.*;

public class guiexample {      
  
    // creates a small window with a text and a button in it
    public static void main(String[] args) {
      
        // create new frame 
      
        JFrame frame = new guiexampleFrame();  
            
        // Set a window look-and-feel
        try {
		       UIManager.setLookAndFeel
                // ("javax.swing.plaf.metal.MetalLookAndFeel"); 
                // ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                 ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");         
		       SwingUtilities.updateComponentTreeUI(frame);
		       frame.pack();
        }  catch (Exception exc) {		
           System.err.println("Could not load given LookAndFeel!");
        }        
       
        // react to window close, pack stuff, show out
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setLocation(200,200);
        //frame.pack();  // give a suitable size to window automatically
        frame.setVisible(true);  // make window visible
        //panel.updateState(); // refresh panel
    }

}

class guiexampleFrame extends JFrame {
  
    guiexampleFrame frame;    
    JPanel toppanel;
    JPanel buttonpanel;
    examplecanvas drawpanel;
    JPanel buttonpanel2;
    JTextField textfield1, textfield2;
  
    guiexampleFrame() {
      
        // create a new window and panels
              
        toppanel = new JPanel();
        buttonpanel = new JPanel();
        buttonpanel2 = new JPanel();
        
         // Create and add two text fields to button panel
	      //JTextField 
        textfield1 = new JTextField(10);
        //JTextField 
        textfield2 = new JTextField(10);
        
        drawpanel = new examplecanvas
          (textfield1,textfield2);
      
        // set layouts and colors of panels
      
        buttonpanel.setLayout(new FlowLayout());
        buttonpanel.setBackground(Color.blue);    
        toppanel.setLayout(new BorderLayout());
        toppanel.setBackground(Color.green);  
        
        buttonpanel2.setLayout(new GridLayout(2,1));
        
        // create and add components
      
        
        // create a label widget with text, put on button panel
        JLabel label = new JLabel("Hi!");         
        buttonpanel.add(label);  
                        
        buttonpanel.add(textfield1); 
        buttonpanel.add(textfield2); 
        
        // Create and add a button to button panel
        JButton button = new JButton("Press me, please!");
        buttonpanel.add(button); 
        
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawpanel.repaint();                
            }
        });
              
        JButton button2 = new JButton("Ohoo");
        JButton button3 = new JButton("Ehee");
        buttonpanel2.add(button2);
        buttonpanel2.add(button3);
           
        // add button and drawpanels to toppanel
        toppanel.add(BorderLayout.NORTH,buttonpanel);
        toppanel.add(BorderLayout.WEST,buttonpanel2);
        toppanel.add(BorderLayout.CENTER,drawpanel);
      
        // add toppanel to the window	
        this.getContentPane().add(toppanel);		                    
                                                               
    }
}


class examplecanvas extends Canvas {
  Color textColor;  // Color in which "Hello      
                    // World" is displayed.
  Font textFont;   // The font in which the 
                    // message is displayed.
   JTextField textfield1,textfield2;
    
   examplecanvas(JTextField t1,
       JTextField t2) {
     // Constructor.
      textfield1=t1;
      textfield2=t2;        
      setBackground(Color.green);
      //textColor = Color.red;
      //textFont = new  
      //           Font("Serif",Font.BOLD,24);
   }
   
   public void paint(Graphics g) {
            
      g.drawString("haa",100,200);
      
      Font timesFont = new Font("Serif", Font.PLAIN, 16);
      g.setFont(timesFont);
      g.setColor( Color.blue);
      g.drawString(textfield1.getText(), 50,250);
      g.drawString(textfield2.getText(), 100,300);
      //g.drawString("suva", 50,250);
      
      g.drawLine(0,0,300,300);
      
      g.drawRect(40,60,90,110);
      
      for (int i=0; i<100; i++) {
        g.drawLine(20,30,70,70+i);
      }                
   }      
   
   void setTextColor(Color color) {
     // Set the text color and tell the system    
     // to repaint the canvas.
      textColor = color;
      repaint();
   }
}  // end class