您可以在JOption窗格输入中将多个字段输入到数组中吗?

时间:2017-05-17 18:18:04

标签: java arrays joptionpane

我正在尝试创建一个多数组设置,其中我从一个JOption面板输入所有书籍作者,标题和页面计数,并且新填充的数组将用作单个书籍数组设置的源。这是我的代码,这甚至可能吗?我可以进入窗格输入,但输入数据后立即出现错误(线程中的异常“main”java.lang.RuntimeException:无法编译的源代码 - 错误的ctor sym类型:     在librarybook.LibraryBookSorting.main(LibraryBookSorting.java:43) 〜\ 8.2 \ executor-snippets \ run.xml:53:Java返回:1)

我目前正在启动的代码是:

public class LibraryBookSorting {

static LibraryBook[] myBook = new LibraryBook[5];
static String title[5];
static String author[5];
static String count[5];
public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
        JTextField Title = new JTextField(5);
        JTextField Author = new JTextField(5);
        JTextField pgCount = new JTextField(5);

        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("Title:"));
        myPanel.add(Title);
        myPanel.add(Box.createHorizontalStrut(15)); // a spacer
        myPanel.add(new JLabel("Author:"));
        myPanel.add(Author);
        myPanel.add(new JLabel("PageCount: "));
        myPanel.add(pgCount);
        int result = JOptionPane.showConfirmDialog(null, myPanel,
                "Please Enter Title, Author and Page Count.",
                JOptionPane.OK_CANCEL_OPTION);
        title[i] = Title.getText();
        author[i] = Author.getText();
        count[i] = pgCount.getText();

        myBook[0] = new LibraryBook(title[0], author[0], count[0]);
        myBook[1] = new LibraryBook(title[1], author[1], count[1]);
        myBook[2] = new LibraryBook(title[2], author[2], count[2]);
        myBook[3] = new LibraryBook(title[3], author[3], count[3]);
        myBook[4] = new LibraryBook(title[4], author[4], count[4]);

        for (int i = 0; i < myBook.length; i++) {
            myBook.toString();
        }
    }

}

我的书课是

public class LibraryBook {

    private String title;
    private String author;
    private String pageCount;

    //no arg contructor
    public LibraryBook() {
        title = "";
        author = "";
        pageCount = "";
    }

    public LibraryBook(String bookTitle, String bookAuthor, String bookpageCount) {
        title = bookTitle;
        author = bookAuthor;
        pageCount = bookpageCount;
    }

    //get methods
    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getPageCount() {
        return pageCount;
    }
    //set methods

    public void setTitle(String bookTitle) {
        title = bookTitle;
    }

    public void setAuthor(String bookAuthor) {
        author = bookAuthor;
    }

    public void setPageCount(String bookPageCount) {
        pageCount = bookPageCount;
    }

    @Override
    public String toString() {
        return ("Your book is: " + title + " by " + author + " with " + pageCount + " pages.");
    }
}

1 个答案:

答案 0 :(得分:0)

您的 LibraryBookSorting 类有许多错误,例如错误的数组声明,而 i 正在使用两个不同循环的两个主题,一旦修复了这些错误,您的代码就会完美运行。看看下面的代码:

// Keep stage reference here.
var draggedStage:Stage;

// Keep dragged item reference here.
var draggedItem:InteractiveObject;

// The list of items to drag.
var aList:Array =
[
    AirBP,PetroChem,LiquifiedGas,Exploration,Plastic,
    BiofuelsFarm,Trading,Electricity,Development,
    Production,Distribution,Lubes,Retail,Shipping,
    Refining,BPMarine,Terminal,Terminal2,Pipeline,
    Pipeline2,SugarCane,WindPower
];

// Subscribe all items for MOUSE_DOWN event.
for each (draggedItem in aList)
    draggedItem.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);

function onDrag(e:MouseEvent):void
{
    // Get the source of event.
    draggedItem = e.target as InteractiveObject;
    draggedItem.startDrag();

    // Hook the stage events for MOUSE_UP event. You can skip using "draggedStage"
    // if you are sure that stage reference  is always available.
    draggedStage = dtaggedItem.stage;
    draggedStage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}

function onDrop(e:MouseEvent):void
{
    // Stopp dragging things.
    draggedItem.stopDrag();

    // Unhook stage for it is no longer needed.
    draggedStage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);

    // Forget the references.
    draggedItem = null;
    draggedStage = null;
}
相关问题