2015-11-25 23 views
-1

你好,我正在嘗試做一個程序,當即將發生的事件是關閉或項目到期時提醒你。我在JList上遇到了一些麻煩。當我嘗試和刪除事件/項目不刷新我已經嘗試list.updateUI(),使無形的列表,然後使其可見再次嘗試刷新,但沒有奏效,如果你能幫助謝謝提前 這裏是java代碼試圖提出提醒程序幫助更新J列表

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import javax.swing.*; 

public class alarm implements ActionListener { 
public static JFrame window; 
public static JPanel p; 
@SuppressWarnings("rawtypes") 
public static JList list; 
public RandomAccessFile rw; 
public int[] numberArray; 
public String[] name; 
public String[] desc; 
public String[] date; 
public JButton remove; 
String[] listData = new String[1000000]; 
@SuppressWarnings({ "unchecked", "rawtypes" }) 
public void windowList() { 
    list = new JList(name); 
    JScrollPane pane = new JScrollPane(list); 
    pane.setBounds(100, 10, 400, 400); 
    p.add(pane); 
    pane.setVisible(true); 
} 
public void dataLoad() throws IOException { 
    try { 
     rw = new RandomAccessFile("db.txt", "rw"); 
     int loop = 0; 
     int insertNumber = 0; 
     numberArray = new int[1000000]; 
     name = new String[1000000]; 
     desc = new String[1000000]; 
     date = new String[1000000]; 
     String row = ""; 
     while(loop == 0) { 
      row = rw.readLine(); 
      if(rw.getFilePointer() == rw.length()) { 
       insertNumber = 0; 
       loop = 1; 
      }else if(row.equals("/")) { 
       insertNumber = insertNumber + 1; 
       numberArray[insertNumber] = insertNumber; 
       name[insertNumber] = rw.readLine(); 
       desc[insertNumber] = rw.readLine(); 
       date[insertNumber] = rw.readLine(); 
      } 
     } 
     while(loop == 1) { 
      insertNumber = insertNumber + 1; 
      if(name[insertNumber] == null) { 
       loop = 2; 
      }else { 
       System.out.println(name[insertNumber]); 
      } 
     } 
     System.out.println("Arrays Loaded"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 
public void dataRemove() throws IOException { 
    rw = new RandomAccessFile("db.txt", "rw"); 
    int remove = 0; 
    int count = 0; 
    int countR = 0; 
    String tempData = ""; 
    String value = list.getSelectedValue().toString(); 
    rw.seek(0); 
    while(remove == 0) { 
     tempData = rw.readLine(); 
     System.out.println(tempData); 
     if(rw.getFilePointer() == rw.length()) { 
      System.out.println("Not found..."); 
      remove = 1; 
     }else if(tempData.equals(value)) { 
      countR = count; 
      count = 0; 
      remove = 1; 
     } 
     else if(tempData.equals("/")) { 
      count = count + 1; 
     } 
    } 
    rw.seek(0); 
    System.out.println("Starting loop 2"); 
    while(remove == 1) { 
     tempData = rw.readLine(); 
     if(tempData.equals("/")) { 
      count = count + 1; 
     } 
     if(count == countR) { 
      rw.seek(rw.getFilePointer()-2); 
      rw.writeBytes("/"); 
      remove = 2; 
     } 
    } 

    numberArray = new int[1000000]; 
    name = new String[1000000]; 
    desc = new String[1000000]; 
    date = new String[1000000]; 
    dataLoad(); 
} 
alarm() throws IOException { 
    dataLoad(); 
    window = new JFrame(); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setResizable(false); 
    window.setSize(600, 600); 
    p = new JPanel(); 
    p.setLayout(null); 
    window.add(p); 
    remove = new JButton("Remove"); 
    remove.addActionListener(this); 
    remove.setBounds(100, 415, 100, 30); 
    p.add(remove); 
    remove.setVisible(true); 
    windowList(); 

} 
public static void main(String[] args) throws IOException { 
    new alarm(); 
    window.setVisible(true); 
} 
public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == remove) { 
     try { 
      dataRemove(); 
      list.setVisible(false); 
      windowList(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 
} 

} 
+0

開始由具有看看[如何使用列表](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html)。然後看看['DefaultListModel'](http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html),它有一個很好用的'remove'方法 – MadProgrammer

回答

3

你的ListModel在哪裏?您需要使用ListModel,例如DefaultListModel<String>,以便您輕鬆添加和移除顯示的JList<String>中的元素。您可以通過其構造函數或其setModel(...)方法將模型傳遞給JList。閱讀JList tutorial,因爲它在那裏都非常詳細地解釋。

其他問題:

  • 您使用空佈局和的setBounds。雖然Swing的新手可能看起來像是創建複雜GUI的最簡單也是最好的方式,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。
  • 您的代碼不遵循Java命名約定,您將需要學習和使用它們:Java naming conventions。變量名應該全部以小寫字母開頭,而類名則以大寫字母開頭。瞭解這一點,並遵循這一點將使我們能夠更好地理解您的代碼,並讓您更好地理解其他代碼。