記事本轉(zhuǎn)到

package java實踐;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JButton;

import javax.swing.JDialog;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

public class Goto extends JDialog {

private JPanel contentPane;

private JTextField textField;

/**

* Launch the application.

*/

/**

* Create the frame.

*/

public Goto(JTextArea textArea) {

setVisible(true);

setBounds(100, 100, 691, 134);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel label = new JLabel("請輸入行序號:");

label.setBounds(73, 36, 105, 18);

contentPane.add(label);

textField = new JTextField();

textField.setBounds(176, 33, 170, 24);

contentPane.add(textField);

textField.setColumns(10);

JButton button = new JButton("定位到");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int totalLine = textArea.getLineCount();

? ? ? ? ? ? ? ? int[] lineNumber = new int[totalLine + 1];

? ? ? ? ? ? ? ? String s = textArea.getText();

? ? ? ? ? ? ? ? int pos = 1, t = 0;

? ? ? ? ? ? ? ? while (true) {

? ? ? ? ? ? ? ? ? ? pos = s.indexOf('\12', pos);

? ? ? ? ? ? ? ? ? ? if (pos == -1)

? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? lineNumber[t++] = pos++;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? int gt = 1;

? ? ? ? ? ? ? ? gt = Integer.parseInt(textField.getText());

? ? ? ? ? ? ? ? if (gt < 2 || gt >= totalLine) {

? ? ? ? ? ? ? ? ? ? if (gt < 2) {

? ? ? ? ? ? ? ? ? ? ? ? textArea.setCaretPosition(0);

? ? ? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? ? ? textArea.setCaretPosition(s.length());

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }else {

? ? ? ? ? ? ? ? ? ? textArea.setCaretPosition(lineNumber[gt - 2] + 1);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? setVisible(false);

}

});

button.setBounds(360, 32, 113, 27);

contentPane.add(button);

JButton button_1 = new JButton("取消");

button_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

setVisible(false);

}

});

button_1.setBounds(497, 32, 113, 27);

contentPane.add(button_1);

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末浪规,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子轧抗,更是在濱河造成了極大的恐慌遥皂,老刑警劉巖钱反,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件黑竞,死亡現(xiàn)場離奇詭異,居然都是意外死亡锁右,警方通過查閱死者的電腦和手機失受,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咏瑟,“玉大人拂到,你說我怎么就攤上這事÷肱ⅲ” “怎么了兄旬?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長余寥。 經(jīng)常有香客問我领铐,道長,這世上最難降的妖魔是什么宋舷? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任绪撵,我火速辦了婚禮,結(jié)果婚禮上祝蝠,老公的妹妹穿的比我還像新娘莲兢。我一直安慰自己,他們只是感情好续膳,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布改艇。 她就那樣靜靜地躺著,像睡著了一般坟岔。 火紅的嫁衣襯著肌膚如雪谒兄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天社付,我揣著相機與錄音承疲,去河邊找鬼邻耕。 笑死,一個胖子當(dāng)著我的面吹牛燕鸽,可吹牛的內(nèi)容都是我干的兄世。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼啊研,長吁一口氣:“原來是場噩夢啊……” “哼御滩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起党远,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤削解,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后沟娱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體氛驮,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年济似,在試婚紗的時候發(fā)現(xiàn)自己被綠了矫废。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡砰蠢,死狀恐怖磷脯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情娩脾,我是刑警寧澤赵誓,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站柿赊,受9級特大地震影響俩功,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜碰声,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一诡蜓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧胰挑,春花似錦蔓罚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至贡这,卻和暖如春茬末,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工丽惭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留击奶,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓责掏,卻偏偏與公主長得像柜砾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子换衬,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容