?? msgbox.java
字號:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
import java.util.StringTokenizer;
/** This class extends JDialog to make a Message Box having two modes namely 0 and 1,
* in mode 0 the text is single line and center aligned while in mode 1 the text is
* multiline and left aligned.
*/
class MsgBox extends JDialog{
FontMetrics fm;
int minWidth,maxWidth,strWidth,screenWidth,screenHeight;
int dialogWidth,dialogHeight,mode;
MsgBox(Frame f,String cap,int mode){
super(f,"Typing Tutor",true);
this.mode=mode;
Font font=Utilities.getDialogFont();
fm=getFontMetrics(font);
Dimension screen=getToolkit().getScreenSize();
screenWidth=screen.width;
screenHeight=screen.height;
minWidth=screenWidth/8;
maxWidth=screenWidth/2;
if(mode==0){
int count=1;
strWidth=fm.stringWidth(cap);
while(strWidth>maxWidth){
strWidth=strWidth-maxWidth;
count++;
}
int l=cap.length()/count;
int start=0,end=l;
String temp[]=new String[count];
for(int i=0;i<count;i++){
temp[i]=cap.substring(start,end);
start=end;
end=end+l;
}
displayText(temp);
}
else{
int i=0;
StringTokenizer st=new StringTokenizer(cap,"$");
String temp[]=new String[st.countTokens()];
while(st.hasMoreTokens())
temp[i++]=" "+st.nextToken();
displayText(temp);
}
setVisible(true);
}
private void displayText(String text[]){
getContentPane().setLayout(new GridLayout(text.length+1,0));
for(int i=0;i<text.length;i++)
if(mode==0)
getContentPane().add(new DialogLabel(text[i],SwingUtilities.CENTER));
else
getContentPane().add(new DialogLabel(text[i],SwingUtilities.LEFT));
JPanel p=new JPanel();
JButton ok=new JButton("Ok");
p.add(ok);
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
getContentPane().add(p);
strWidth=0;
for(int i=0;i<text.length;i++)
if(fm.stringWidth(text[i])>strWidth)
strWidth=fm.stringWidth(text[i]);
dialogWidth=minWidth+strWidth+mode*40;
dialogHeight=screenHeight/6+(text.length-1)*fm.getHeight()*2;
setBounds((screenWidth-dialogWidth)/2,(screenHeight-dialogHeight)/2,
dialogWidth,dialogHeight);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -