1
您好,我試圖從一個JPanel傳遞值到另一個JPanel。我無法弄清楚,因爲我沒有構造函數來傳遞它。我試圖從類「CallsPanel」中的JTextField中獲取上下文,並將其傳遞給我的「ActiveCall」。嘗試將值從一個JPanel傳遞給另一個JPanel
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;
}
}
提示:清理不必要的字段,以便人們可以專注於實際問題。傾倒整個班級不是一件好事。 – givanse
我試圖清理一下,我不知道這是否足夠。 垃圾桶我需要更簡單的東西,我想。 – user2993005