gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:30 24 4
gpt4 key购买 nike

您好,我正在尝试将值从一个 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;
}
}

最佳答案

I need something more simple, I think.

我推断您希望 ActiveCall 接收一个事件,该事件表示 CallsPanelJList 的选择状态发生变化。 CallsPanel 本身包含一个更新 txfCallListSelectionListener。由于组件可以有多个监听器,因此让 ActiveCall 还包含一个注册为从 lstOpkald 接收事件的 ListSelectionListener

如果现有事件没有满足您的需要,您可以定义自己的PropertyChangeEvent,如图here .

关于java - 尝试将值从一个 JPanel 传递到另一个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com