📄 interactortest.java
字号:
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import acm.graphics.*;
import acm.program.*;
public class InteractorTest extends GraphicsProgram{
public void init(){
initTextField();
initAddButton();
initRemoveButton();
initClearButton();
addMouseListeners();
addActionListeners();
text.addActionListener(this);
}
private void initClearButton() {
clear = new JButton("Clear");
add(clear, SOUTH);
}
private void initRemoveButton() {
remove = new JButton("Remove");
add(remove, SOUTH);
}
private void initAddButton() {
add = new JButton("Add");
add(add, SOUTH);
}
private void initTextField() {
text = new JTextField(40);
add(new JLabel("Name "), SOUTH);
add(text, SOUTH);
text.setActionCommand("Add");
}
private void addGBox(String str, double height){
GBox rect = new GBox(str);
rects.put(str, rect);
add(rect,(getWidth()- rect.getWidth())/2, (getHeight() - rect.getHeight())/2 + height);
}
private void removeGBox(){
String GBoxName = text.getText();
for(String name: rects.keySet()){
if(name.equals(GBoxName)){
remove(rects.get(name));
}
}
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("Clear")){
removeAll();
height = 0;
}
else if(cmd.equals("Add")){
addGBox(text.getText(), height);
height = height + GBox.BOX_HEIGHT;
}
else if (cmd.equals("Remove")){
removeGBox();
}
}
public void mouseDragged(MouseEvent e){
}
private GBox getGBox(int x, int y){
if(getElementAt(x, y) != null){
GBox box = (GBox)getElementAt(x, y);
return box;
}
return null;
}
private Map<String, GBox> rects = new HashMap<String, GBox>();
private JTextField text;
private JButton add;
private JButton clear;
private JButton remove;
private double height;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -