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

<< 返回
  

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值