Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing JDialog';s的内容窗格在执行幻灯片时未更新_Java_Swing_Animation_Jdialog_Event Dispatch Thread - Fatal编程技术网

Java Swing JDialog';s的内容窗格在执行幻灯片时未更新

Java Swing JDialog';s的内容窗格在执行幻灯片时未更新,java,swing,animation,jdialog,event-dispatch-thread,Java,Swing,Animation,Jdialog,Event Dispatch Thread,大家好 我试图实现的是,当按下按钮时,通过从底部滑入,在框架上显示一个对话框(带有键盘,但这超出了本问题的范围) 我是swing的初学者,如果问题明显,请不要责怪我。 我抛出了一些代码,实现了这一点(有一些缺陷),但是在开始和结束位置之间的对话框的实际位置没有显示给用户。动画完成后将显示该对话框,但不显示该对话框。 有人知道为什么他们没有出现吗 public class TestSliding extends JFrame { private static Window kbdOwner; p

大家好

我试图实现的是,当按下按钮时,通过从底部滑入,在框架上显示一个对话框(带有键盘,但这超出了本问题的范围)

我是swing的初学者,如果问题明显,请不要责怪我。
我抛出了一些代码,实现了这一点(有一些缺陷),但是在开始和结束位置之间的对话框的实际位置没有显示给用户。动画完成后将显示该对话框,但不显示该对话框。 有人知道为什么他们没有出现吗

public class TestSliding
extends JFrame {

private static Window kbdOwner;
private static final double kbdHeightRatio = 1d / 4d;
private static final long kbdSlideDurationMs = 3000;

public static Point getKbdLocation() {
  int x = (int) kbdOwner.getBounds().getLocation().getX();
  int y =
    (int) (kbdOwner.getBounds().getLocation().getY() + kbdOwner.getBounds().getSize().getHeight() - getKbdSize()
        .getHeight());

  return new Point(x, y);
}

public static Dimension getKbdSize() {
  int width = (int) kbdOwner.getBounds().getSize().getWidth();
  int height = (int) (width * kbdHeightRatio);

  return new Dimension(width, height);
}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
      new TestSliding().setVisible(true);
    }
  });
}

private JButton toogleBtn = new JButton("Toogle");
private VirtualKeyboardSlide slide;

public TestSliding() {
  super();
  kbdOwner = this;
  setTitle("Test Sliding");
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setLayout(new BorderLayout());
  setBackground(Color.BLUE);

  setResizable(false);
  setSize(1024, 768);

  toogleBtn.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent argE) {
      switch (getSlide().getState()) {
        case OUT: {
          getSlide().in();
          break;
        }

        case IN: {
          getSlide().out();
          break;
        }
      }
    }
  });

  JPanel panel = new JPanel();
  getContentPane().add(panel);
  panel.add(toogleBtn);
}

public VirtualKeyboardSlide getSlide() {
  if (slide == null) {
    slide = new VirtualKeyboardSlide();
    slide.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    slide.pack();
    slide.setSize(getKbdSize());
    slide.setLocation(getKbdLocation());
    slide.setVisible(true);
  }

  return slide;
}

public interface IVirtualKeyboardPane {
  public void in();
  public void out();
}

public class VirtualKeyboardSlide
....
// see below
....

public class VirtualKeyboardPane
....
// see below
....

}
对话框:

private class VirtualKeyboardSlide
  extends JDialog {

private static final long serialVersionUID = 1L;

private KeyboardState kbdState_ = OUT;

private double height;
private double width;

/**
 * Constructs a <code>VirtualKeyboardDialog</code>.
 */
public VirtualKeyboardSlide() {
  this(new VirtualKeyboardPane());
}

public <T extends Container & IVirtualKeyboardPane> VirtualKeyboardSlide(T contentPane) {
  super(kbdOwner, ModalityType.MODELESS);
  setContentPane(contentPane);
  setUndecorated(true);
  setState(OUT);
  setBackground(new Color(0, 0, 0, 0));
}

/**
 * Returns the state of the keyboard
 * @return the state of the keyboard
 */
public synchronized KeyboardState getState() {
  return kbdState_;
}

public void in() {
  if (canSlideIn()) {
    setState(SLIDING_IN);

    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        getVirtualKeyboardPane().in();
        setState(IN);
      }
    });
  }
}

public void out() {
  if (canSlideOut()) {
    setState(SLIDING_OUT);

    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        getVirtualKeyboardPane().out();
        setState(OUT);
      }
    });
  }
}

/**
 * Sets the keyboard state
 * @param argState
 */
public synchronized void setState(KeyboardState argState) {
  kbdState_ = argState;
}

/**
 * Returns true if the keyboard can slide in now
 * @return true if the keyboard can slide in now
 */
protected boolean canSlideIn() {
  if (!getState().equals(OUT)) {
    return false;
  }

  return true;
}

/**
 * Returns true if the keyboard can slide out now
 * @return true if the keyboard can slide out now
 */
protected boolean canSlideOut() {
  if (!getState().equals(IN)) {
    return false;
  }

  return true;
}

protected IVirtualKeyboardPane getVirtualKeyboardPane() {
  return (IVirtualKeyboardPane) getContentPane();
}

}
KeyboardState是定义键盘状态的枚举:

public enum KeyboardState {
  SLIDING_IN, SLIDING_OUT, IN, OUT, DIED
}
对话框的内容窗格由以下类表示:

public class VirtualKeyboardPane
  extends JPanel
  implements IVirtualKeyboardPane {

private static final long serialVersionUID = 1L;

private int height_;
private int width_;

private int normalHeight_;
private int normalWidth_;

private int x_;
private int y_;

public VirtualKeyboardPane() {
  super();
  setFocusable(false);
  setOpaque(false);
  setBorder(BorderFactory.createEmptyBorder());
}

/** {@inheritDoc} */
@Override
public void in() {
  normalHeight_ = (int) getKbdSize().getHeight();
  normalWidth_ = (int) getKbdSize().getWidth();
  width_ = normalWidth_;
  x_ = 0;
  y_ = 0;

  long currentTime = System.currentTimeMillis();
  long startTime = currentTime;
  long endTime = currentTime + kbdSlideDurationMs;

  height_ = 0;
  while (currentTime < endTime) {
    long elapsedTime = currentTime - startTime;
    float f = ((float) elapsedTime / (float) kbdSlideDurationMs);

    height_ = (int) (f * normalHeight_);

    y_ = normalHeight_ - height_;

    repaint();
    currentTime = System.currentTimeMillis();
  }
}

/** {@inheritDoc} */
@Override
public void out() {
  normalHeight_ = (int) getKbdSize().getHeight();
  normalWidth_ = (int) getKbdSize().getWidth();
  width_ = normalWidth_;
  x_ = 0;
  y_ = normalHeight_;

  long currentTime = System.currentTimeMillis();
  long startTime = currentTime;
  long endTime = currentTime + kbdSlideDurationMs;

  height_ = normalHeight_;
  while (currentTime < endTime) {
    long elapsedTime = currentTime - startTime;
    float f = ((float) elapsedTime / (float) kbdSlideDurationMs);

    height_ = normalHeight_ - (int) (f * normalHeight_);

    y_ = normalHeight_ - height_;

    repaint();
    currentTime = System.currentTimeMillis();
  }
}

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.setColor(new Color(RGBToFloat(255), RGBToFloat(255), RGBToFloat(255), 0.5f));
  // g.fillRect((int) x_, (int) y_, (int) width_, (int) height_);
  g.fillRect(x_, y_, width_, height_);
}

private float RGBToFloat(int rgbValue) {
  return (rgbValue - 0.5f) / 255f;
}
}
公共类VirtualKeyboardPane
扩展JPanel
实现IVirtualKeyboardPane{
私有静态最终长serialVersionUID=1L;
私人内部高度!;
私有整数宽度;
私有int-normalHeight;
私有int-normalWidth;
私有int x_2;;
私家侦探;
公共VirtualKeyboardPane(){
超级();
设置聚焦(假);
设置不透明(假);
setboorder(BorderFactory.createEmptyByOrder());
}
/**{@inheritardoc}*/
@凌驾
在()中公开无效{
法线高度=(int)getKbdSize().getHeight();
normalWidth=(int)getKbdSize().getWidth();
宽度=正常宽度;
x=0;
y=0;
长currentTime=System.currentTimeMillis();
长启动时间=当前时间;
long-endTime=当前时间+kbdSlideDurationMs;
高度=0;
while(当前时间<结束时间){
long elapsedTime=当前时间-开始时间;
浮点f=((浮点)延时/(浮点)kbdSlideDurationMs);
高度=(int)(f*正常高度);
y=正常高度-高度;
重新油漆();
currentTime=System.currentTimeMillis();
}
}
/**{@inheritardoc}*/
@凌驾
公开作废{
法线高度=(int)getKbdSize().getHeight();
normalWidth=(int)getKbdSize().getWidth();
宽度=正常宽度;
x=0;
y=正常高度;
长currentTime=System.currentTimeMillis();
长启动时间=当前时间;
long-endTime=当前时间+kbdSlideDurationMs;
高度=正常高度;
while(当前时间<结束时间){
long elapsedTime=当前时间-开始时间;
浮点f=((浮点)延时/(浮点)kbdSlideDurationMs);
高度=正常高度-(int)(f*正常高度);
y=正常高度-高度;
重新油漆();
currentTime=System.currentTimeMillis();
}
}
@凌驾
受保护组件(图形g){
超级组件(g);
g、 setColor(新颜色(RGBToFloat(255)、RGBToFloat(255)、RGBToFloat(255)、0.5f));
//g.fillRect((int)x_uux,(int)y_ux,(int)宽度_ux,(int)高度_x);
g、 fillRect(x,y,宽度,高度);
}
专用浮点RGBToFloat(int rgbValue){
返回值(RGB值-0.5f)/255f;
}
}

问题在于,您的方法
in()
out()
正在EDT上运行,并一直阻止它,直到它们结束,这会阻止重新绘制事件被分派并显示修改后的键盘位置

使用摆动计时器来调整动画的速度。这将允许中间重新绘制实际发生