0

我们有 pageA ,它有一个 Jtable 和 World_info_object "info" 。该表显示来自人的数据(人在世界信息中)。我想编辑“信息”,所以每个人都有一个编辑按钮,这个页面也有一个“+NEW”按钮。这些按钮有动作监听器:(编辑几乎相同)

 newPerson.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        personFrame=new PersonPage( getInfo() , null );

        Thread closing =new Thread( new Runnable() {

            @Override
            public void run() {

                while(true){
                    if(personFrame.getConfirmed()==true){

                        setWorldInfo(personFrame.getInfo());
                        personFrame.setVisible(false);
                        personFrame.dispose();
                        System.out.println("closed");
                        updateTableData(); // repaint table !

                        break;
                    }
                    System.out.println("open...");
                }   

            }
        }, "closingWindow");




        closing.start();


    }
});

如您所见,这是一个寻找布尔值变化的线程,当用户在 personFrame 中按下 Okay 或取消按钮时,我将其变为真!目的是从 personFrame 中获取信息()并将其设置在 PageA (第一帧)中,并通过 this 完成,但是该线程会产生“线程中的异常”AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException: 5 >= 5”。

如果有人知道如何解决这个异常或者我应该如何在关闭它之前设置从其他页面发送的数据,请告诉我......(或者问题可能出在表格上)

例外:http: //i.stack.imgur.com/UnZnd.png

** 更新

我要做的就是从 pageA 制作 pageB 并向其发送信息(完成),然后在确认或关闭 pageB 后运行函数 update(); 在页面 A 中!任何想法 ?:)

**

// 几乎解决的程序:

公共类 PageA 扩展 JFrame {

private static HashMap<Integer, String>info=new HashMap<Integer,String>();
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {


    info.put(1, "Sofia");
    info.put(2, "XSR");


    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PageA frame = new PageA();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public PageA() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(80, 80, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JButton btnNewButton = new JButton("Open pageB");
    btnNewButton.setBounds(126, 11, 160, 23);
    btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            final PageB pageB=new PageB(info);
            pageB.addWindowListener(new WindowAdapter(){ // add listener to detect 
                 public void windowClosing(WindowEvent e){

                     setInfo(pageB.getInfo());
                     System.out.println("xxxx");

                 }
             });



        }
    });
    contentPane.add(btnNewButton);



}

public void setInfo(HashMap<Integer, String> uinfo){

    this.info=info;
}

}

公共类 PageB 扩展 JFrame {

private JPanel contentPane;
private HashMap<Integer, String> info;

public PageB(HashMap<Integer, String> info) {

    this.info=info;

    this.info.put(113, "Alfred");
    this.info.put(314, "Alfa");
    this.info.put(13, "Luter");

    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final PageB that=this;

    JButton btnOkayDone = new JButton("Okay , Done");
    btnOkayDone.setBounds(34, 228, 130, 23);
    btnOkayDone.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // add event for closing
            that.dispatchEvent(new WindowEvent(that, WindowEvent.WINDOW_CLOSING));

        }
    });
    contentPane.add(btnOkayDone);

    this.setVisible(true);



}


public HashMap<Integer, String> getInfo(){

    return info;


}

}

4

2 回答 2

0

您的错误消息说应用布局时出现错误,它希望找到一个组件但没有找到。这可能是由于使用不当而personFrame.dispose();没有更新您的布局以减少一个元素。

于 2014-12-25T09:57:28.643 回答
0

好的,所以我更改了一些部分:

            personFrame=new PersonPage( getInfo() , null );

        personFrame.addWindowListener(new WindowAdapter(){ // close page with listener
             public void windowClosing(WindowEvent e){

                 System.out.println("Closed");
                 setWorldInfo(personFrame.getInfo()); // update data (as before)
                 updateTableData(); // repaint and update frame (as before)
             }
         });

在 personPage 中,我添加了一个关闭事件以供收听!这里代码:

    // cancel operation (Also for okay button I add this ** line after update info )

    btnCancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {


            that.dispatchEvent(new WindowEvent(that, WindowEvent.WINDOW_CLOSING)); 
            //** instead of using a boolean I used closing event !


        }
    });

这解决了 AWT 问题也更新了数据 ^_^ (现在:|)

于 2014-12-25T10:59:09.103 回答