面板可以相互沟通吗?
我试图从面板类调用一个方法,但它不会导致任何结果。面板可以相互沟通吗?还是有另一个原因,为什么这不起作用?面板可以相互沟通吗?
在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 class。
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? (人们会认为你会得到顶部代码中的空指针异常)。
我有一个InputPanel类。在那个类中,我构建了LeftInputPanel和RightInputPanel。然后,我在RightInputPanel中构建一个ButtonPanel(带有监听器的ButtonPanel)。 – Karen 2009-10-27 13:27:11
您不应该命名像属性的方法。例如,name()应该是getName()或类似的东西。可能解决你的问题。
有一对夫妇的事情,我看到的是缺少在这里:
- 您
actionPerformed
方法中,需要声明一个字段并没有试图对其进行初始化,您尝试访问它。这将被编译器捕获。 - 我没有看到任何地方,你创建一个
AllocateListener
或附加到你的面板上的任何事情,将触发actionPerformed
方法。
什么不工作,你期望发生什么?
如果您希望从另一个对象调用现有对象的方法,这是完全可行的,前提是该方法是公开的。这些对象是JPanel
的事实是无关紧要的。
你应该做的就是学习如何使用调试器来确定你的方法是否被调用,并且println发生了,但名称是空的,或者你的方法没有被调用或者其他问题。
如果您使用的是Eclipse,那么有一些很棒的调试视频教程here。但即使你没有使用Eclipse,你也可以检查它们,并将它们应用到你正在使用的任何IDE中。这将比在这里和那里喷洒System.out.println
效率更高。
所以我这样做的方式,我从我已经建立的面板调用方法?如果这不是原因,那么它有什么问题? – Karen 2009-10-27 13:45:19
JPanels是Component的扩展。这意味着您可以始终致电Component.getParent()以获取组件的直接容器,并使用该参考,通过使用Container.getComponents()访问容器中的所有兄弟组件。
更具体地说,如果你add your panels to the top level container using indexes,那么你可以专门request the reference to a sibling component using its index。
这是一种通过使用父容器作为包含上下文(正是它是什么)来避免传递各种面板引用的方法。
一旦你有了参考,一个类是一个类,你显然可以调用所有可见的方法。
什么不工作,你期望发生什么? – JRL 2009-10-27 13:44:50
当我测试它时,看看是否使用System.out正确调用了该方法。打印,它不打印出来。在开始调用其他方法之前,我需要确保它调用正确。 – Karen 2009-10-27 13:52:25