📄 lessondialog.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
/** This class shows a lesson dialog that allows the user to customize the lesson in terms of
* colors, typing font, typing style and background music. */
class LessonDialog extends JDialog implements ActionListener{
CustomizationPanel panel;
MainScreen mainScreen;
private int x;
private boolean flag;
Frame f;
LessonDialog(Frame f){
super(f,Utilities.getName(),true);
this.f=f;
f.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Dimension d=getToolkit().getScreenSize();
int w=495;
int h=405;
setBounds((d.width-w)/2,(d.height-h)/2,w,h);
Container content=getContentPane();
Quote quote=new Quote();
ImagePanel iPanel=new ImagePanel(selectImage(),quote.getQuote());
content.add(iPanel,BorderLayout.NORTH);
mainScreen=(MainScreen)f;
UserData user=mainScreen.getCurrentUser();
content.add(panel=new CustomizationPanel(user));
JPanel temp=new JPanel();
JButton ok,cancel;
temp.add(ok=new JButton("Ok"));
temp.add(cancel=new JButton("Cancel"));
ok.addActionListener(this);
cancel.addActionListener(this);
content.add(temp,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent a){
String s=a.getActionCommand();
if(s.equals("Ok")){
x=panel.getLessonDuration();
getValues();
flag=true;
}
setVisible(false);
}
public boolean showDialog(){
setVisible(true);
return flag;
}
private void getValues(){
TempInfo info=mainScreen.getTempInfo();
info.updateSettings(panel.getBackgroundColor(),panel.getLessonColor(),
panel.getTypingColor(),panel.getErrorColor(),
panel.getTypingFontSize(),panel.getBackgroundMusic(),
panel.getErrorSound(),panel.getTypingStyle());
}
public int getLessonDuration(){
return x;
}
public void paint(Graphics g){
super.paint(g);
f.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public ImageIcon selectImage(){
String path=System.getProperty("user.dir");
File f=new File(path+"/Images");
String s[]=f.list(new FilenameFilter(){
public boolean accept(File f,String s){
return s.startsWith("BACK");
}
});
int r=(int)(Math.random()*s.length-1);
return new ImageIcon(path+"/Images/"+s[r]);
}
}
class CustomizationPanel extends JPanel{
ColorChooser background,lessonColor,typingColor,errorColor;
TimeSelect time;
ValueSelect fontSize,bSound,errorSound,typingStyle;
CustomizationPanel(UserData user){
super(new GridLayout(1,2));
background=new ColorChooser("Background:",user.getBackground());
lessonColor=new ColorChooser("Lesson Color:",user.getLessonColor());
typingColor=new ColorChooser("Typing Color:",user.getTypingColor());
errorColor=new ColorChooser("Error Color:",user.getErrorColor());
time=new TimeSelect("Lesson Duration:",100);
String size[]={"10","12","14","16","18"};
fontSize=new ValueSelect("Font Size:",size);
fontSize.setSelectedItem(""+user.getTypingFontSize());
String style[]={"Classic","Highlight Character","Moving Text"};
typingStyle=new ValueSelect("Typing Style:",style);
typingStyle.setSelectedItem(user.getTypingStyle());
SoundFiles sFiles=new SoundFiles("Music");
String strBSound[]=sFiles.getSoundFiles();
bSound=new ValueSelect("Background Music:",strBSound);
bSound.addItem("None");
bSound.setSelectedItem(user.getBackgroundMusic());
sFiles=new SoundFiles("Error");
String strError[]=sFiles.getSoundFiles();
errorSound=new ValueSelect("Error Sound:",strError);
errorSound.addItem("None");
errorSound.setSelectedItem(user.getErrorSound());
Box box=Box.createVerticalBox();
box.add(time);
box.add(typingStyle);
box.add(bSound);
box.add(errorSound);
JPanel p1=new JPanel(new GridLayout(5,0));
JPanel p2=new JPanel();
p1.setBorder(new TitledBorder(new EtchedBorder(),"Lesson Colors"));
p2.setBorder(new TitledBorder(new EtchedBorder(),"Other Information"));
p1.add(background);
p1.add(lessonColor);
p1.add(typingColor);
p1.add(errorColor);
p1.add(fontSize);
p2.add(box);
add(p1);
add(p2);
}
public Color getBackgroundColor(){
return background.getColor();
}
public Color getTypingColor(){
return typingColor.getColor();
}
public Color getLessonColor(){
return lessonColor.getColor();
}
public Color getErrorColor(){
return errorColor.getColor();
}
public int getTypingFontSize(){
int size=14;
try{
size=Integer.parseInt(fontSize.getValue());
}catch(NumberFormatException nfe){return 14;}
return size;
}
public int getLessonDuration(){
return time.getTime();
}
public String getBackgroundMusic(){
return bSound.getValue();
}
public String getErrorSound(){
return errorSound.getValue();
}
public String getTypingStyle(){
return typingStyle.getValue();
}
}
class SoundFiles implements FilenameFilter{
private String token;
SoundFiles(String token){
this.token=token;
}
public String[] getSoundFiles(){
String s=System.getProperty("user.dir");
File f=new File(s+"/Sounds");
String files[]=f.list(this);
return files;
}
public boolean accept(File f,String s){
if(!s.startsWith(token)) return false;
return s.endsWith(".mid")||s.endsWith(".wav")||s.endsWith(".au");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -