java基礎教程案例:[56]java記事本5?

public void run()

{

while(true)

{

String str="";

try {

str=mReplace.jtf1.getText();

if(str.length()>0)

{

mReplace.jb1.setEnabled(true);

mReplace.jb2.setEnabled(true);

mReplace.jb3.setEnabled(true);

}else

{

//JOptionPane.showConfirmDialog(null,"輸入錯誤!","警告",0);

//break;

}

} catch (Exception e) {

// TODO: handle exception

}

}

}

}

class MyFont extends JDialog implements ActionListener,ItemListener,WindowListener

{

public JCheckBox Bold=new JCheckBox("Bold",false);

public JCheckBox Italic=new JCheckBox("Italic",false);

public List Size,Name;

public int FontName;

public int FontStyle;

public int FontSize;

public JButton OK=new JButton("OK");

public JButton Cancel=new JButton("Cancel");

public JTextArea Text=new JTextArea("字體預覽文本域\n0123456789\nAaBbCcXxYyZz");

public MyFont(JFrame owner)

{

super(owner,"字體設置",true);

GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();

String name[]=g.getAvailableFontFamilyNames();

String fontSize[] = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28","30","32","34","36","38","40", "48", "72" };

Name=new List();

Size=new List();

FontName=0;

FontStyle=0;

FontSize=8;

int i=0;

Name.add("Default Value");

for(i=0;i

Name.add(name[i]);

for(i=0;i

Size.add(fontSize[i]);

this.setLayout(null);

this.setBounds(250,200,480, 306);

this.setResizable(false);

OK.setFocusable(false);

Cancel.setFocusable(false);

Bold.setFocusable(false);

Italic.setFocusable(false);

Name.setFocusable(false);

Size.setFocusable(false);

Name.setBounds(10, 10, 212, 259);

this.add(Name);

Bold.setBounds(314, 10, 64, 22);

this.add(Bold);

Italic.setBounds(388, 10, 64, 22);

this.add(Italic);

Size.setBounds(232, 10, 64, 259);

this.add(Size);

Text.setBounds(306, 40, 157, 157);

this.add(Text);

OK.setBounds(306, 243, 74, 26);

this.add(OK);

Cancel.setBounds(390, 243, 74, 26);

this.add(Cancel);

Name.select(FontName);

Size.select(FontSize);

Text.setFont(getFont());

Name.addItemListener(this);

Size.addItemListener(this);

Bold.addItemListener(this);

Italic.addItemListener(this);

OK.addActionListener(this);

Cancel.addActionListener(this);

this.addWindowListener(this);

}

public void itemStateChanged(ItemEvent e) {

Text.setFont(getFont());

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==OK){

FontName=Name.getSelectedIndex();

FontStyle=getStyle();

FontSize=Size.getSelectedIndex();

this.setVisible(false);

}

else cancel();

}

public void windowClosing(WindowEvent e) {

cancel();

}

public Font getFont(){

if(Name.getSelectedIndex()==0) return new Font("新宋體",getStyle(),Size.getSelectedIndex()+8);

else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);

}

public void cancel(){

Name.select(FontName);

Size.select(FontSize);

setStyle();

Text.setFont(getFont());

this.setVisible(false);

}

public void setStyle(){

if(FontStyle==0 FontStyle==2) Bold.setSelected(false);

else Bold.setSelected(true);

if(FontStyle==0 FontStyle==1) Italic.setSelected(false);

else Italic.setSelected(true);

}

public int getStyle(){

int bold=0,italic=0;

if(Bold.isSelected()) bold=1;

if(Italic.isSelected()) italic=1;

return bold+italic*2;

}

public void windowActivated(WindowEvent arg0) {}

public void windowClosed(WindowEvent arg0) {}

public void windowDeactivated(WindowEvent arg0) {}

public void windowDeiconified(WindowEvent arg0) {}

public void windowIconified(WindowEvent arg0) {}

public void windowOpened(WindowEvent arg0) {}

}

class MyPanel extends JPanel implements ActionListener,Runnable

{

public void actionPerformed(ActionEvent e)

{

if(mSearch.jb1==e.getSource())

{

int a = 0, b = 0;

int FindStartPos = pad.jta.getCaretPosition();

String str1, str2, str3, str4, strA, strB;

str1 = pad.jta.getText();

str2 = str1.toLowerCase();

str3 = mSearch.jtf.getText();

str4 = str3.toLowerCase();

//"區分大小寫"的CheckBox被選中

if (mSearch.jcb.isSelected())

{

strA = str1;

strB = str3;

} else

{

strA = str2;

strB = str4;

}

if (mSearch.jrb1.isSelected())

{

if (pad.jta.getSelectedText() == null)

{

a = strA.lastIndexOf(strB, FindStartPos - 1);

} else

{

a = strA.lastIndexOf(strB, FindStartPos - mSearch.jtf.getText().length() - 1);

}

} else if (mSearch.jrb2.isSelected())

{

if (pad.jta.getSelectedText() == null)

{

a = strA.indexOf(strB, FindStartPos);

} else

{

a = strA.indexOf(strB, FindStartPos - mSearch.jtf.getText().length() + 1);

}

}

if (a > -1)

{

if (mSearch.jrb1.isSelected())

{

pad.jta.setCaretPosition(a);

b = mSearch.jtf.getText().length();

pad.jta.select(a, a + b);

} else if (mSearch.jrb2.isSelected())

{

pad.jta.setCaretPosition(a);

b = mSearch.jtf.getText().length();

pad.jta.select(a, a + b);

}

} else

{

JOptionPane.showMessageDialog(null, "查找完畢!", "記事本", JOptionPane.INFORMATION_MESSAGE);

}

}

if(mSearch.jb2==e.getSource())

{

mSearch.dispose();

}

//if(mSearch.jb1==e.getSource()&&(!mSearch.jrb1.isSelected() mSearch.jrb2.isSelected())&&mSearch.jcb.isSelected())

//{

//String string=pad.jta.getText();

//String text=mSearch.jtf.getText();

//int s=string.indexOf(text,start);

//if(string.indexOf(text,start)!=-1)

//{

//pad.jta.setSelectionStart(s);

//pad.jta.setSelectionEnd(s+text.length());

//pad.jta.setSelectedTextColor(Color.blue);

//start=s+1;

//}else

//{

// JOptionPane.showMessageDialog(null, "查找完畢!", "提示", JOptionPane.WARNING_MESSAGE );

//

//}

//}

//if(j>-1)

//{

// String string=pad.jta.getText();

// String text=mSearch.jtf.getText();

// if(mSearch.jrb1.isSelected()&&mSearch.jb1==e.getSource()&&mSearch.jcb.isSelected())

// {

// //向上

// if(string.indexOf(text,start)!=-1&&counter<=j-1)

//{

//int s=string.indexOf(text,start);

//pad.jta.setSelectionStart(s);

//pad.jta.setSelectionEnd(s+text.length());

//pad.jta.setSelectedTextColor(Color.blue);

//start=s+1;

//counter=pad.jta.getCaretPosition();

//System.out.println(counter);

//}else

//{

// JOptionPane.showMessageDialog(null, "查找完畢!", "提示", 0 );

//}

// }

// if(mSearch.jrb2.isSelected()&&mSearch.jb1==e.getSource()&&mSearch.jcb.isSelected())

// {

////向下

// int s=string.indexOf(text,j);

// if(string.indexOf(text,start)!=-1)

// {

// pad.jta.setSelectionStart(s);

//pad.jta.setSelectionEnd(s+text.length());

//pad.jta.setSelectedTextColor(Color.blue);

//j=s+1;

// }else

//{

// JOptionPane.showMessageDialog(null, "查找完畢!", "提示", 0 );

//}

// }

//}

if(e.getActionCommand().equals("clip"))

{

String string=jtf.getText();

int m=Integer.valueOf(string);

if(m<=pad.jta.getRows()+1)

{

try

{

int f=pad.jta.getLineStartOffset(0);

int c=pad.jta.getLineStartOffset(m-1);

int d=Math.abs(c-f);

pad.jta.setCaretPosition(d);

jf.dispose();

} catch (BadLocationException e1)

{

e1.printStackTrace();

}

}else

{

int ption=JOptionPane.showConfirmDialog(null,"對不起,文本行較小,輸入溢出,是否退出!","Over",JOptionPane.WARNING_MESSAGE);

if(ption==JOptionPane.YES_OPTION)

{

jf.dispose();

}

}

}

if(e.getActionCommand().equals("cancel"))

{

jf.dispose();

}

}

public void run()

{

while(true)

{

String str="";

try {

str=mSearch.jtf.getText();

if(str.length()>0)

{

mSearch.jb1.setEnabled(true);

}else

{

//JOptionPane.showConfirmDialog(null,"輸入錯誤!","警告",0);

//break;

}

} catch (Exception e) {

// TODO: handle exception

}

}

}

}

class MyTime extends JPanel implements Runnable

{

private int hour,minute,second,line,row;

String str1="",str2="",str3="",str4="";

public void paint(Graphics g)

{

Date date=new Date();

this.hour=date.getHours();

this.minute=date.getMinutes();

this.second=date.getSeconds();

this.line=getlineNumber();

this.row=getColumnNumber();

if(minute>=0&&minute<=9&&second>=0&&second<=9)

{

str1=hour+" 時 0"+minute+" 分 0"+second+" 秒 ";

}else if(minute>=0&&minute<=9&&second>9)

{

str1=hour+" 時 0"+minute+" 分 "+second+" 秒 ";

}else if(second>=0&&second<=9&&minute>9)

{

str1=hour+" 時 "+minute+" 分 0"+second+" 秒 ";

}else if(second>9&&minute>9)

{

str1=hour+" 時 "+minute+" 分 "+second+" 秒 ";

}

str2="第"+line+"行";

str3="第"+row+"列";

str4="按F1獲得幫助";

super.paint(g);

g.setFont(new Font("宋體",Font.BOLD,12));

g.drawString(str1,0,12);

g.drawString(str2+" "+str3, 300,12);

g.drawString(str4,150, 12);

}

public void run()

{

while(true)

{

// TODO Auto-generated method stub

try {

Thread.sleep(1000);

} catch (Exception e)

{

// TODO: handle exception

}

Date date=new Date();

this.hour=date.getHours();

this.minute=date.getMinutes();

this.second=date.getSeconds();

this.line=getlineNumber();

this.row=getColumnNumber();

str1=" 現在時間為:"+hour+" 時 "+minute+" 分 "+second+" 秒 ";

str2="第"+line+"行";

str3="第"+row+"列";

this.repaint();

}

}

}

}

java基礎教程案例 (共54篇) 上一篇:java記事本代碼4 下一篇:java繪圖代碼1

案例, 基礎教程, 記事本,
相關問題答案