简体   繁体   English

面板可以互相通信吗?

[英]Can panels communicate with each other?

I'm trying to call a method from a panel class, however it does not result in anything. 我正在尝试从面板类中调用方法,但是不会产生任何结果。 Can panels communicate with each other? 面板可以互相通信吗? Or is there another reason why this isn't working? 还是有其他原因导致其不起作用?

Calling the method name() in the leftInput class. 在leftInput类中调用方法name()。

ButtonPanel class. 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. 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("");
}
}

Do you ever actually construct the LeftInputPanel anywhere? 您实际上在任何地方构造了LeftInputPanel吗? (One would think you'd be getting a null pointer exception in the code at the top). (有人认为您会在顶部的代码中得到一个空指针异常)。

JPanels are extensions of Component . JPanelsComponent的扩展。 That means you can always call Component.getParent() to get the immediate container of your component and using that reference, gain access to all sibling components in the container by using Container.getComponents() . 这意味着您始终可以调用Component.getParent()来获取组件的直接容器,并使用该引用,通过使用Container.getComponents()来访问容器中的所有同级组件。

More specifically, if you add your panels to the top level container using indexes , then you can specifically request the reference to a sibling component using its index . 更具体地说,如果使用索引将面板添加到顶级容器 ,则可以使用其index专门请求对同级组件的引用

This is one way to avoid passing around references to various panels, by using the parent container as a containment context (which is precisely what it is). 通过使用父容器作为包含上下文(正是它的意思),这是避免传递对各个面板的引用的一种方法。

And once you have the reference, a class is a class and you obviously can call all visible methods. 一旦有了引用,一个类就是一个类,显然可以调用所有可见的方法。

What is not working and what are you expecting to happen? 什么不起作用,您期望发生什么?

If you expect to be calling a method on an existing object from another object, that is perfectly doable, provided the method is public. 如果您希望从另一个对象调用现有对象上的方法,那是完全可行的,只要该方法是公共的。 The fact that these objects are JPanel s are irrelevant. 这些对象是JPanel的事实是无关紧要的。

What you should do is learn how to use the debugger to figure out if your method is being called and the println is occuring but the name is empty, or your method is not called, or any other problem. 您应该做的是学习如何使用调试器来确定您的方法是否正在被调用,是否正在运行println但名称为空或未调用您的方法或其他任何问题。

If you're using Eclipse, there are some great debugging video tutorials here . 如果你正在使用Eclipse,有一些伟大的调试视频教程在这里 But even if you're not using Eclipse you can check them out, and can apply them to whatever IDE you're using. 但是,即使您不使用Eclipse,也可以将其签出,并将其应用于您使用的任何IDE。 It'll be far more efficient than sprinkling System.out.println s here and there. 这将比在各处System.out.println效率更高。

You shouldn't name methods like attributes. 您不应命名诸如属性之类的方法。 For instance, name() should be getName() or something like that. 例如,name()应该是getName()或类似的名称。 Might solve your problem. 可能会解决您的问题。

There are a couple things that I see as missing here: 我认为这里缺少几件事:

  • within your actionPerformed method, you declare a field and without trying to initialize it, you try to access it. actionPerformed方法中,您声明一个字段,并且不尝试对其进行初始化,而是尝试对其进行访问。 This will be caught by the compiler. 这将被编译器捕获。
  • I dont see anywhere that you create an AllocateListener or attach it to anything in your panel that would trigger the actionPerformed method. 我看不到您创建AllocateListener或将其附加到面板中任何会触发actionPerformed方法的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM