Java by API/javax.swing/SpringLayout

Материал из Java эксперт
Версия от 14:19, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

SpringLayout.Constraints: setX(Spring x)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
   
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500,500);
    SpringLayout layout = new SpringLayout();      
    Container content = aWindow.getContentPane();  
    content.setLayout(layout);                     
    JButton[] buttons = new JButton[6];            
    SpringLayout.Constraints constr = null;
    for(int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i+1));
      content.add(buttons[i]);                     
   }
    Spring xSpring = Spring.constant(5,15,25);    
    Spring ySpring = Spring.constant(10,30, 50);  
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring); 
    constr.setY(ySpring); 

    for(int i = 1 ; i< buttons.length ; i++) {
      constr = layout.getConstraints(buttons[i]);
      layout.putConstraint(SpringLayout.WEST, buttons[i],
                             xSpring,SpringLayout.EAST, buttons[i-1]);    
      layout.putConstraint(SpringLayout.NORTH, buttons[i],
                             ySpring,SpringLayout.SOUTH, buttons[i-1]);
    }
    aWindow.setVisible(true);                      // Display the window
  }
}





SpringLayout.Constraints: setY(Spring y)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
   
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500,500);
    SpringLayout layout = new SpringLayout();      
    Container content = aWindow.getContentPane();  
    content.setLayout(layout);                     
    JButton[] buttons = new JButton[6];            
    SpringLayout.Constraints constr = null;
    for(int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i+1));
      content.add(buttons[i]);                     
   }
    Spring xSpring = Spring.constant(5,15,25);    
    Spring ySpring = Spring.constant(10,30, 50);  
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring); 
    constr.setY(ySpring); 

    for(int i = 1 ; i< buttons.length ; i++) {
      constr = layout.getConstraints(buttons[i]);
      layout.putConstraint(SpringLayout.WEST, buttons[i],
                             xSpring,SpringLayout.EAST, buttons[i-1]);    
      layout.putConstraint(SpringLayout.NORTH, buttons[i],
                             ySpring,SpringLayout.SOUTH, buttons[i-1]);
    }
    aWindow.setVisible(true);                      // Display the window
  }
}





SpringLayout.EAST

import java.awt.ruponent;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    Component left = new JLabel("Name");
    Component right = new JTextField(15);
    contentPane.add(left);
    contentPane.add(right);
    layout.putConstraint(SpringLayout.WEST,  left, 10,
      SpringLayout.WEST,  contentPane);
    layout.putConstraint(SpringLayout.NORTH, left, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.NORTH, right, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.WEST, right, 20,
      SpringLayout.EAST, left);
    frame.setSize(300, 100);
    frame.setVisible(true);  }
}





SpringLayout: getConstraints(Component c)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
   
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500,500);
    SpringLayout layout = new SpringLayout();      
    Container content = aWindow.getContentPane();  
    content.setLayout(layout);                     
    JButton[] buttons = new JButton[6];            
    SpringLayout.Constraints constr = null;
    for(int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i+1));
      content.add(buttons[i]);                     
   }
    Spring xSpring = Spring.constant(5,15,25);    
    Spring ySpring = Spring.constant(10,30, 50);  
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring); 
    constr.setY(ySpring); 

    for(int i = 1 ; i< buttons.length ; i++) {
      constr = layout.getConstraints(buttons[i]);
      layout.putConstraint(SpringLayout.WEST, buttons[i],
                             xSpring,SpringLayout.EAST, buttons[i-1]);    
      layout.putConstraint(SpringLayout.NORTH, buttons[i],
                             ySpring,SpringLayout.SOUTH, buttons[i-1]);
    }
    aWindow.setVisible(true);                      // Display the window
  }
}





SpringLayout.NORTH

import java.awt.ruponent;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    Component left = new JLabel("Name");
    Component right = new JTextField(15);
    contentPane.add(left);
    contentPane.add(right);
    layout.putConstraint(SpringLayout.WEST,  left, 10,
      SpringLayout.WEST,  contentPane);
    layout.putConstraint(SpringLayout.NORTH, left, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.NORTH, right, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.WEST, right, 20,
      SpringLayout.EAST, left);
    frame.setSize(300, 100);
    frame.setVisible(true);  }
}





SpringLayout: putConstraint(String e1, Component c1, Spring s, String e2, Component c2)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
   
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setSize(500,500);
    SpringLayout layout = new SpringLayout();      
    Container content = aWindow.getContentPane();  
    content.setLayout(layout);                     
    JButton[] buttons = new JButton[6];            
    SpringLayout.Constraints constr = null;
    for(int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i+1));
      content.add(buttons[i]);                     
   }
    Spring xSpring = Spring.constant(5,15,25);    
    Spring ySpring = Spring.constant(10,30, 50);  
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring); 
    constr.setY(ySpring); 

    for(int i = 1 ; i< buttons.length ; i++) {
      constr = layout.getConstraints(buttons[i]);
      layout.putConstraint(SpringLayout.WEST, buttons[i],
                             xSpring,SpringLayout.EAST, buttons[i-1]);    
      layout.putConstraint(SpringLayout.NORTH, buttons[i],
                             ySpring,SpringLayout.SOUTH, buttons[i-1]);
    }
    aWindow.setVisible(true);                      // Display the window
  }
}





SpringLayout.WEST

import java.awt.ruponent;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);
    Component left = new JLabel("Name");
    Component right = new JTextField(15);
    contentPane.add(left);
    contentPane.add(right);
    layout.putConstraint(SpringLayout.WEST,  left, 10,
      SpringLayout.WEST,  contentPane);
    layout.putConstraint(SpringLayout.NORTH, left, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.NORTH, right, 25,
      SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.WEST, right, 20,
      SpringLayout.EAST, left);
    frame.setSize(300, 100);
    frame.setVisible(true);  }
}