Java by API/java.awt/SpringLayout.Constraints

Материал из Java эксперт
Перейти к: навигация, поиск

SpringLayout.Constraints: setHeight(Spring height)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new SpringLayoutPanel());
    aWindow.setVisible(true);
  }
}
class SpringLayoutPanel extends JPanel {
  public SpringLayoutPanel() {
    SpringLayout layout = new SpringLayout();
    setLayout(layout);
    JButton[] buttons = new JButton[6];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i + 1));
      add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    Spring wSpring = Spring.constant(30, 80, 130);
    SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
    buttonConstr.setX(xSpring);
    buttonConstr.setY(ySpring);
    for (int i = 0; i < buttons.length; i++) {
      buttonConstr = layout.getConstraints(buttons[i]);
      buttonConstr.setHeight(ySpring);
      buttonConstr.setWidth(wSpring);
      if (i > 0) {
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring,
            SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring,
            SpringLayout.SOUTH, buttons[i - 1]);
      }
    }
    SpringLayout.Constraints constr = layout.getConstraints(this);
    constr.setConstraint(SpringLayout.EAST, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.EAST), Spring.constant(15)));
    constr.setConstraint(SpringLayout.SOUTH, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.SOUTH), Spring.constant(10)));
  }
}





SpringLayout.Constraints: setWidth(Spring width)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new SpringLayoutPanel());
    aWindow.setVisible(true);
  }
}
class SpringLayoutPanel extends JPanel {
  public SpringLayoutPanel() {
    SpringLayout layout = new SpringLayout();
    setLayout(layout);
    JButton[] buttons = new JButton[6];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i + 1));
      add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    Spring wSpring = Spring.constant(30, 80, 130);
    SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
    buttonConstr.setX(xSpring);
    buttonConstr.setY(ySpring);
    for (int i = 0; i < buttons.length; i++) {
      buttonConstr = layout.getConstraints(buttons[i]);
      buttonConstr.setHeight(ySpring);
      buttonConstr.setWidth(wSpring);
      if (i > 0) {
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring,
            SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring,
            SpringLayout.SOUTH, buttons[i - 1]);
      }
    }
    SpringLayout.Constraints constr = layout.getConstraints(this);
    constr.setConstraint(SpringLayout.EAST, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.EAST), Spring.constant(15)));
    constr.setConstraint(SpringLayout.SOUTH, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.SOUTH), Spring.constant(10)));
  }
}





SpringLayout.Constraints: setX(Spring x)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new SpringLayoutPanel());
    aWindow.setVisible(true);
  }
}
class SpringLayoutPanel extends JPanel {
  public SpringLayoutPanel() {
    SpringLayout layout = new SpringLayout();
    setLayout(layout);
    JButton[] buttons = new JButton[6];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i + 1));
      add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    Spring wSpring = Spring.constant(30, 80, 130);
    SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
    buttonConstr.setX(xSpring);
    buttonConstr.setY(ySpring);
    for (int i = 0; i < buttons.length; i++) {
      buttonConstr = layout.getConstraints(buttons[i]);
      buttonConstr.setHeight(ySpring);
      buttonConstr.setWidth(wSpring);
      if (i > 0) {
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring,
            SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring,
            SpringLayout.SOUTH, buttons[i - 1]);
      }
    }
    SpringLayout.Constraints constr = layout.getConstraints(this);
    constr.setConstraint(SpringLayout.EAST, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.EAST), Spring.constant(15)));
    constr.setConstraint(SpringLayout.SOUTH, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.SOUTH), Spring.constant(10)));
  }
}





SpringLayout.Constraints: setY(Spring y)

import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new SpringLayoutPanel());
    aWindow.setVisible(true);
  }
}
class SpringLayoutPanel extends JPanel {
  public SpringLayoutPanel() {
    SpringLayout layout = new SpringLayout();
    setLayout(layout);
    JButton[] buttons = new JButton[6];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new JButton("Press " + (i + 1));
      add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    Spring wSpring = Spring.constant(30, 80, 130);
    SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
    buttonConstr.setX(xSpring);
    buttonConstr.setY(ySpring);
    for (int i = 0; i < buttons.length; i++) {
      buttonConstr = layout.getConstraints(buttons[i]);
      buttonConstr.setHeight(ySpring);
      buttonConstr.setWidth(wSpring);
      if (i > 0) {
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring,
            SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring,
            SpringLayout.SOUTH, buttons[i - 1]);
      }
    }
    SpringLayout.Constraints constr = layout.getConstraints(this);
    constr.setConstraint(SpringLayout.EAST, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.EAST), Spring.constant(15)));
    constr.setConstraint(SpringLayout.SOUTH, Spring.sum(buttonConstr
        .getConstraint(SpringLayout.SOUTH), Spring.constant(10)));
  }
}