【问题标题】:Minimize/maximize parent JFrame最小化/最大化父 JFrame
【发布时间】:2014-07-10 01:23:23
【问题描述】:

我正在做一个测验应用程序。当一个团队按下他们的按钮时,他们仍然不能看到问题或看到视频/音频片段。这种信息显示在添加到父 JFrame 的 quizview(这是一个 jpanel )中。我要做的是在团队按下按钮时最小化 JFrame。这完美地工作。如果按下按钮,管理员会在他的视图中弹出一个窗口。如果答案不正确,则应再次最大化 JFrame。因此,当他按下按钮时,如果答案不正确,则会在测验模型( $maximize )中将布尔值设置为 true。然后我们更新视图。在视图的更新中,我检查布尔值是否设置为真。如果是真的,我调用最大化方法。并在答案错误时再次最大化它。最小化效果很好,但最大化却不行。

有谁知道怎么回事?

这是我在视图中的代码,这个视图是更大 JFrame 中的 JPanel,最大化发生的地方:

public void update(Observable arg0, Object arg1) {
    $question = (Question) arg1;

    if(((QuizModel) getModel()).getMaximize()) /* Only maximize the JFrame when needed */
        maximizeFrame(); /* Maximize the frame first */     



    repaint();
}


/**
 * Maximize the parent JFrame so the teams can see the question
 */
protected void maximizeFrame() {
    System.out.println("MAXIMIZE");
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    topFrame.setState(JFrame.MAXIMIZED_BOTH);

}

最小化发生在团队按下按钮时,代码如下:

/**
 * If a button gets pressed we call this function
 * @param team the team that pressed their button
 */
protected void buttonPressed(int team) {
    /* Check if a button is pressed */
    if(((QuizModel) getModel()).getTeamPressed() > 0)
        $isPressed = true;
    else
        $isPressed = false;

    /* If there hasn't been pressed yet and the question is not null */
    if(!$isPressed && $question != null){
        minimizeFrame(); /* Minimize the frame */

        /* If this question has a media path we need to pause the audio/video, we also check if the user has installed vlcplayer and selected the right path */
        if($question.getMediaPath() != null && QuizSoftwareModel.$vlcPath != null)
            ((QuizController)getController()).pause(); /* Pause the video */

        /* Give a pop up message to the admin that a team has pushed their button */
        ((QuizController)getController()).showScoreView(team);
    }

}

/**
 * Minimize the parent JFrame so the teams can't see the question anymore 
 */
protected void minimizeFrame() {
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    topFrame.setState(JFrame.ICONIFIED);

}

[编辑] 减少了代码。

谢谢!

【问题讨论】:

  • 拜托现在不是发布 SSCCE/MCVE 的时候
  • 这不是PHP,不要在变量名中使用$
  • @TimB Nope 已经试过了,还是不行。
  • “他们在学校告诉我们为面向对象的 java 做这个”这句话没有意义。 Java 是面向对象的,使用 $ 是 PHP 中主要使用的东西,与面向对象的语言无关。删除它以获得更好的代码可读性。

标签: java swing parent-child minimize maximize


【解决方案1】:

解决方案是使用 topFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);而不是 setState。 setstate 只设置当前框架的状态,因为我需要使用 setExtendedState 的父框架。

还遗漏了一些布尔值,以便在需要时最大化/最小化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    相关资源
    最近更新 更多