<< 返回
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GoDialog extends JDialog {
private JDialog myself = null;
private Notepad notepad = null;
private JLabel label = null;
private JTextField text = null;
private JButton go = null;
private JButton cancel = null;
public GoDialog (Notepad notepad) {
super(notepad.getFrame(), "转到指定行");
this.notepad = notepad;
this.myself = this;
this.label = new JLabel("行号:");
this.label.setSize(200, 25);
this.label.setLocation(10, 5);
this.label.setFont(new Font("宋体", 0, 12));
this.text = new JTextField();
this.text.setSize(215, 25);
this.text.setLocation(10, 30);
this.go = new JButton("转到");
this.go.setSize(80, 25);
this.go.setLocation(60, 60);
this.cancel = new JButton("取消");
this.cancel.setSize(80, 25);
this.cancel.setLocation(145, 60);
this.go.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setPosition(text.getText());
}
});
this.cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
myself.dispose();
}
});
this.text.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
setPosition(text.getText());
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
setPosition(text.getText());
}
}
});
this.setLayout(null);
this.add(this.label);
this.add(this.text);
this.add(this.go);
this.add(this.cancel);
this.setSize(250, 130);
this.setLocation(400,300);
this.setResizable(false);
this.setVisible(true);
}
private void setPosition(String row) {
this.setVisible(false);
int result = 0;
result = Integer.valueOf(row);
if(result > this.notepad.getTextArea().getLineCount() || result <= 0) {
result = this.notepad.getTextArea().getLineCount();
}
System.out.println("转换之后的数字是:" + result);
this.notepad.setPosition(result);
}
}
<< 返回