Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton';s的ActionListener正在响应,但代码显然并没有被执行,但当我直接调用该方法时,它就被执行了。非常奇怪的行为?_Java_Swing_Actionlistener - Fatal编程技术网

Java JButton';s的ActionListener正在响应,但代码显然并没有被执行,但当我直接调用该方法时,它就被执行了。非常奇怪的行为?

Java JButton';s的ActionListener正在响应,但代码显然并没有被执行,但当我直接调用该方法时,它就被执行了。非常奇怪的行为?,java,swing,actionlistener,Java,Swing,Actionlistener,当我添加ActionListener并将代码放入调用方法的actionPerformed中时,该方法被调用得很好,但其中的代码(应该动态添加各种Swing组件)似乎没有响应。但是,当我在ActionListener之外添加行时,会运行相同的代码,并且添加的组件很好。这很奇怪,我不知道问题出在哪里 当我在action performed方法中添加类似于System.out.println的语句来添加设备时,显然正在调用它,但是没有向JPanel添加任何内容。当我把System.out.printl

当我添加
ActionListener
并将代码放入调用方法的
actionPerformed
中时,该方法被调用得很好,但其中的代码(应该动态添加各种Swing组件)似乎没有响应。但是,当我在
ActionListener
之外添加行时,会运行相同的代码,并且添加的组件很好。这很奇怪,我不知道问题出在哪里

当我在action performed方法中添加类似于
System.out.println的语句来添加设备时,显然正在调用它,但是没有向
JPanel
添加任何内容。当我把
System.out.println
放在
addAppliance()
方法中时,调用它也很好。就像我说的,一个非常奇怪的问题。以下是我到目前为止的全部计划:

package Main;    
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ElectricPanel extends JPanel
{
  private static final int WIDTH = 360;
  private static final int HEIGHT = 600;
  private ArrayList<JTextField> description;
  private ArrayList<JTextField> volts;
  private ArrayList<JTextField> current;
  private ArrayList<JTextField> power;
  private JButton addButton;
  private JButton subtractButton;
  private JLabel[] labels;
  private int numOfAppliances;
  public ElectricPanel()
  {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));  
    setFocusable(true);
    requestFocus();
    setLayout(null);
    description = new ArrayList<JTextField>();
    volts = new ArrayList<JTextField>();
    current = new ArrayList<JTextField>();
    power = new ArrayList<JTextField>();
    labels = new JLabel[]{new JLabel("Description"),
        new JLabel("Volts"),
        new JLabel("Current"),
        new JLabel("Power")};
    Dimension size = labels[0].getPreferredSize();
    labels[0].setBounds(10, 10, size.width, size.height);
    size = labels[1].getPreferredSize();
    labels[1].setBounds(125, 10, size.width, size.height);
    size = labels[2].getPreferredSize();
    labels[2].setBounds(185, 10, size.width, size.height);
    size = labels[3].getPreferredSize();
    labels[3].setBounds(245, 10, size.width, size.height);
    for (int i = 0; i < 4; i++)
    {
      add(labels[i]);
    }
    numOfAppliances = 0;
    addAppliance();
    setupAddAndSubtractButtons();
  }
  private void setupAddAndSubtractButtons()
  {
    Insets insets = new Insets(0, 0, 0, 0);
    addButton = new JButton("+");
    subtractButton = new JButton("-");
    addButton.setBounds(305, 30, 24, 18);
    subtractButton.setBounds(305, 50, 24, 18);
    subtractButton.setVisible(false);
    addButton.setMargin(insets);
    subtractButton.setMargin(insets);
    addButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        addAppliance();
      }
    });
    subtractButton.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent arg0)
      {
        subtractAppliance();
      }
    });
    add(addButton);
    add(subtractButton);
    addAppliance();
  }
  private void tryToAdd()
  {
    addAppliance();
  }
  private void addAppliance()
  {
    description.add(new JTextField(10));
    volts.add(new JTextField(5));
    current.add(new JTextField(5));
    power.add(new JTextField(5));

    Dimension size = description.get(numOfAppliances).getPreferredSize();
    description.get(numOfAppliances).setBounds(10, 30 + 20 * numOfAppliances, size.width, size.height);
    size = volts.get(numOfAppliances).getPreferredSize();
    volts.get(numOfAppliances).setBounds(125, 30 + 20 * numOfAppliances, size.width, size.height);
    size = current.get(numOfAppliances).getPreferredSize();
    current.get(numOfAppliances).setBounds(185, 30 + 20 * numOfAppliances, size.width, size.height);
    size = power.get(numOfAppliances).getPreferredSize();
    power.get(numOfAppliances).setBounds(245, 30 + 20 * numOfAppliances, size.width, size.height);

    add(description.get(numOfAppliances));
    add(volts.get(numOfAppliances));
    add(current.get(numOfAppliances));
    add(power.get(numOfAppliances));

    numOfAppliances++;
  }
  private void subtractAppliance()
  {
    int index = numOfAppliances - 1;
    description.remove(index);
    volts.remove(index);
    current.remove(index);
    power.remove(index);
  }
}
packagemain;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.Font;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
@抑制警告(“串行”)
公共级电气面板扩展JPanel
{
专用静态最终整数宽度=360;
专用静态最终内部高度=600;
私有数组列表描述;
专用阵列电压;
私有ArrayList电流;
私权主义;
私有JButton addButton;
私有JButton按钮;
专用JLabel[]标签;
私人电器;
公共配电盘()
{
setPreferredSize(新尺寸(宽度、高度));
设置聚焦(真);
requestFocus();
setLayout(空);
description=新建ArrayList();
伏特=新阵列列表();
当前=新的ArrayList();
power=新的ArrayList();
labels=new JLabel[]{new JLabel(“说明”),
新JLabel(“伏特”),
新JLabel(“当前”),
新JLabel(“Power”)};
维度大小=标签[0]。getPreferredSize();
标签[0]。收进边界(10,10,size.width,size.height);
大小=标签[1]。getPreferredSize();
标签[1]。立根(125,10,size.width,size.height);
大小=标签[2]。getPreferredSize();
标签[2]。立根(185,10,size.width,size.height);
size=标签[3]。getPreferredSize();
标签[3]。立根(245,10,size.width,size.height);
对于(int i=0;i<4;i++)
{
添加(标签[i]);
}
numf=0;
addAppliance();
setupaddandsubtract按钮();
}
专用void setupAddAndSubtractButtons()
{
插图插图=新插图(0,0,0,0);
addButton=新的JButton(“+”);
减法按钮=新的JButton(“-”);
addButton.立根(305,30,24,18);
减法按钮.立根(305,50,24,18);
减法按钮。设置可见(假);
addButton.setMargin(插图);
减法按钮。设置边距(插图);
addButton.addActionListener(新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
addAppliance();
}
});
subtractButton.addActionListener(新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件arg0)
{
减法器械();
}
});
添加(添加按钮);
添加(减法按钮);
addAppliance();
}
私有void tryToAdd()
{
addAppliance();
}
私有void addAppliance()
{
说明.增加(新的JTextField(10));
添加电压(新JTextField(5));
当前添加(新JTextField(5));
power.add(新JTextField(5));
维度大小=description.get(numOfAppliances.getPreferredSize();
描述.get(numfactories).setBounds(10,30+20*numfactories,size.width,size.height);
size=volts.get(numOfAppliances.getPreferredSize();
伏特.get(numf装置).setBounds(125,30+20*numf装置,尺寸.宽度,尺寸.高度);
size=current.get(numOfAppliances.getPreferredSize();
当前.get(numfactures).setBounds(185,30+20*numfactures,size.width,size.height);
size=power.get(numOfAppliances.getPreferredSize();
power.get(numfactories).setBounds(245,30+20*numfactories,size.width,size.height);
添加(description.get(numOfAppliances));
加上(伏特数)得到(numOfAppliances);
add(current.get(numOfAppliances));
添加(power.get(numOfAppliances));
NUMOF++;
}
私有设备()
{
int-index=numf-1;
说明.删除(索引);
移除(索引);
当前。删除(索引);
电源。移除(索引);
}
}
感谢您的任何帮助和建议

如果在单击按钮后调整窗口大小(当然可以调整大小),您会注意到按钮的单击正在生效。发生的情况是,您正在添加组件,但没有重新绘制面板。只需在
actionPerformed()
方法中添加一个
repaint()
调用,您的更改就会显示出来

这些方法在操作侦听器之外工作的原因是,方法调用发生在面板首次绘制之前。然而,当方法调用位于操作侦听器内部时,在绘制面板后,只要单击按钮,它们就会被执行


除此之外,您绝对不应该使用空布局,这将使您的GUI仅限于您正在使用的计算机。另外,请继续阅读。

非常感谢。我决定不使用布局,因为我觉得绝对定位是最好的,而且在本例中也是最容易实现的。与一些布局经理打交道很忙。我想FlowLayout会很好地配合这个,但我无法让它看起来像没有布局管理器一样好