[英]Can panels communicate with each other?
我正在嘗試從面板類中調用方法,但是不會產生任何結果。 面板可以互相通信嗎? 還是有其他原因導致其不起作用?
在leftInput類中調用方法name()。
ButtonPanel類。
import model.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonPanel extends JPanel implements View
{
private Prison prison;
private JButton button = new JButton("Allocate Cell");
private LeftInputPanel leftInput;
private CrimePanel crimePanel;
public ButtonPanel(Prison prison, LeftInputPanel leftInput)
{
this.prison = prison;
this.leftInput = leftInput;
setup();
build();
}
public void setup()
{
}
public void build()
{
Dimension size = new Dimension(240, 70);
button.setPreferredSize(size);
button.setMinimumSize(size);
button.setMaximumSize(size);
button.addActionListener(new AllocateListener());
add(button);
}
public void update()
{
leftInput.clear();
}
private class AllocateListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Criminal criminal = new Criminal(leftInput.name());
prison.add(criminal);
System.out.println(leftInput.name());
}
}
}
leftInput類。
import model.*;
import java.awt.*;
import javax.swing.*;
public class LeftInputPanel extends JPanel
{
private Prison prison;
public JTextField name = new JTextField();
public JTextField days = new JTextField();
public JTextField months = new JTextField();
public JTextField years = new JTextField();
public LeftInputPanel(Prison prison)
{
this.prison = prison;
setup();
build();
}
public void setup()
{
setLayout(new FlowLayout());
Dimension size = new Dimension(100, 190);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
public void build()
{
JLabel label = new JLabel("Name");
Dimension size = new Dimension(90, 20);
name.setPreferredSize(size);
add(label);
add(name);
Box box = Box.createVerticalBox();
box.add(daysPanel());
box.add(monthsPanel());
box.add(yearsPanel());
add(box);
}
public JPanel daysPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, days, " days");
return panel;
}
public JPanel monthsPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, months, " months");
return panel;
}
public JPanel yearsPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
addField(panel, years, " years");
return panel;
}
public void addField(JPanel panel, JTextField field, String label)
{
Dimension size = new Dimension(30, 20);
field.setPreferredSize(size);
field.setMinimumSize(size);
field.setMaximumSize(size);
panel.add(field);
panel.add(new JLabel(label));
}
public String name()
{
return name.getText();
}
public int days()
{
return Integer.parseInt(days.getText());
}
public int months()
{
return Integer.parseInt(months.getText());
}
public int years()
{
return Integer.parseInt(years.getText());
}
public void clear()
{
name.setText("");
days.setText("");
months.setText("");
years.setText("");
}
}
您實際上在任何地方構造了LeftInputPanel嗎? (有人認為您會在頂部的代碼中得到一個空指針異常)。
JPanels是Component的擴展。 這意味着您始終可以調用Component.getParent()來獲取組件的直接容器,並使用該引用,通過使用Container.getComponents()來訪問容器中的所有同級組件。
更具體地說,如果使用索引將面板添加到頂級容器 ,則可以使用其index專門請求對同級組件的引用 。
通過使用父容器作為包含上下文(正是它的意思),這是避免傳遞對各個面板的引用的一種方法。
一旦有了引用,一個類就是一個類,顯然可以調用所有可見的方法。
什么不起作用,您期望發生什么?
如果您希望從另一個對象調用現有對象上的方法,那是完全可行的,只要該方法是公共的。 這些對象是JPanel
的事實是無關緊要的。
您應該做的是學習如何使用調試器來確定您的方法是否正在被調用,是否正在運行println但名稱為空或未調用您的方法或其他任何問題。
如果你正在使用Eclipse,有一些偉大的調試視頻教程在這里 。 但是,即使您不使用Eclipse,也可以將其簽出,並將其應用於您使用的任何IDE。 這將比在各處System.out.println
效率更高。
您不應命名諸如屬性之類的方法。 例如,name()應該是getName()或類似的名稱。 可能會解決您的問題。
我認為這里缺少幾件事:
actionPerformed
方法中,您聲明一個字段,並且不嘗試對其進行初始化,而是嘗試對其進行訪問。 這將被編譯器捕獲。 AllocateListener
或將其附加到面板中任何會觸發actionPerformed
方法的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.