在 类 中使用方法?
Using methods across classes?
我有两个 classes,一个是在特定时间打印命令的计时器,另一个是 GUI,其中包括用于所述计时器的启动按钮。
我正在尝试让我的 GUI 中的 start/stop 按钮能够使用 timer.start();和 timer.stop(); TimeKeeper class 中使用的方法。
我在整个网站上进行了搜索并阅读了许多 Oracle 文档,但仍不清楚这对我的情况如何起作用。
这是完整的计时器class:
package tests;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TimeKeeper extends JFrame
{
private Timer timer;
private int delay = 1000; // every 1 second
private static final long serialVersionUID = 1L;
private int counter = 0;
private int[] times = {};
private String[] commands = {};
public TimeKeeper()
{
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
System.out.println(counter);
counter = counter+1;
if (counter == times[0]) {
new SayText();
SayText.say(commands[0]);
}
if (counter == times[1]){
SayText.say(commands[1]);
}
else
{
timer.stop();
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start(); //MOVE THIS TO START BUTTON IN OTHER CLASS
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new TimeKeeper();
}
});
}
}
这是 GUI 的简短版本 class
//package name
//imports
public class TwoPlayer {
//variable initializations
public TwoPlayer(){
//mainFrame specs
//Jlabel
//Some JFields
JButton button1 = new JButton("Start/Stop");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//Another button
//Another button
//Another button
//JPanel creation
//add components to mainframe
}
}
为了保持禁用状态,我至少要有一个带有停止和启动方法的接口,在 Timekeeper class 上实现接口,实现独立方法来执行 timer.start ()/timer.start() 然后将 Timekeeper 实例作为接口传递到 GUI 中。相当简单,真的,但是 GUI class 可以接受任何你想要实现 start/stop 功能的东西。
使用方法的基本方法是创建一个 class 的实例,使用像
这样的语句
Timekeeper timekeeper = new Timekeeper();
在某些 class 中(例如 TwoPlayer
)。然后,在该变量的范围内,您可以调用方法 "on" 您创建的实例,例如
// ...
timekeeper.start();
// ... other code in here
timekeeper.stop();
此外,由于我看到您已经在计时器中实现了 ActionListener class,您可以将计时器实例传递给按钮的 setActionListener()
方法。
JButton button = new JButton("start timer");
button.setActionListener(timekeeper);
然后单击该按钮将调用该 Timekeeper 实例上的 actionPerformed()
方法。
请注意计时器 class,因为它实现了 ActionListener,因此部分是 GUI class。这只是意味着您只能在需要使用 Swing Action 进行操作的地方使用 class。
我会说你的设计缺少一点点,哦,好吧,设计。
我们先退一步,TwoPlayer
class有什么权利修改TimeKeeper
class?实际上,none,这不是它的责任。目前,它只想启动和停止计时器。它不在乎如何
同样,TimeKeeper
class 关心的是管理 Timer
。
这是一个很好的例子,说明为什么你不应该从 JFrame
扩展,它把你锁定在一个单一的用例中,这使得几乎不可能扩展或扩展功能。
那么,答案是什么?好吧,让我们退后一步,看看您如何重新设计这个...
TimeKeeper
负责管理Timer
,为此,我们需要为其他class提供启动和停止这个Timer
的能力(它可能需要其他功能,但我坚持使用基础知识)。此外,它应该从更灵活的东西扩展,也许 JPanel
,这将使您在需要时更容易重用和扩展。
public class TimeKeeper extends JPanel {
private Timer timer;
private int delay = 1000; // every 1 second
private int counter = 0;
private int[] times = {};
private String[] commands = {};
public TimeKeeper() {
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println(counter);
counter = counter + 1;
if (counter == times[0]) {
//new SayText();
//SayText.say(commands[0]);
}
if (counter == times[1]) {
//SayText.say(commands[1]);
} else {
timer.stop();
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
}
现在,我们需要让玩家与 TimeKeeper
互动,为此,我们需要一个 start/stop 按钮。您可以使用 JToggleButton
或以其他方式管理单个按钮的状态,但为简单起见,我使用了两个...
public static class ControlsPane extends JPanel {
public static final String START_COMMAND = "Start";
public static final String STOP_COMMAND = "Stop";
private JButton start;
private JButton stop;
public ControlsPane() {
start = new JButton(START_COMMAND);
stop = new JButton(STOP_COMMAND);
setLayout(new GridBagLayout());
add(start);
add(stop);
}
public void addActionListener(ActionListener listener) {
start.addActionListener(listener);
stop.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
start.removeActionListener(listener);
stop.removeActionListener(listener);
}
}
现在,class 所做的只是提供两个按钮(在面板上)和 add/remove 一个 ActionListener
的能力,当一个或另一个按钮被点击时会收到通知.
注意,此 class 没有实际方式可以与 TimeKeeper
交互,它的唯一责任是在按下一个或另一个按钮时生成通知,负责实际执行有事和别人在一起
让我们把它放在一起...
TimeKeeper timeKeeper = new TimeKeeper();
ControlsPane controlsPane = new ControlsPane();
controlsPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case ControlsPane.START_COMMAND:
timeKeeper.start();
break;
case ControlsPane.STOP_COMMAND:
timeKeeper.stop();
break;
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(timeKeeper);
frame.add(controlsPane, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
因此,我们创建了 TimeKeeper
和 ControlsPane
的实例。我们注册一个ActionListener
到ControlsPane
,它根据ControlsPane
产生的事件调用TimeKeeper
的start
或stop
方法然后我们将两个面板添加到屏幕...
这是一个非常松散的例子 Model-View-Controller 和
Observer Pattern
您可能想查看 How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners 了解更多详情
我有两个 classes,一个是在特定时间打印命令的计时器,另一个是 GUI,其中包括用于所述计时器的启动按钮。 我正在尝试让我的 GUI 中的 start/stop 按钮能够使用 timer.start();和 timer.stop(); TimeKeeper class 中使用的方法。
我在整个网站上进行了搜索并阅读了许多 Oracle 文档,但仍不清楚这对我的情况如何起作用。
这是完整的计时器class:
package tests;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TimeKeeper extends JFrame
{
private Timer timer;
private int delay = 1000; // every 1 second
private static final long serialVersionUID = 1L;
private int counter = 0;
private int[] times = {};
private String[] commands = {};
public TimeKeeper()
{
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
System.out.println(counter);
counter = counter+1;
if (counter == times[0]) {
new SayText();
SayText.say(commands[0]);
}
if (counter == times[1]){
SayText.say(commands[1]);
}
else
{
timer.stop();
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start(); //MOVE THIS TO START BUTTON IN OTHER CLASS
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new TimeKeeper();
}
});
}
}
这是 GUI 的简短版本 class
//package name
//imports
public class TwoPlayer {
//variable initializations
public TwoPlayer(){
//mainFrame specs
//Jlabel
//Some JFields
JButton button1 = new JButton("Start/Stop");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//Another button
//Another button
//Another button
//JPanel creation
//add components to mainframe
}
}
为了保持禁用状态,我至少要有一个带有停止和启动方法的接口,在 Timekeeper class 上实现接口,实现独立方法来执行 timer.start ()/timer.start() 然后将 Timekeeper 实例作为接口传递到 GUI 中。相当简单,真的,但是 GUI class 可以接受任何你想要实现 start/stop 功能的东西。
使用方法的基本方法是创建一个 class 的实例,使用像
这样的语句Timekeeper timekeeper = new Timekeeper();
在某些 class 中(例如 TwoPlayer
)。然后,在该变量的范围内,您可以调用方法 "on" 您创建的实例,例如
// ...
timekeeper.start();
// ... other code in here
timekeeper.stop();
此外,由于我看到您已经在计时器中实现了 ActionListener class,您可以将计时器实例传递给按钮的 setActionListener()
方法。
JButton button = new JButton("start timer");
button.setActionListener(timekeeper);
然后单击该按钮将调用该 Timekeeper 实例上的 actionPerformed()
方法。
请注意计时器 class,因为它实现了 ActionListener,因此部分是 GUI class。这只是意味着您只能在需要使用 Swing Action 进行操作的地方使用 class。
我会说你的设计缺少一点点,哦,好吧,设计。
我们先退一步,TwoPlayer
class有什么权利修改TimeKeeper
class?实际上,none,这不是它的责任。目前,它只想启动和停止计时器。它不在乎如何
同样,TimeKeeper
class 关心的是管理 Timer
。
这是一个很好的例子,说明为什么你不应该从 JFrame
扩展,它把你锁定在一个单一的用例中,这使得几乎不可能扩展或扩展功能。
那么,答案是什么?好吧,让我们退后一步,看看您如何重新设计这个...
TimeKeeper
负责管理Timer
,为此,我们需要为其他class提供启动和停止这个Timer
的能力(它可能需要其他功能,但我坚持使用基础知识)。此外,它应该从更灵活的东西扩展,也许 JPanel
,这将使您在需要时更容易重用和扩展。
public class TimeKeeper extends JPanel {
private Timer timer;
private int delay = 1000; // every 1 second
private int counter = 0;
private int[] times = {};
private String[] commands = {};
public TimeKeeper() {
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println(counter);
counter = counter + 1;
if (counter == times[0]) {
//new SayText();
//SayText.say(commands[0]);
}
if (counter == times[1]) {
//SayText.say(commands[1]);
} else {
timer.stop();
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
}
现在,我们需要让玩家与 TimeKeeper
互动,为此,我们需要一个 start/stop 按钮。您可以使用 JToggleButton
或以其他方式管理单个按钮的状态,但为简单起见,我使用了两个...
public static class ControlsPane extends JPanel {
public static final String START_COMMAND = "Start";
public static final String STOP_COMMAND = "Stop";
private JButton start;
private JButton stop;
public ControlsPane() {
start = new JButton(START_COMMAND);
stop = new JButton(STOP_COMMAND);
setLayout(new GridBagLayout());
add(start);
add(stop);
}
public void addActionListener(ActionListener listener) {
start.addActionListener(listener);
stop.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
start.removeActionListener(listener);
stop.removeActionListener(listener);
}
}
现在,class 所做的只是提供两个按钮(在面板上)和 add/remove 一个 ActionListener
的能力,当一个或另一个按钮被点击时会收到通知.
注意,此 class 没有实际方式可以与 TimeKeeper
交互,它的唯一责任是在按下一个或另一个按钮时生成通知,负责实际执行有事和别人在一起
让我们把它放在一起...
TimeKeeper timeKeeper = new TimeKeeper();
ControlsPane controlsPane = new ControlsPane();
controlsPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case ControlsPane.START_COMMAND:
timeKeeper.start();
break;
case ControlsPane.STOP_COMMAND:
timeKeeper.stop();
break;
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(timeKeeper);
frame.add(controlsPane, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
因此,我们创建了 TimeKeeper
和 ControlsPane
的实例。我们注册一个ActionListener
到ControlsPane
,它根据ControlsPane
产生的事件调用TimeKeeper
的start
或stop
方法然后我们将两个面板添加到屏幕...
这是一个非常松散的例子 Model-View-Controller 和 Observer Pattern
您可能想查看 How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners 了解更多详情