2016-01-22 46 views
0

我想让它列出来自arrayList传单的数据与它的toString()类,但无论我做什么,我都无法让字符串出现在textArea和随后的JScrollPane。文本没有出现在滚动窗格或文本区域

 if (event.getSource() == jbtList) 
     { 
     // New textarea 
     JTextArea displayStrings = new JTextArea(5, 30); 
     // Generate a new JScrollPane and set its viewport to the textarea 
     for(int p = 0; p < flyers.size(); p++) 
     { 
      String flyerList = flyers.get(p).toString() + "\n"; 
      displayStrings.append(flyerList); 
     } 
     JScrollPane scroll = new JScrollPane(displayStrings); 
     JOptionPane.showMessageDialog(null,scroll); 

含传单阵列我的最终等级:

import java.util.ArrayList; 
import java.util.Iterator; 
public class Culminating { 

private String FirstName; 
private String LastName; 
private String PhoneNumber; 
private String Adress; 
private int Column; 
private int Row; 
//Initializing the variables 

public String getFirstName() 
{ 
    return this.FirstName; 
} 
public String getLastName() 
{ 
    return this.LastName; 
} 
public String getPhoneNumber() 
{ 
    return this.PhoneNumber; 
} 
public String getAdress() 
{ 
    return this.Adress; 
} 
public int getColumn() 
{ 
    return this.Column; 
} 
public int getRow() 
{ 
    return this.Row; 
} 

// Set data to variables and return if needed 
public void setFirstName(String FirstName) 
{ 
    this.FirstName = FirstName; 
} 
public void setLastName(String LastName) 
{ 
    this.LastName = LastName; 
} 
public void setPhoneNumber(String PhoneNumber) 
{ 
    this.PhoneNumber = PhoneNumber; 
} 
public void setAdress(String Adress) 
{ 
    this.Adress = Adress; 
} 

public Culminating(String FirstName, String LastName, String PhoneNumber, String Adress, int Column, int Row) 

{ 
    this.FirstName = FirstName; 
    this.LastName = LastName; 
    this.PhoneNumber = PhoneNumber; 
    this.Adress = Adress; 
    this.Column = Column; 
    this.Row = Row; 
} 



public String toString() 
{ 
    return FirstName + " " + LastName + " " + " Phone Number: " + PhoneNumber + " Adress: " + Adress; 
    //Turn array to string and return 
} 
} 

而其他的全类:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Scanner; 
import java.util.ArrayList; 
import java.util.Iterator; 
public class CulminatingPro implements ActionListener 
{ 
    //Create an array of buttons 
    static JButton[][] buttons = new JButton[10][4]; 
    static JButton jbtList = new JButton("List"); 
    ArrayList<Culminating> flyers = new ArrayList<Culminating>(); 
    static JPanel mainPanel = new JPanel(); 
    static JPanel paneSeats = new JPanel(); 
    static JPanel paneInfo = new JPanel(); 
    String fn; 
    String ln; 
    String pn; 
    String adress; 
    int column; 
    int row; 
    int reply; 
    int v; 
    Object[] options = {"First Name", 
        "Last Name", 
        "Phone Number", 
        "Adress"}; 
    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Fly By Buddies"); 
     frame.setSize(900, 900); 

     mainPanel.setLayout(new GridLayout(1,2)); 
     JPanel paneSeats = new JPanel(); 
     JPanel paneInfo = new JPanel(); 
     paneSeats.setLayout(new GridLayout(11, 4, 5,5)); 
     mainPanel.add(paneSeats); 
     mainPanel.add(paneInfo); 
     frame.setContentPane(mainPanel); 

     for (int i = 0; i < buttons.length; i++) 
     { 
      for(int j = 0; j < buttons[0].length; j++) 
      { 

       if (j + 1 == 1) 
       { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "A"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(new CulminatingPro()); 
       } 
       else if (j + 1 == 2) 
       { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "B"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(new CulminatingPro()); 
       } 
       else if (j + 1== 3) 
       { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "C"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(new CulminatingPro()); 
       } 
       else if (j + 1== 4) 
       { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "D"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(new CulminatingPro()); 
       } 

      } 
     } 
     jbtList.setPreferredSize(new Dimension(400, 40)); 
     jbtList.addActionListener(new CulminatingPro()); 
     paneInfo.add(jbtList, BorderLayout.NORTH); 

     frame.setVisible(true); 
     frame.toFront(); 
    } 
    public void actionPerformed(ActionEvent event) 
    { 
     for (int i = 0; i < buttons.length; i++) 
     { 
      for(int j = 0; j < buttons[0].length; j++) 
      { 
       if (event.getSource() == buttons[i][j]) 
       { 
        v = 0; 
        for (int k = 0; k < flyers.size();k++) 
        { 

         if((flyers.get(k).getColumn() == (j) && (flyers.get(k).getColumn() == (j)))) 
         { 
          if (!flyers.get(k).getFirstName().equals("")) 
          { 
           reply = JOptionPane.showConfirmDialog(null, 
           "Would you like to delete the info on the passenger?", "Deletion", JOptionPane.YES_NO_OPTION); 
           if (reply == JOptionPane.YES_OPTION) 
           { 
            flyers.remove(k); 
            buttons[i][j].setBackground(Color.GREEN); 
           } 
           else if (reply == JOptionPane.NO_OPTION) 
           { 
+0

你执行或编译时是否得到一个异常?您是否尝试将输出打印到控制台,是否为空?你看到scrollpane/textarea了吗? –

+0

我想,你的输出字符串是空的,我试过你的代码,它运行没有问题(可运行的例子)http://pastebin.com/kbXQsH2W –

+0

这是我的最终课程。我没有看到我在做什么与你不同......我将它编辑到文章 –

回答

0

你的问题是,您定义culminatingPro类的新实例为每个按钮作为动作监听器。所以每次你点击一个新按钮,你只需修改它的最高级对象,然后列表按钮不知道其他实例。

重要行是:

//here you are creating for each button its own CulminatingPro instance 
//so in clicking it, it will never know about the flying list 
//of the other instances 
buttons[i][j].addActionListener(new CulminatingPro()); 

//here the same like before, your print list button have allways an 
//empty list, because this instance of culminatingPro is only used 
//by this button. 
jbtList.addActionListener(new CulminatingPro()); 

这应该是永诺相同culminatingpro对象,所以创建酮和(在samle代码等)使用它。

我有一个简单的修改版本的代码,工作。 (你不提供整个代码,所以我不得不修改它):

public class CulminatingPro implements ActionListener { 
    // Create an array of buttons 
    static JButton[][] buttons = new JButton[10][4]; 
    static JButton jbtList = new JButton("List"); 
    ArrayList<Culminating> flyers = new ArrayList<Culminating>(); 
    static JPanel mainPanel = new JPanel(); 
    static JPanel paneSeats = new JPanel(); 
    static JPanel paneInfo = new JPanel(); 
    String fn; 
    String ln; 
    String pn; 
    String adress; 
    int column; 
    int row; 
    int reply; 
    int v; 
    Object[] options = { "First Name", "Last Name", "Phone Number", "Adress" }; 
    JFrame frame = new JFrame("Fly By Buddies"); 

    public CulminatingPro() { 
     System.out.println("HERE"); 
     frame.setSize(900, 900); 

     mainPanel.setLayout(new GridLayout(1, 2)); 
     JPanel paneSeats = new JPanel(); 
     JPanel paneInfo = new JPanel(); 
     paneSeats.setLayout(new GridLayout(11, 4, 5, 5)); 
     mainPanel.add(paneSeats); 
     mainPanel.add(paneInfo); 
     frame.setContentPane(mainPanel); 

     for (int i = 0; i < buttons.length; i++) { 
      for (int j = 0; j < buttons[0].length; j++) { 

       if (j + 1 == 1) { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "A"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(this); 
       } else if (j + 1 == 2) { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "B"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(this); 
       } else if (j + 1 == 3) { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "C"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(this); 
       } else if (j + 1 == 4) { 
        buttons[i][j] = new JButton("Seat " + (i + 1) + "D"); 
        buttons[i][j].setBackground(Color.GREEN); 
        paneSeats.add(buttons[i][j]); 
        buttons[i][j].addActionListener(this); 
       } 

      } 
     } 
     jbtList.setPreferredSize(new Dimension(400, 40)); 
     jbtList.addActionListener(this); 
     paneInfo.add(jbtList, BorderLayout.NORTH); 

     frame.setVisible(true); 
     frame.toFront(); 
    } 

    public void actionPerformed(ActionEvent event) { 
     if (event.getSource() == jbtList) { 
      // New textarea 
      JTextArea displayStrings = new JTextArea(5, 30); 
      // Generate a new JScrollPane and set its viewport to the textarea 
      System.out.println(flyers.size()); 
      for (int p = 0; p < flyers.size(); p++) { 
       String flyerList = flyers.get(p).toString() + "\n"; 
       displayStrings.append(flyerList); 
      } 
      JScrollPane scroll = new JScrollPane(displayStrings); 
      JOptionPane.showMessageDialog(null, scroll); 
      return; 
     } 

     Point pos = getPos(event.getSource()); 

     // is seat free? 
     boolean free = true; 
     for (int i = 0; i < flyers.size(); i++) { 
      if (flyers.get(i).getColumn() == pos.x 
        && flyers.get(i).getRow() == pos.y) { 
       free = false; 
       if (!flyers.get(i).getFirstName().equals("")) { 
        reply = JOptionPane 
          .showConfirmDialog(
            null, 
            "Would you like to delete the info on the passenger?", 
            "Deletion", JOptionPane.YES_NO_OPTION); 
        if (reply == JOptionPane.YES_OPTION) { 
         flyers.remove(i); 
         buttons[pos.x][pos.y].setBackground(Color.GREEN); 
        } 
       } 
      } 
     } 

     if (free) { 
      flyers.add(new Culminating("Test1", "1Tset", "00000", "test", 
        pos.x, pos.y)); 
      System.out.println("Add culmination: " + flyers.size()); 

      buttons[pos.x][pos.y].setBackground(Color.RED); 
     } 
    } 

    public Point getPos(Object object) { 
     for (int i = 0; i < buttons.length; i++) { 
      for (int j = 0; j < buttons[i].length; j++) { 
       if (object == buttons[i][j]) { 
        return new Point(i, j); 
       } 
      } 
     } 
     return new Point(-1, -1); 
    } 

    public static void main(String[] args) { 
     new CulminatingPro(); 
    } 
}