2016-05-04 17 views
-1

GUIを使用しているときにarraylistの項目を追加、削除、検索するにはどうすればよいですか?私はユーザーの入力をプログラムarraylistに追加したいと思います。 また、どのように私はINFO.TXTというファイルが存在するかどうかを確認します - もしそうであれば - SET TEXTAREA INTO INFO.TXTファイル内のすべての情報は、 - 以下を参照してください。arraylistにJava GUIを保存する

import java.awt.*; 
import java.awt.event.*; 
import java.io.BufferedWriter; 
import java.io.FileWriter; 
import javax.swing.*; 
import java.io.File; 
import java.io.*; 
import static java.lang.System.out; 

public class FinalMainFrame extends 
JFrame { 
    BufferedReader br = null; 
private Container pane; 
private JTextField textField; 
public FinalMainFrame() { 
    pane=getContentPane(); 
      pane.setLayout(null); 

    JMenuBar menuBar = new JMenuBar(); 
    JMenu menuFile = new JMenu(); 
    JMenuItem menuFileExit = new  
    JMenuItem(); 

    menuFile.setText("File"); 
    menuFileExit.setText("Exit"); 
    menuFileExit.addActionListener 
    (  (ActionEvent e) -> { 
     FinalMainFrame.this.windowClosed(); 
      }); 
    menuFile.add(menuFileExit); 
    menuBar.add(menuFile); 

    setTitle("Final Project..."); 
    setJMenuBar(menuBar); 
    setSize(new Dimension(250, 200)); 


    JLabel label = new JLabel("Enter Info:"); 
    label.setFont(new Font("Serif", 
    Font.PLAIN, 18)); 
    pane.add(label); 
    label.setLocation(0, 10); 
    label.setSize(100, 30); 

    textField = new JTextField(20); 
    pane.add(textField); 
    textField.setSize(100, 30); 
    textField.setLocation(120, 10); 

    JButton button1 = new JButton("Save"); 
    pane.add(button1); 
    button1.setLocation(30, 70); 
    button1.setSize(150, 50); 
    button1.addActionListener 
    (
     new ActionListener() { 
      public void 
    actionPerformed(ActionEvent e) { 

    JOptionPane.showMessageDialog( 
    null,"You pressed: " + 
    e.getActionCommand()); 
    try{ 

     FileReader read = new 
     FileReader("info.txt"); 
     BufferedReader br = new 
     BufferedReader(read); 
      String txt = "";    
      String line = br.readLine(); 
      while (line != null){ 
       txt += line; 
       line = br.readLine(); 
       out.println(line); 
      } 


     String text = textField.getText(); 
     FileWriter stream = new 
     FileWriter("info.txt"); 
     try (BufferedWriter out = new 
     BufferedWriter(stream)) { 
      out.write(text); 
    } 

     JOptionPane.showMessageDialog( 
     null,"You entered: " + text); 
    } 
    catch (Exception ex) {}     
      } 
     } 
    ); 
    this.addWindowListener 
    (
     new WindowAdapter() { 
      public void 
     windowClosing(WindowEvent e) { 

     FinalMainFrame.this.windowClosed(); 
      } 
     } 
    ); 
     } 
    protected void windowClosed() { 
    System.exit(0); 
       } 
     } 
+0

コードを投稿してください。それはあなたが正解を得るのを助け、同じ問題に直面する他の人を助けるでしょう –

答えて

0

をGUIについてあなたの話をするときイベントを考えるべきです。あなたのarraylistにクリックイベントなどでユーザー入力を追加してください。

関連する問題