タイトルの通りです。
このクラスは、プレイヤーに練習メニューを選んでもらい、練習を実行するという流れなのですが、TextAreaにプレイヤーに記入してもらったあと、エンターキーで読み込むにはどうしたらいいんでしょうか。
現状Mainメソッドを呼び出しても動きません。
一応流れとしては、Frame1クラスのフレームを作り、その後Frame1からこのMainメソッドを呼び出そうとしています。
java
1 2public class Main extends JFrame implements MouseListener{ 3 4 public static void main(String args[]){ 5 Frame1 frame1 = new Frame1("パワプロくん"); 6 frame1.setVisible(true); 7 } 8 9 public JTextArea mainArea; 10 Scanner input = new Scanner(System.in); 11 JMenuItem menuitem1; 12 JMenuItem menuitem2; 13 JMenuItem menuitem3; 14 String userName = ""; 15 16 ByteArrayOutputStream bytes = new ByteArrayOutputStream() { 17 public synchronized void flush() throws IOException { 18 mainArea.setText(toString()); 19 } 20 }; 21 22 PrintStream out = new PrintStream(bytes, true); 23 24 Main(String title, String name){ 25 userName = name; 26 this.setTitle(title); 27 this.setSize(624, 580); 28 this.setLocationRelativeTo(null); 29 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 30 this.addMouseListener(this); 31 32 JPanel pane1 = new JPanel(); 33 ImageIcon icon1 = new ImageIcon("背景.jpg"); 34 JLabel label1 = new JLabel(icon1); 35 pane1.add(label1); 36 37 JPanel pane2 = new JPanel(); 38 39 mainArea = new JTextArea(""); 40 mainArea.setPreferredSize(new Dimension(580, 150)); 41 mainArea.setEditable(true); 42 mainArea.setLineWrap(true); 43 mainArea.setBorder(new EtchedBorder(EtchedBorder.LOWERED)); 44 45 pane2.add(mainArea); 46 47 JPanel pane3 = new JPanel(); 48 49 pane3.add(pane1); 50 pane3.add(pane2); 51 pane3.add(menubar); 52 Container con = this.getContentPane(); 53 con.add(pane3); 54 55 Story(userName); 56 } 57 58 private void Story(String name) { 59 userName = name; 60 Hero h = new Hero(userName); 61 System.setErr(out); 62 System.setOut(out); 63 System.out.println(userName + "「嫌な夢にうなされたな、、、」"); 64 System.out.println(userName + "「でも確かにその通りだ。今日から必死に練習しよう」"); 65 boolean done = false; 66 boolean doit = false; 67 int mon = 9; 68 int day = 1; 69 Scanner scanner = new Scanner(System.in); 70 while(!done){ 71 String Days = mon + "月" + day + "日"; 72 while(!doit){ 73 bytes.reset(); 74 mainArea.setText(""); 75 System.out.println(Days); 76 System.out.println("(1)筋力, (2)打撃, (3)守備, (4)投げ込み, (5)変化球, (6)走り込み"); 77 try{ 78 System.out.println("今日はどうしようか(練習番号を入力): "); 79 int cmd = scanner.nextInt(); 80 if(cmd >= 1 && cmd <= 6){ 81 doit = true; 82 h.practice(cmd); 83 done = true; 84 } 85 else{ 86 bytes.reset(); 87 mainArea.setText(""); 88 System.out.println("練習する気がないのか!"); 89 } 90 }catch(InputMismatchException e) { 91 bytes.reset(); 92 mainArea.setText(""); 93 System.out.println("練習する気がないのか!"); 94 } 95 }//while(doit) 96 } 97 } 98}
JTextAreaには色々な内容が各行に出力されていきますけど、取り出したいのはJTextArea上にユーザーが入力した行だけ(典型的には最後の行)なのですよね?
回答2件
あなたの回答
tips
プレビュー