本文整理汇总了Java中javax.swing.SpringLayout.Constraints.setConstraint方法的典型用法代码示例。如果您正苦于以下问题:Java Constraints.setConstraint方法的具体用法?Java Constraints.setConstraint怎么用?Java Constraints.setConstraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.SpringLayout.Constraints
的用法示例。
在下文中一共展示了Constraints.setConstraint方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConstraints_SetHeight
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_SetHeight() throws Exception {
layout.getConstraints(container).setHeight(Spring.constant(5));
layout.getConstraints(button).setHeight(Spring.constant(6));
layout.getConstraints(button).setHeight(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setHeight(Spring.constant(8));
constraints.setY(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(40));
layout.getConstraints(panel).setHeight(Spring.constant(6));
constraints.setY(Spring.constant(20));
layout.getConstraints(innerButton).setHeight(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
assertEquals(300, container.getHeight());
assertEquals(8, button.getHeight());
assertEquals(8, textField.getHeight());
assertEquals(6, panel.getHeight());
assertEquals(0, innerButton.getHeight());
}
示例2: testConstraints_SetWidth
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_SetWidth() throws Exception {
layout.getConstraints(container).setWidth(Spring.constant(5));
layout.getConstraints(button).setWidth(Spring.constant(6));
layout.getConstraints(button).setWidth(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setWidth(Spring.constant(8));
constraints.setX(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(40));
layout.getConstraints(panel).setWidth(Spring.constant(6));
constraints.setX(Spring.constant(20));
layout.getConstraints(innerButton).setWidth(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
assertEquals(200, container.getWidth());
assertEquals(8, button.getWidth());
assertEquals(20, textField.getWidth());
assertEquals(6, panel.getWidth());
assertEquals(0, innerButton.getWidth());
}
示例3: testConstraints_SetX
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_SetX() throws Exception {
layout.getConstraints(container).setX(Spring.constant(5));
layout.getConstraints(button).setX(Spring.constant(6));
layout.getConstraints(button).setX(Spring.constant(7));
Constraints constraints = layout.getConstraints(textField);
constraints.setX(Spring.constant(8));
constraints.setWidth(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(30));
layout.getConstraints(panel).setX(Spring.constant(7));
constraints.setWidth(Spring.constant(20));
layout.getConstraints(innerButton).setX(Spring.constant(17));
container.setSize(200, 300);
layout.layoutContainer(container);
assertEquals(0, container.getX());
assertEquals(7, button.getX());
assertEquals(10, textField.getX());
assertEquals(7, panel.getX());
assertEquals(0, innerButton.getX());
}
示例4: testConstraints_SetY
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_SetY() throws Exception {
layout.getConstraints(container).setY(Spring.constant(5));
layout.getConstraints(button).setY(Spring.constant(6));
layout.getConstraints(button).setY(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setY(Spring.constant(8));
constraints.setHeight(Spring.constant(10));
constraints.setConstraint(SpringLayout.SOUTH, Spring.constant(40));
layout.getConstraints(panel).setY(Spring.constant(6));
constraints.setHeight(Spring.constant(20));
layout.getConstraints(innerButton).setY(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
assertEquals(0, container.getY());
assertEquals(8, button.getY());
assertEquals(20, textField.getY());
assertEquals(6, panel.getY());
assertEquals(0, innerButton.getY());
}
示例5: testInvalidateLayout
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testInvalidateLayout() throws Exception {
JFrame frame = new JFrame("testMinimumLayoutSize");
frame.setContentPane(container);
layout = new SpringLayout();
container.setLayout(layout);
Constraints buttonConstraints = layout.getConstraints(button);
buttonConstraints.setHeight(Spring.constant(33, 34, 35));
Constraints containerConstraints = layout.getConstraints(container);
containerConstraints.setConstraint(SpringLayout.SOUTH,
buttonConstraints.getConstraint(SpringLayout.SOUTH));
containerConstraints.setConstraint(SpringLayout.EAST,
buttonConstraints.getConstraint(SpringLayout.EAST));
containerConstraints.setConstraint(SpringLayout.WEST, Spring.constant(3));
frame.pack();
frame.setVisible(true);
frame.setSize(100, 200);
Spring width = buttonConstraints.getWidth();
if (isHarmony()) {
assertEquals(5, width.getValue());
} else {
assertEquals(115, width.getValue());
}
frame.setSize(200, 300);
layout.invalidateLayout(container);
assertSame(width, buttonConstraints.getWidth());
if (isHarmony()) {
assertEquals(5, width.getValue());
} else {
assertEquals(115, width.getValue());
}
}
示例6: testConstraints_GetHeight
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_GetHeight() throws Exception {
layout.getConstraints(container).setHeight(Spring.constant(5));
layout.getConstraints(button).setHeight(Spring.constant(6));
layout.getConstraints(button).setHeight(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setHeight(Spring.constant(8));
constraints.setY(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(40));
layout.getConstraints(panel).setHeight(Spring.constant(6));
constraints.setY(Spring.constant(20));
layout.getConstraints(innerButton).setHeight(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
SpringTest.assertSizes(5, 5, 5,
layout.getConstraints(container).getHeight());
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(button).getHeight());
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(textField).getHeight());
SpringTest.assertSizes(6, 6, 6,
layout.getConstraints(panel).getHeight());
SpringTest.assertSizes(15, 15, 15,
layout.getConstraints(innerButton).getHeight());
container.setSize(200, 300);
layout.layoutContainer(button);
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(button).getHeight());
}
示例7: testConstraints_GetWidth
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_GetWidth() throws Exception {
layout.getConstraints(container).setWidth(Spring.constant(5));
layout.getConstraints(button).setWidth(Spring.constant(6));
layout.getConstraints(button).setWidth(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setWidth(Spring.constant(8));
constraints.setX(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(40));
layout.getConstraints(panel).setWidth(Spring.constant(6));
constraints.setX(Spring.constant(20));
layout.getConstraints(innerButton).setWidth(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
SpringTest.assertSizes(5, 5, 5,
layout.getConstraints(container).getWidth());
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(button).getWidth());
SpringTest.assertSizes(20, 20, 20,
layout.getConstraints(textField).getWidth());
SpringTest.assertSizes(6, 6, 6,
layout.getConstraints(panel).getWidth());
SpringTest.assertSizes(15, 15, 15,
layout.getConstraints(innerButton).getWidth());
container.setSize(200, 300);
layout.layoutContainer(button);
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(button).getWidth());
}
示例8: testConstraints_GetX
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_GetX() throws Exception {
layout.getConstraints(container).setX(Spring.constant(5));
layout.getConstraints(button).setX(Spring.constant(6));
layout.getConstraints(button).setX(Spring.constant(7));
Constraints constraints = layout.getConstraints(textField);
constraints.setX(Spring.constant(8));
constraints.setWidth(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(30));
layout.getConstraints(panel).setX(Spring.constant(7));
constraints.setWidth(Spring.constant(20));
layout.getConstraints(innerButton).setX(Spring.constant(17));
container.setSize(200, 300);
layout.layoutContainer(container);
SpringTest.assertSizes(0, 0, 0,
layout.getConstraints(container).getX());
SpringTest.assertSizes(7, 7, 7,
layout.getConstraints(button).getX());
SpringTest.assertSizes(10, 10, 10,
layout.getConstraints(textField).getX());
SpringTest.assertSizes(7, 7, 7,
layout.getConstraints(panel).getX());
SpringTest.assertSizes(17, 17, 17,
layout.getConstraints(innerButton).getX());
container.setSize(200, 300);
layout.layoutContainer(button);
SpringTest.assertSizes(0, 0, 0,
layout.getConstraints(button).getX());
}
示例9: testConstraints_GetY
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_GetY() throws Exception {
layout.getConstraints(container).setY(Spring.constant(5));
layout.getConstraints(button).setY(Spring.constant(6));
layout.getConstraints(button).setY(Spring.constant(8));
Constraints constraints = layout.getConstraints(textField);
constraints.setY(Spring.constant(8));
constraints.setHeight(Spring.constant(10));
constraints.setConstraint(SpringLayout.SOUTH, Spring.constant(40));
layout.getConstraints(panel).setY(Spring.constant(6));
constraints.setHeight(Spring.constant(20));
layout.getConstraints(innerButton).setY(Spring.constant(15));
container.setSize(200, 300);
layout.layoutContainer(container);
SpringTest.assertSizes(0, 0, 0,
layout.getConstraints(container).getY());
SpringTest.assertSizes(8, 8, 8,
layout.getConstraints(button).getY());
SpringTest.assertSizes(20, 20, 20,
layout.getConstraints(textField).getY());
SpringTest.assertSizes(6, 6, 6,
layout.getConstraints(panel).getY());
SpringTest.assertSizes(15, 15, 15,
layout.getConstraints(innerButton).getY());
container.setSize(200, 300);
layout.layoutContainer(button);
SpringTest.assertSizes(0, 0, 0, layout.getConstraints(button).getY());
}
示例10: testMinimumLayoutSize
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testMinimumLayoutSize() throws Exception {
//container
setComponentSizes(button, Spring.constant(2, 5, 100), Spring.constant(3, 6, 101));
Constraints buttonConstraints = layout.getConstraints(button);
buttonConstraints.setHeight(Spring.constant(33, 34, 35));
Constraints containerConstraints = layout.getConstraints(container);
containerConstraints.setConstraint(SpringLayout.SOUTH,
buttonConstraints.getConstraint(SpringLayout.SOUTH));
containerConstraints.setConstraint(SpringLayout.EAST,
buttonConstraints.getConstraint(SpringLayout.EAST));
containerConstraints.setConstraint(SpringLayout.WEST, Spring.constant(3));
layout.layoutContainer(container);
assertEquals(2, layout.minimumLayoutSize(container).width);
assertEquals(33, layout.minimumLayoutSize(container).height);
//putConstraints()
assertEquals(button.getInsets().left + button.getInsets().right,
layout.minimumLayoutSize(button).width);
assertEquals(panel.getInsets().left + panel.getInsets().right,
layout.minimumLayoutSize(panel).width);
assertEquals(innerButton.getInsets().left + innerButton.getInsets().right,
layout.minimumLayoutSize(innerButton).width);
buttonConstraints.setX(Spring.constant(20));
assertEquals(button.getInsets().left + button.getInsets().right,
layout.minimumLayoutSize(button).width);
assertEquals(0, layout.getConstraint(SpringLayout.EAST, button).getPreferredValue());
buttonConstraints.setWidth(Spring.constant(100));
assertEquals(100 + button.getInsets().left + button.getInsets().right,
layout.minimumLayoutSize(button).width);
layout.getConstraints(innerButton).setX(Spring.constant(20));
assertEquals(innerButton.getInsets().left + innerButton.getInsets().right,
layout.minimumLayoutSize(innerButton).width);
layout.getConstraints(innerButton).setWidth(Spring.constant(100));
assertEquals(100 + innerButton.getInsets().left + innerButton.getInsets().right,
layout.minimumLayoutSize(innerButton).width);
layout.getConstraints(panel).setX(Spring.constant(20));
assertEquals(panel.getInsets().left + panel.getInsets().right,
layout.minimumLayoutSize(panel).width);
assertEquals(0, layout.getConstraint(SpringLayout.EAST, panel).getPreferredValue());
layout.getConstraints(panel).setWidth(Spring.constant(100));
assertEquals(100 + panel.getInsets().left + panel.getInsets().right,
layout.minimumLayoutSize(panel).width);
assertEquals(100, layout.getConstraint(SpringLayout.EAST, panel).getPreferredValue());
layout = new SpringLayout();
container.setLayout(layout);
layout.putConstraint(SpringLayout.SOUTH, button, Spring.constant(15),
SpringLayout.NORTH, container);
layout.putConstraint(SpringLayout.EAST, button, Spring.constant(5),
SpringLayout.WEST, container);
assertEquals(panel.getInsets().left + panel.getInsets().right,
layout.minimumLayoutSize(panel).width);
assertEquals(innerButton.getInsets().left + innerButton.getInsets().right,
layout.minimumLayoutSize(innerButton).width);
assertEquals(5 + button.getInsets().left + button.getInsets().right,
layout.minimumLayoutSize(button).width);
}
示例11: testConstraints_SetConstraint
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
public void testConstraints_SetConstraint() throws Exception {
Constraints constraints;
layout.getConstraints(container).setConstraint(SpringLayout.WEST,
Spring.constant(5));
layout.getConstraints(button).setConstraint(SpringLayout.WEST,
Spring.constant(6));
layout.getConstraints(button).setX(Spring.constant(7));
constraints = layout.getConstraints(textField);
constraints.setX(Spring.constant(8));
constraints.setWidth(Spring.constant(10));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(30));
layout.getConstraints(panel).setConstraint(SpringLayout.WEST,
Spring.constant(7));
constraints.setWidth(Spring.constant(20));
layout.getConstraints(panel).setConstraint(SpringLayout.EAST,
Spring.constant(17));
constraints = layout.getConstraints(innerButton);
constraints.setX(Spring.constant(18));
constraints.setWidth(Spring.constant(20));
constraints.setConstraint(SpringLayout.EAST, Spring.constant(50));
container.setSize(200, 300);
layout.layoutContainer(container);
assertEdges(layout.getConstraints(container),
0, 200, 0, 300);
assertEdges(layout.getConstraints(button),
7, 12, 0, 5);
assertEdges(layout.getConstraints(textField),
10, 30, 0, 10);
if (isHarmony()) {
assertEdges(layout.getConstraints(panel),
7, 17, 0, 3);
} else {
assertEdges(layout.getConstraints(panel),
14, 17, 0, 3);
}
assertEdges(layout.getConstraints(innerButton),
30, 50, 0, 10);
component = new JPanel();
setComponentSizes(component, Spring.constant(10), Spring.constant(5));
layout.getConstraints(panel).setConstraint(SpringLayout.NORTH,
Spring.constant(1));
assertEdges(layout.getConstraints(component),
0, 10, 0, 5);
}
示例12: mouseDragged
import javax.swing.SpringLayout.Constraints; //导入方法依赖的package包/类
/**
* Called when the mouse is dragged and the shift key is down.
* @param event the event
*/
private void mouseDragged(MouseEvent event) {
// How much we've moved from the original location
int deltaX = event.getXOnScreen() - screenX;
int deltaY = event.getYOnScreen() - screenY;
Container parent = legend.getParent();
SpringLayout layout = (SpringLayout) parent.getLayout();
layout.removeLayoutComponent(legend);
// The new location, in parent coordinates
int x = myX + deltaX;
int y = myY + deltaY;
// See if we snap to anything.
boolean snapLeft = false;
boolean snapRight = false;
boolean snapTop = false;
boolean snapBottom = false;
int legendWidth = legend.getWidth();
int legendHeight = legend.getHeight();
for(Component snapTo : this.snapTo) {
// Current location of the top left corner of the legend, in snapTo coordinates.
Point p = SwingUtilities.convertPoint(parent, x, y, snapTo);
if(!snapLeft && !snapRight) {
if(Math.abs(p.x) <= snapDist) {
snapLeft = true;
layout.putConstraint(SpringLayout.WEST, legend, 0, SpringLayout.WEST, snapTo);
x = SwingUtilities.convertPoint(snapTo, 0, 0, parent).x;
} else if(Math.abs(p.x + legendWidth - snapTo.getWidth()) <= snapDist) {
snapRight = true;
layout.putConstraint(SpringLayout.EAST, legend, 0, SpringLayout.EAST, snapTo);
x = SwingUtilities.convertPoint(snapTo, snapTo.getWidth() - legendWidth, 0, parent).x;
}
}
if(!snapTop && !snapBottom) {
if(Math.abs(p.y) <= snapDist) {
snapTop = true;
layout.putConstraint(SpringLayout.NORTH, legend, 0, SpringLayout.NORTH, snapTo);
y = SwingUtilities.convertPoint(snapTo, 0, 0, parent).y;
} else if(Math.abs(p.y + legendHeight - snapTo.getHeight()) <= snapDist) {
snapBottom = true;
layout.putConstraint(SpringLayout.SOUTH, legend, 0, SpringLayout.SOUTH, snapTo);
y = SwingUtilities.convertPoint(snapTo, 0, snapTo.getHeight() - legendHeight, parent).y;
}
}
}
// Set the location directly for immediate effect
legend.setLocation(x, y);
// Set the location in the layout so it stays where it was, percentagewise.
// In other words, if the center of the legend was 33% of the way from the left to the right of the parent,
// it will remain 33% of the way from the left to the right of the parent even if the parent is resized.
Constraints constraints = layout.getConstraints(legend);
if(!snapTop && !snapBottom) {
float py = (y + legendHeight * .5f) / (float) parent.getHeight();
constraints.setConstraint(SpringLayout.VERTICAL_CENTER, Spring.scale(new HeightSpring(parent), py));
}
if(!snapLeft && !snapRight) {
float px = (x + legendWidth * .5f) / (float) parent.getWidth();
constraints.setConstraint(SpringLayout.HORIZONTAL_CENTER, Spring.scale(new WidthSpring(parent), px));
}
// Hide borders on the sides that have been snapped to.
Border outsideBorder = BorderFactory.createMatteBorder(snapTop ? 0 : 1, snapLeft ? 0 : 1, snapBottom ? 0 : 1,
snapRight ? 0 : 1, Color.darkGray);
Border insideBorder = ((CompoundBorder) legend.getBorder()).getInsideBorder();
legend.setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
}