弹簧布局Jbutton消失,大小混乱

时间:2012-02-23 23:44:03

标签: java swing jbutton springlayout

好的,这是一个使用Spring Layout的普通通用JFrame程序。 有两个问题。 首先,有没有办法调整按钮,使它们大小相同? 其次,在按下第二个按钮(输出尺寸的按钮)后,按钮消失,其中一个按钮始终显示在顶部。这似乎只有在从按钮访问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();
  }
}

0 个答案:

没有答案