guioutput.java
来自「国外的数据结构与算法分析用书」· Java 代码 · 共 58 行
JAVA
58 行
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/** This class is used to display a small window that has a specific title and contents.
When new text is to be displayed in the window, the normal execution of the calling program
is halted until the button on the interface is clicked. */
public class GuiOutput extends JFrame implements ActionListener
{
JTextArea textArea;
JButton continueBtn;
/** The constructor that is used to initialize the graphical components, and place them
in the correct locations on the interface. */
public GuiOutput()
{
textArea = new JTextArea(8, 30);
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panel = new JPanel();
continueBtn = new JButton("Continue Executing Program");
continueBtn.addActionListener(this);
setResizable(false); // window is a fixed size
textArea.setEditable(false); // can't edit output area
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(continueBtn);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.SOUTH);
setLocation(150, 150); // display window to the right and down
setSize(328, 220);
}
/** Used to display messages on the output window. This method is synchronized so normal
flow of the application is suspended until the button on the interface is clicked. */
public synchronized void displayOutput(String output, String windowTitle)
{
setTitle(windowTitle);
textArea.setText(output);
setVisible(true); // display window
try
{
wait();
} catch (InterruptedException e) {} // do nothing
}
/** This method is called when the button is clicked. It causes the normal flow of
execution for the program to resume. */
public synchronized void actionPerformed(ActionEvent ae)
{
notify(); // resume execution
setVisible(false); // hide window
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?