簡體   English   中英

如何將動作偵聽器設置為3個按鈕

[英]How to set Action Listener to 3 buttons

我正在嘗試制作帶有三個按鈕的秒表:“開始”,“暫停”和“停止”。 我的老師只教我們如何將動作監聽器設置為兩個按鈕。 如何將動作監聽器設置為三個按鈕? 到目前為止,這是我的編碼

JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");

startButton.addActionListener(this);
stopButton.addActionListener(this);

public void actionPerformed(ActionEvent actionEvent) {
    Calendar aCalendar = Calendar.getInstance();
    if (actionEvent.getActionCommand().equals("Start")){
        start = aCalendar.getTimeInMillis();
        aJLabel.setText("Stopwatch is running...");
    } else {
        aJLabel.setText("Elapsed time is: " + 
                (double) (aCalendar.getTimeInMillis() - start) / 1000 );

    }
}

我還沒有為“暫停”功能設置任何動作監聽器,因為無論如何我都不知道如何暫停計時器。 但是我想先將動作鏈接到按鈕,然后再弄清楚如何暫停。

您正在尋找的是if-then-else if-then語句。

基本上,將ActionListener添加到所有三個按鈕中……

JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");

startButton.addActionListener(this);
stopButton.addActionListener(this);
pauseButton.addActionListener(this);

然后提供if-else-if系列條件以測試每種可能的情況(您正在期待)

public void actionPerformed(ActionEvent e) {
    Calendar aCalendar = Calendar.getInstance();
    if (e.getSource() == startButton){
        start = aCalendar.getTimeInMillis();
        aJLabel.setText("Stopwatch is running...");
    } else if (e.getSource() == stopButton) {
        aJLabel.setText("Elapsed time is: " + 
                (double) (aCalendar.getTimeInMillis() - start) / 1000 );
    } else if (e.getSource() == pauseButton) {
        // Do pause stuff
    }
}

仔細查看if-then和if-then-else語句以獲取更多詳細信息

與其嘗試使用對按鈕的引用, AcionEvent考慮使用AcionEventactionCommand屬性,這意味着您將不需要引用原始按鈕...

public void actionPerformed(ActionEvent e) {
    Calendar aCalendar = Calendar.getInstance();
    if ("Start".equals(e.getActionCommand())){
        start = aCalendar.getTimeInMillis();
        aJLabel.setText("Stopwatch is running...");
    } else if ("Stop".equals(e.getActionCommand())) {
        aJLabel.setText("Elapsed time is: " + 
                (double) (aCalendar.getTimeInMillis() - start) / 1000 );
    } else if ("Pause".equals(e.getActionCommand())) {
        // Do pause stuff
    }
}

這也意味着,只要它們具有相同的actionCommand ,就可以將ActionListener用於JMenuItem的東西。

話雖如此,我鼓勵你不要遵循這種范例。 通常,我鼓勵您使用Action的API,但是對於您現在所處的位置,這可能有點太先進了,相反,我鼓勵您利用Java的匿名類支持。 ..

startButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        start = aCalendar.getTimeInMillis();
        aJLabel.setText("Stopwatch is running...");
    }
});
stopButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        aJLabel.setText("Elapsed time is: "
                + (double) (aCalendar.getTimeInMillis() - start) / 1000);
    }
});
pauseButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Do pause stuff
    }
});

這將每個按鈕的職責隔離到單個ActionListener ,這使查看事情的進展以及在需要時更輕松地進行修改而無需擔心或影響其他按鈕。

它也不需要維護對按鈕的引用(因為可以通過ActionEvent getSource屬性獲得它)

如果您不想實現ActionListener,則可以向按鈕添加匿名偵聽器,如下所示:

 JButton startButton = new JButton("Start");
 JButton stopButton = new JButton("Stop");
 JButton pauseButton = new JButton("Pause");
 startButton.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //start action logic here
        }
    });
 stopButton.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //stop action logic here
        }
    });
 pauseButton.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            //action logic here
        }
    });

這個解決方案必須工作:)

創建按鈕之后,您需要添加的就是這個。

startButton.setActionCommand("Start");
stopButton.setActionCommand("Stop");
pauseButton.setActionCommand("Pause");

在actionPerformed方法中使用它。

switch(actionEvent.getActionCommand())
{
// cases
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM