关闭

引用页_初学Java:仿写记事本_GoDialog.java

180人阅读 评论(0) 收藏 举报

<< 返回
  

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);
	}
}

  
<< 返回

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6284次
    • 积分:228
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档