用Java编写程序,界面里有三个窗口,三个窗口一共只能输出五十次,同一个窗口输出时间间隔大于一秒,在还剩输出六次时停止输出,要求输出的每一次都要有输出的时刻和输出窗口和输出次数
时间: 2024-03-15 09:47:15 浏览: 53
好的,以下是使用Java Swing编写的程序示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreeWindowsDemo extends JFrame implements ActionListener {
private JTextArea window1TextArea;
private JTextArea window2TextArea;
private JTextArea window3TextArea;
private static final int TOTAL_OUTPUT_COUNT = 50;
private static final int STOP_OUTPUT_COUNT = 6;
private int currentOutputCount = 0;
private ScheduledExecutorService executorService;
public ThreeWindowsDemo() {
super("Three Windows Demo");
// Create text areas for three windows
window1TextArea = new JTextArea();
window2TextArea = new JTextArea();
window3TextArea = new JTextArea();
// Create buttons to trigger output in each window
JButton window1Button = new JButton("Window 1");
JButton window2Button = new JButton("Window 2");
JButton window3Button = new JButton("Window 3");
// Add action listeners to buttons
window1Button.addActionListener(this);
window2Button.addActionListener(this);
window3Button.addActionListener(this);
// Create panels to hold text areas and buttons
JPanel window1Panel = new JPanel(new BorderLayout());
window1Panel.setBorder(BorderFactory.createTitledBorder("Window 1"));
window1Panel.add(new JScrollPane(window1TextArea), BorderLayout.CENTER);
window1Panel.add(window1Button, BorderLayout.SOUTH);
JPanel window2Panel = new JPanel(new BorderLayout());
window2Panel.setBorder(BorderFactory.createTitledBorder("Window 2"));
window2Panel.add(new JScrollPane(window2TextArea), BorderLayout.CENTER);
window2Panel.add(window2Button, BorderLayout.SOUTH);
JPanel window3Panel = new JPanel(new BorderLayout());
window3Panel.setBorder(BorderFactory.createTitledBorder("Window 3"));
window3Panel.add(new JScrollPane(window3TextArea), BorderLayout.CENTER);
window3Panel.add(window3Button, BorderLayout.SOUTH);
// Add panels to main frame
JPanel mainPanel = new JPanel(new GridLayout(1, 3));
mainPanel.add(window1Panel);
mainPanel.add(window2Panel);
mainPanel.add(window3Panel);
getContentPane().add(mainPanel);
// Set up executor service to run output tasks
executorService = Executors.newScheduledThreadPool(3);
}
@Override
public void actionPerformed(ActionEvent e) {
currentOutputCount++;
// Check if we have reached the total output count
if (currentOutputCount > TOTAL_OUTPUT_COUNT) {
// Stop executor service
executorService.shutdown();
return;
}
// Get current time
Date currentTime = new Date();
// Determine which window triggered the output
JTextArea textArea = null;
if (e.getActionCommand().equals("Window 1")) {
textArea = window1TextArea;
} else if (e.getActionCommand().equals("Window 2")) {
textArea = window2TextArea;
} else if (e.getActionCommand().equals("Window 3")) {
textArea = window3TextArea;
}
// Schedule output task with executor service
executorService.schedule(new OutputTask(textArea, currentTime, currentOutputCount), 0, TimeUnit.MILLISECONDS);
}
private class OutputTask implements Runnable {
private JTextArea textArea;
private Date outputTime;
private int outputCount;
public OutputTask(JTextArea textArea, Date outputTime, int outputCount) {
this.textArea = textArea;
this.outputTime = outputTime;
this.outputCount = outputCount;
}
@Override
public void run() {
// Check if we have reached the stop output count for this window
if (outputCount > TOTAL_OUTPUT_COUNT - STOP_OUTPUT_COUNT) {
return;
}
// Output to text area
textArea.append(outputTime + " - Output " + outputCount + "\n");
// Schedule next output task with executor service
executorService.schedule(new OutputTask(textArea, new Date(), outputCount + 1), 1000, TimeUnit.MILLISECONDS);
}
}
public static void main(String[] args) {
ThreeWindowsDemo demo = new ThreeWindowsDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setVisible(true);
}
}
```
这个程序创建了三个文本区域和三个按钮,点击按钮可以在相应的文本区域中输出时间、窗口和输出次数。程序使用了Java的ScheduledExecutorService来调度每次输出任务,并且在输出次数达到一定阈值时停止输出。
阅读全文