我需要帮助来调整定时器代码到我的代码 [英] I need help to adjust timer code to my code

查看:83
本文介绍了我需要帮助来调整定时器代码到我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre lang =   C# >  
import javax.swing。*; // 图形
import java.awt.Color; // 图形颜色
import java.awt。事件 .ActionListener; // Evrnts
import java.awt。事件 .ActionEvent; // Evrnts

public class ButtonDemo_Extended实现ActionListener {

// 全局值的定义和属于GUI的项目。
int redScoreAmount = 0 ;
int blueScoreAmount = 0 ;

JPanel titlePanel,scorePanel,buttonPanel;
JLabel redLabel,blueLabel,redScore,blueScore;
JButton redButton,blueButton,resetButton;

public JPanel createContentPane(){

// 我们创建一个底部JPanel来放置所有内容。
JPanel totalGUI = new JPanel( );
totalGUI.setLayout( null );

// 创建一个包含标题标签的面板
titlePanel = new JPanel();
titlePanel.setLayout( null );
titlePanel.setLocation( 10 10 );
titlePanel.setSize( 250 30 );
totalGUI。 add (titlePanel);

redLabel = new JLabel(& quot; Red Team& quot;);
redLabel.setLocation( 0 0 );
redLabel.setSize( 120 30 );
redLabel.setHorizo​​ntalAlignment( 0 );
redLabel.setForeground(Color.red);
titlePanel。 add (redLabel);

blueLabel = new JLabel(& quot; Blue Team& quot;);
blueLabel.setLocation( 130 0 );
blueLabel.setSize( 120 30 );
blueLabel.setHorizo​​ntalAlignment( 0 );
blueLabel.setForeground(Color.blue);
titlePanel。 add (blueLabel);

// 创建一个包含分数标签的Panel。
scorePanel = new JPanel();
scorePanel.setLayout( null );
scorePanel.setLocation( 10 40 );
scorePanel.setSize( 260 30 );
totalGUI。 add (scorePanel);

redScore = new JLabel(& quot;& quot; + redScoreAmount);
redScore.setLocation( 0 0 );
redScore.setSize( 120 30 );
redScore.setHorizo​​ntalAlignment( 0 );
scorePanel。 add (redScore);

blueScore = new JLabel(& quot;& quot; + blueScoreAmount);
blueScore.setLocation( 130 0 );
blueScore.setSize( 120 30 );
blueScore.setHorizo​​ntalAlignment( 0 );
scorePanel。 add (blueScore);

// 创建一个包含所有JButton的面板。
buttonPanel = new JPanel();
buttonPanel.setLayout( null );
buttonPanel.setLocation( 10 80 );
buttonPanel.setSize( 260 70 );
totalGUI。 add (buttonPanel);

// 我们创建一个按钮并使用我们的语法对其进行操作
// 之前使用过。现在每个按钮都有一个ActionListener发布
// 当按下按钮时其动作输出。
redButton = new JButton(& quot; Red Score!& quot;);
redButton.setLocation( 0 0 );
redButton.setSize( 120 30 );
redButton.addActionListener( this );
buttonPanel。 add (redButton);

blueButton = new JButton(& quot; Blue Score!& quot;);
blueButton.setLocation( 130 0 );
blueButton.setSize( 120 30 );
blueButton.addActionListener( this );
buttonPanel。 add (blueButton);

resetButton = new JButton(& quot; Reset Score& quot;);
resetButton.setLocation( 0 40 );
resetButton.setSize( 250 30 );
resetButton.addActionListener( this );
buttonPanel。 add (resetButton);
return totalGUI;
}

// 这是新的ActionPerformed方法。
// 它捕获附加了ActionListener的任何事件。
// 使用if语句,我们可以确定按下了哪个按钮
// 并在我们的GUI中更改相应的值。
public void actionPerformed(ActionEvent e){
if (e.getSource()== redButton ){
redScoreAmount = redScoreAmount + 1 ;
redScore.setText(& quot;& quot; + redScoreAmount);
JOptionPane.showMessageDialog(buttonPanel,& quot; GOOOOOOOOOOOL& quot;);
} else if (e.getSource()== blueButton){
blueScoreAmount = blueScoreAmount + 1 ;
blueScore.setText(& quot;& quot; + blueScoreAmount);
JOptionPane.showMessageDialog(buttonPanel,& quot; GOOOOOOOOOOOL& quot;);
} else if (e.getSource()== resetButton){
redScoreAmount = 0 ;
blueScoreAmount = 0 ;
redScore.setText(& quot;& quot; + redScoreAmount);
blueScore.setText(& quot;& quot; + blueScoreAmount);
}
}

私有 静态 void createAndShowGUI(){ // 对于这个类Onlyyyyyyyyyyyyy。

JFrame.setDefaultLookAndFeelDecorated( true ); // 框架样式
JFrame frame = JFrame(& quot; [=] JButton Scores![=]& quot;);

// 创建并设置内容窗格。
ButtonDemo_Extended demo = new ButtonDemo_Extended();
frame.setContentPane(demo.createContentPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 280 190 );
frame.setVisible( true );
}

public static void main( String [] args){

createAndShowGUI();

}
} < / pre >





我的尝试:



(我的java程序是分数应用程序,当我点击按钮并弹出新窗口时将分数提高1表示目标我想让它成为新窗口关闭自己的计时器)一些帮助请

解决方案

showMessageDialog 是没用的,因为它需要用户输入关闭。您需要使用 https://docs.oracle.com/javase/中所述的无模式对话框tutorial / uiswing / misc / modality.html [ ^ ]。

 <pre lang="C#">
import javax.swing.*; // Graphics
import java.awt.Color; // Graphics Colors
import java.awt.event.ActionListener; // Evrnts
import java.awt.event.ActionEvent; // Evrnts

public class ButtonDemo_Extended implements ActionListener {

    // Definition of global values and items that are part of the GUI.
    int redScoreAmount = 0;
    int blueScoreAmount = 0;

    JPanel titlePanel, scorePanel, buttonPanel;
    JLabel redLabel, blueLabel, redScore, blueScore;
    JButton redButton, blueButton, resetButton;

    public JPanel createContentPane() {

        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);

        // Creation of a Panel to contain the title labels
        titlePanel = new JPanel();
        titlePanel.setLayout(null);
        titlePanel.setLocation(10, 10);
        titlePanel.setSize(250, 30);
        totalGUI.add(titlePanel);

        redLabel = new JLabel(&quot;Red Team&quot;);
        redLabel.setLocation(0, 0);
        redLabel.setSize(120, 30);
        redLabel.setHorizontalAlignment(0);
        redLabel.setForeground(Color.red);
        titlePanel.add(redLabel);

        blueLabel = new JLabel(&quot;Blue Team&quot;);
        blueLabel.setLocation(130, 0);
        blueLabel.setSize(120, 30);
        blueLabel.setHorizontalAlignment(0);
        blueLabel.setForeground(Color.blue);
        titlePanel.add(blueLabel);

        // Creation of a Panel to contain the score labels.
        scorePanel = new JPanel();
        scorePanel.setLayout(null);
        scorePanel.setLocation(10, 40);
        scorePanel.setSize(260, 30);
        totalGUI.add(scorePanel);

        redScore = new JLabel(&quot;&quot; + redScoreAmount);
        redScore.setLocation(0, 0);
        redScore.setSize(120, 30);
        redScore.setHorizontalAlignment(0);
        scorePanel.add(redScore);

        blueScore = new JLabel(&quot;&quot; + blueScoreAmount);
        blueScore.setLocation(130, 0);
        blueScore.setSize(120, 30);
        blueScore.setHorizontalAlignment(0);
        scorePanel.add(blueScore);

        // Creation of a Panel to contain all the JButtons.
        buttonPanel = new JPanel();
        buttonPanel.setLayout(null);
        buttonPanel.setLocation(10, 80);
        buttonPanel.setSize(260, 70);
        totalGUI.add(buttonPanel);

        // We create a button and manipulate it using the syntax we have
        // used before. Now each button has an ActionListener which posts 
        // its action out when the button is pressed.
        redButton = new JButton(&quot;Red Score!&quot;);
        redButton.setLocation(0, 0);
        redButton.setSize(120, 30);
        redButton.addActionListener(this);
        buttonPanel.add(redButton);

        blueButton = new JButton(&quot;Blue Score!&quot;);
        blueButton.setLocation(130, 0);
        blueButton.setSize(120, 30);
        blueButton.addActionListener(this);
        buttonPanel.add(blueButton);

        resetButton = new JButton(&quot;Reset Score&quot;);
        resetButton.setLocation(0, 40);
        resetButton.setSize(250, 30);
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);
        return totalGUI;
    }

    // This is the new ActionPerformed Method.
    // It catches any events with an ActionListener attached.
    // Using an if statement, we can determine which button was pressed
    // and change the appropriate values in our GUI.
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == redButton) {
            redScoreAmount = redScoreAmount + 1;
            redScore.setText(&quot;&quot; + redScoreAmount);
            JOptionPane.showMessageDialog(buttonPanel, &quot;GOOOOOOOOOOOL&quot;);
        } else if (e.getSource() == blueButton) {
            blueScoreAmount = blueScoreAmount + 1;
            blueScore.setText(&quot;&quot; + blueScoreAmount);
            JOptionPane.showMessageDialog(buttonPanel, &quot;GOOOOOOOOOOOL&quot;);
        } else if (e.getSource() == resetButton) {
            redScoreAmount = 0;
            blueScoreAmount = 0;
            redScore.setText(&quot;&quot; + redScoreAmount);
            blueScore.setText(&quot;&quot; + blueScoreAmount);
        }
    }

    private static void createAndShowGUI() { // For this Class Onlyyyyyyyyyyyy .

        JFrame.setDefaultLookAndFeelDecorated(true); // Style Of Frame
        JFrame frame = new JFrame(&quot;[=] JButton Scores! [=]&quot;);

        //Create and set up the content pane.
        ButtonDemo_Extended demo = new ButtonDemo_Extended();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(280, 190);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        createAndShowGUI();

    }
}</pre>



What I have tried:

( my java program is score app that increase score by 1 when i click on the button and pops up new window says goal what i want it to make the new window close itself by timer ) some help please

解决方案

showMessageDialog is no use because it needs user input to close. You need to use a Modeless dialog as described in https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html[^].


这篇关于我需要帮助来调整定时器代码到我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆