我试图让它使用它的toString()类列出来自arrayList传单的数据,但无论我做什么,我都无法让字符串出现在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 :(得分:0)
您的问题是,您为每个按钮定义了一个新的culminatingPro类实例作为动作侦听器。因此,每次单击一个新按钮时,只需修改其最终的Pro对象,然后列表按钮就不会知道其他实例。
重要的是:
//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();
}
}