📄 sortinganimationcontrollermanager.java
字号:
package animationController;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SortingAnimationControllerManager extends AnimationControllerManager {
public static SortingAnimationController controller = new SortingAnimationController();
public static void changeValuesByUser(JFrame parent) {
SortingAnimationControllerChanger changer = new SortingAnimationControllerChanger();
changer.changeValuesByUser(parent);
}
public static SortingAnimationController createSortingAnimationController() {
SortingAnimationController localController = new SortingAnimationController();
localController.stepDelay = AnimationControllerManager.controller.stepDelay;
localController.animationDelay = AnimationControllerManager.controller.animationDelay;
localController.unitMoving = AnimationControllerManager.controller.unitMoving;
localController.dataSize = controller.dataSize;
localController.maxDataValue = controller.maxDataValue;
localController.setDefaultValue();
return localController;
}
}
class SortingAnimationControllerChanger {
private JDialog place;
private JTextField dataSizeField;
private JTextField maxDataValueField;
public void changeValuesByUser(JFrame parent) {
JPanel basicPanel = new JPanel();
basicPanel.setLayout(new GridLayout(2, 1, 0, 15));
basicPanel.setBorder(BorderFactory.createTitledBorder(""));
JPanel tempPanel = new JPanel();
tempPanel.setLayout(new GridLayout(1, 2, 0, 0));
JLabel promptLabel = new JLabel("Element size for sorting: ", JLabel.LEFT);
dataSizeField = new JTextField(6);
dataSizeField.setText(SortingAnimationControllerManager.controller.dataSize+"");
promptLabel.setLabelFor(dataSizeField);
tempPanel.add(promptLabel);
tempPanel.add(dataSizeField);
basicPanel.add(tempPanel);
tempPanel = new JPanel();
tempPanel.setLayout(new GridLayout(1, 2, 0, 0));
promptLabel = new JLabel("Max value of element for sorting: ", JLabel.LEFT);
maxDataValueField = new JTextField(6);
maxDataValueField.setText(SortingAnimationControllerManager.controller.maxDataValue+"");
promptLabel.setLabelFor(maxDataValueField);
tempPanel.add(promptLabel);
tempPanel.add(maxDataValueField);
basicPanel.add(tempPanel);
// 创建一事件监听器,该监听器监听下述两个按钮上发生的事件
SimpleListener listener = new SimpleListener();
JButton okButton = new JButton("Ok");
okButton.setMnemonic('O'); // 设置快捷键
okButton.setActionCommand("ok");
okButton.addActionListener(listener); // 添加事件监听器
JButton cancelButton = new JButton("Cancel");
cancelButton.setMnemonic('C'); // 设置快捷键
cancelButton.setActionCommand("cancel");
cancelButton.addActionListener(listener); // 添加事件监听器
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 0));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
place = new JDialog(parent, "Set control parameters for sorting animation", true);
place.setLayout(new BorderLayout());
place.add(basicPanel, BorderLayout.NORTH); // 放在左边
place.add(buttonPanel, BorderLayout.SOUTH);
place.pack();
place.setLocation((parent.getWidth()-place.getWidth())/2, (parent.getHeight()-place.getHeight())/2);
place.setVisible(true);
}
// 监听按钮的事件
private class SimpleListener implements java.awt.event.ActionListener {
// 事件监听程序
public void actionPerformed(java.awt.event.ActionEvent evt) {
String command = evt.getActionCommand();
if (command.equals("ok")) {
String text = dataSizeField.getText();
SortingAnimationControllerManager.controller.dataSize = Integer.valueOf(text);
text = maxDataValueField.getText();
SortingAnimationControllerManager.controller.maxDataValue = Integer.valueOf(text);
SortingAnimationControllerManager.controller.setDefaultValue();
}
place.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -