尝试将值从一个JPanel传递到另一个JPanel

时间:2013-12-07 22:55:45

标签: java swing jpanel

您好我正在尝试将值从一个JPanel传递到另一个JPanel。我无法理解,因为我没有任何构造函数可以通过它。我正在尝试从“CallsPanel”类中的JTextField获取上下文并将其传递给我的“ActiveCall”。

public class ActiveCall extends JPanel {

    private MainFrame frame;
    private JLabel lblNumber;
    private JButton btnHangUp;


    private Controller controller = new Controller();

    public ActiveCall(MainFrame frame) {

        this.frame = frame;
        this.setSize(300, 380);
        this.setLocation(10, 10);

        lblNumber = new JLabel();
        this.add(lblNumber);
        lblNumber.setLocation(10, 10);
        this.lblNumber.setSize(270, 40);

        btnHangUp = new JButton("Læg på");
        this.add(btnHangUp);
        btnHangUp.setLocation(10, 270);
        this.btnHangUp.setSize(270, 40);
        btnHangUp.addActionListener(controller);
    }

     private class Controller implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();

                if (source == btnHangUp) {
                    frame.showPanel("Calls");   //TODO: SKAL AFSLUTTE OPKALD, GEMME DET I - OG REDIRECT TIL LOGGEN!

                }

            }

        }
}



public class CallsPanel extends JPanel {


    private MainFrame frame;
    private JTextField txfCall;
    private JButton btnSearch, btnCall, btnCallLast, btnDelete;
    private JList lstOpkald;
    private JScrollPane scpOpkald;

    private Controller controller = new Controller();

    public CallsPanel(MainFrame frame) {

        this.frame = frame;
        this.setSize(300, 380);
        this.setLocation(10, 10);

        txfCall = new JTextField();
        this.add(txfCall);
        txfCall.setLocation(10, 10);
        txfCall.setSize(210, 40);
        txfCall.setText("Indtast Telefonnummer...");
        txfCall.addActionListener(controller);

        btnCall = new JButton("Ring Op");
        this.add(btnCall);
        btnCall.setLocation(10, 60);
        btnCall.setSize(270, 40);
        btnCall.addActionListener(controller);

        lstOpkald.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent arg0) {
                // TODO Sætter valgte nummer op i JTextField
                if(!lstOpkald.isSelectionEmpty())
                    txfCall.setText(Integer.toString(Service.getOpkaldList().get(lstOpkald.getSelectedIndex()).getTelefonnummer()));
            }
        });
    }

     private class Controller implements ActionListener
     {
            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();

                if (source == btnSearch) {      //SKAL ÅBNE EN NY TOM FRAME, HVOR MAN KAN SØGE OG VÆLGE EN KONTAKT HVOR TELEFONNUMMERET BLIVER AUTOMATISK PASTET IND I TLFNUMMER TEKSTFELTET!

                }

                if (source == btnCall) {        //TODO: SKAL SIMULERE ET OPKALD!
                    frame.showPanel("ActiveCall");
                }

                if (source == btnCallLast) {    //TODO: SKAL SIMULERE ET OPKALD FRA DET SIDSTE OPKALDTE NUMMER!
                    frame.showPanel("ActiveCall");
                }

                if (source.equals(btnDelete)) { // SLETTER MARKERET OPKALD FRA ARRAYLIST!
                    Opkald2 opkald = (Opkald2) lstOpkald.getSelectedValue();

                    if (opkald != null)
                    {
                        Service.deleteOpkald(opkald);
                    }
                    updateOpkaldList();         
                }
            }


            private void updateOpkaldList()
            {
                //Listen over opkald opdateres
                lstOpkald.setListData(Service.getOpkaldList().toArray());
            }
        }           

     public String getNumber()
     {
         String number = txfCall.getText();
         return number;
     }
}

1 个答案:

答案 0 :(得分:1)

  

我认为我需要更简单的东西。

我推断您希望ActiveCall收到一个事件,表示JListCallsPanel的选择状态发生了变化。 CallsPanel本身包含更新ListSelectionListener的{​​{1}}。由于组件可以包含多个侦听器,因此txfCall还包含已注册为ActiveCall的事件的ListSelectionListener

如果现有活动不适合您,您可以定义自己的lstOpkald,如图所示here