Spring Layout Jbutton исчезает и размер портится - PullRequest
0 голосов
/ 24 февраля 2012

Хорошо, вот обычная программа JFrame, использующая Spring Layout. Есть две проблемы. Во-первых, есть ли способ настроить кнопки так, чтобы они все были одинакового размера? Во-вторых, после нажатия второй кнопки (той, которая выводит размерность) кнопка исчезает, и одна из них всегда идет вверх. Кажется, это происходит только при доступе к измерению get с помощью кнопки. Есть идеи, что происходит?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CodeExample1 extends JFrame implements ActionListener
{
  JPanel frame;
  SpringLayout layout;
  JButton layoutAlignment;
  JButton dimensions;
  JButton compress;
  JButton lift;
  public CodeExample1 ()
  {
    super("Spring Layout");
    setSize(640,500);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    setResizable(false);
    setVisible(true);
    springLayout();
  }
  public void springLayout()
  {
    frame=new JPanel();
    layout =new SpringLayout();
    frame.setLayout(layout);
    layoutAlignment=new JButton("Click to find out this button's layout alignment!");
    dimensions=new JButton("Click to find out this button's dimension settings!");
    compress=new JButton("Click to compress the buttons!");
    lift=new JButton("Click to expand the buttons!"); 

    layout.putConstraint(SpringLayout.WEST,layoutAlignment,5,SpringLayout.WEST,frame);
    layout.putConstraint(SpringLayout.WEST,dimensions,5,SpringLayout.WEST,frame);
    layout.putConstraint(SpringLayout.WEST,compress,5,SpringLayout.WEST,frame);
    layout.putConstraint(SpringLayout.WEST,lift,5,SpringLayout.WEST,frame);

    layout.putConstraint(SpringLayout.NORTH,layoutAlignment,5,SpringLayout.NORTH,frame);
    layout.putConstraint(SpringLayout.SOUTH,layoutAlignment,-5,SpringLayout.NORTH,dimensions);
    layout.putConstraint(SpringLayout.SOUTH,dimensions,-5,SpringLayout.NORTH,compress);
    layout.putConstraint(SpringLayout.SOUTH,compress,-5,SpringLayout.NORTH,lift);
    layout.putConstraint(SpringLayout.SOUTH,lift,-5,SpringLayout.SOUTH,frame);

    layout.putConstraint(SpringLayout.EAST,layoutAlignment,-5,SpringLayout.EAST,frame);
    layout.putConstraint(SpringLayout.EAST,dimensions,-5,SpringLayout.EAST,frame);  
    layout.putConstraint(SpringLayout.EAST,compress,-5,SpringLayout.EAST,frame);    
    layout.putConstraint(SpringLayout.EAST,lift,-5,SpringLayout.EAST,frame);

    layoutAlignment.addActionListener(this);
    dimensions.addActionListener(this);
    compress.addActionListener(this);
    lift.addActionListener(this);
    add(frame);
    frame.add(layoutAlignment);
    frame.add(dimensions);
    frame.add(compress);
    frame.add(lift);

  }
  public void actionPerformed(ActionEvent e)
  {
    JButton tempB=(JButton)(e.getSource());
    if(tempB==lift ||tempB==compress)
    {
      int temp;
      if(tempB==lift)
      {
        temp=(layout.getConstraints(lift).getConstraint(SpringLayout.SOUTH).getPreferredValue())+10;
      }
      else
      {
        temp=(layout.getConstraints(lift).getConstraint(SpringLayout.SOUTH).getPreferredValue())-10;
      }
      if(temp<0 && temp>-180)
      {
        layout.putConstraint(SpringLayout.SOUTH,lift,temp,SpringLayout.SOUTH,frame);
        layout.putConstraint(SpringLayout.NORTH,layoutAlignment,-temp,SpringLayout.NORTH,frame);

        layout.putConstraint(SpringLayout.WEST,layoutAlignment,-temp,SpringLayout.WEST,frame);
        layout.putConstraint(SpringLayout.WEST,dimensions,-temp,SpringLayout.WEST,frame);
        layout.putConstraint(SpringLayout.WEST,compress,-temp,SpringLayout.WEST,frame);
        layout.putConstraint(SpringLayout.WEST,lift,-temp,SpringLayout.WEST,frame);          

        layout.putConstraint(SpringLayout.EAST,layoutAlignment,temp,SpringLayout.EAST,frame);
        layout.putConstraint(SpringLayout.EAST,dimensions,temp,SpringLayout.EAST,frame);  
        layout.putConstraint(SpringLayout.EAST,compress,temp,SpringLayout.EAST,frame);    
        layout.putConstraint(SpringLayout.EAST,lift,temp,SpringLayout.EAST,frame);
      }
      frame.revalidate();
    }
    else if(tempB==layoutAlignment)
    {
      JOptionPane.showMessageDialog(frame,"The x layout alignment is: "+Float.toString(layout.getLayoutAlignmentX(layoutAlignment))+". The y layout alignment is: "+Float.toString(layout.getLayoutAlignmentY(layoutAlignment)));
    }
    else
    {
      System.out.println(layout.minimumLayoutSize(dimensions));
      frame.revalidate();
    }
  }
  public static void main (String [] args)
  {
    new CodeExample1();
  }
}
...