BuildListener

时间:2013-11-11 04:59:26
【文件属性】:

文件名称:BuildListener

文件大小:9KB

文件格式:TXT

更新时间:2013-11-11 04:59:26

BuildListener

import java.util.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class Jiy{ private JFrame frame; public static void main(String[] args){ Jiy jy=new Jiy(); jy.go(); } public void go(){ JFrame frame1=new JFrame("主窗体"); JMenuBar bar=new JMenuBar(); JMenu menu=new JMenu("File"); JMenuItem build=new JMenuItem("builder"); JMenuItem play=new JMenuItem("player"); build.addActionListener(new BuildListener()); play.addActionListener(new PlayListener()); menu.add(build); menu.add(play); bar.add(menu); // frame1.setJMenuBar(bar); // frame1.setMenuBar(bar); frame1.setJMenuBar(bar); frame1.setSize(500,600); frame1.setVisible(true); } /* public class BuildListener imlements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCardBuilder builder=new QuizCardBuilder(); builder.go(); } }*/ public class BuildListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCardBuilder build=new QuizCardBuilder(); build.go(); } } /* public class PlayListener imlements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCardPlayer player=new QuizCardPlayer(); player.go(); } }*/ public class PlayListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCardPlayer player=new QuizCardPlayer(); player.go(); } } } public class QuizCard { String question; String answer; public QuizCard(String q,String a){ question=q; question=a; } public String getQuestion(){ return question; } public String getAnswer(){ return answer; } } import java.util.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class QuizCardBuilder{ private JTextArea question; private JTextArea answer; private ArrayList cardList; private JFrame frame; public void go(){ frame=new JFrame("Quiz Card Build"); JPanel mainPanel=new JPanel(); Font bigFont=new Font("sanserif",Font.BOLD,24); question=new JTextArea(6,20); question.setLineWrap(true); question.setWrapStyleWord(true); question.setFont(bigFont); JScrollPane qScroller=new JScrollPane(question); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); answer=new JTextArea(6,20); answer.setLineWrap(true); answer.setWrapStyleWord(true); answer.setFont(bigFont); JScrollPane aScroller=new JScrollPane(answer); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JButton nextButton=new JButton("Next Card"); cardList=new ArrayList(); JLabel qLabel=new JLabel("Question:"); JLabel aLabel=new JLabel("Answer"); mainPanel.add(qLabel); mainPanel.add(qScroller); mainPanel.add(aLabel); mainPanel.add(aScroller); mainPanel.add(nextButton); nextButton.addActionListener(new NextCardListener()); JMenuBar menuBar=new JMenuBar(); JMenu fileMenu=new JMenu("File"); JMenuItem newMenuItem=new JMenuItem("new"); JMenuItem saveMenuItem=new JMenuItem("save"); newMenuItem.addActionListener(new NewMenuListener()); saveMenuItem.addActionListener(new SaveMenuListener()); fileMenu.add(newMenuItem); fileMenu.add(saveMenuItem); menuBar.add(fileMenu); frame.setJMenuBar(menuBar); frame.getContentPane().add(BorderLayout.CENTER,mainPanel); frame.setSize(500,600); frame.setVisible(true); } public class NextCardListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCard card=new QuizCard(question.getText(),answer.getText()); cardList.add(card); clearCard(); } } public class SaveMenuListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ QuizCard card=new QuizCard(question.getText(),answer.getText()); cardList.add(card); JFileChooser fileSave=new JFileChooser(); fileSave.showSaveDialog(frame); saveFile(fileSave.getSelectedFile()); } } public class NewMenuListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ cardList.clear(); clearCard(); } } private void clearCard(){ question.setText(" "); answer.setText(" "); question.requestFocus(); } private void saveFile(File file){ try{ BufferedWriter writer=new BufferedWriter(new FileWriter(file)); for(QuizCard card:cardList){ writer.write(card.getQuestion()+"/"); writer.write(card.getAnswer()+"\n"); } writer.close(); }catch(IOException ex){ System.out.println("couldn't write the cardList out"); ex.printStackTrace(); } } } import java.util.*; import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class QuizCardPlayer{ private JTextArea display; private ArrayList cardList; private QuizCard currentCard; private int currentCardIndex; private JFrame frame; private JButton nextButton; private boolean isShowAnswer; public void go(){ frame=new JFrame("Quiz Card Player"); JPanel mainPanel=new JPanel(); Font bigFont=new Font("sanserif",Font.BOLD,24); display=new JTextArea(10,20); display.setFont(bigFont); display.setLineWrap(true); display.setEditable(false); JScrollPane qScroller=new JScrollPane(display); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); nextButton=new JButton("Show Question"); mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS)); mainPanel.add(qScroller); mainPanel.add(nextButton); nextButton.addActionListener(new NextCardListener()); JMenuBar menuBar=new JMenuBar(); JMenu fileMenu=new JMenu("File"); JMenuItem loadMenuItem=new JMenuItem("Load card set"); loadMenuItem.addActionListener(new OpenMenuListener()); fileMenu.add(loadMenuItem); menuBar.add(fileMenu); frame.setJMenuBar(menuBar); frame.getContentPane().add(BorderLayout.CENTER,mainPanel); frame.setSize(640,500); frame.setVisible(true); } public class NextCardListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ if(isShowAnswer){ display.setText(currentCard.getAnswer()); nextButton.setText("Next Card"); isShowAnswer=false; }else{ if(currentCardIndex(); try{ BufferedReader reader=new BufferedReader(new FileReader(file)); String line=null; while((line=reader.readLine())!=null){ makeCard(line); } reader.close(); }catch(Exception ex){ System.out.println("couldn't read he caef file"); ex.printStackTrace(); } } private void makeCard(String lineToParse){ String[] result=lineToParse.split("/"); QuizCard card=new QuizCard(result[0],result[1]); cardList.add(card); System.out.println("make a card"); } private void showNextCard(){ currentCard=cardList.get(currentCardIndex); currentCardIndex++; display.setText(currentCard.getQuestion()); nextButton.setText("Show Answer"); isShowAnswer=true; } }


网友评论