Java 边界布局循环。JPanel/swing

Java 边界布局循环。JPanel/swing,java,swing,loops,jpanel,border-layout,Java,Swing,Loops,Jpanel,Border Layout,我的循环和“边界布局”有问题。当我使用驱动程序编译并运行它时,add.west(etc)似乎正在被正在进行的add.west覆盖。我只剩下“南”面板中的第9个组件,“东”和“西”完全为空。如果我更改“for”(int I=0;I,一次只能在哪个位置有一个组件。 通过将另一个组件添加到同一位置(例如BorderLayout.WEST),可以删除以前的组件。这是BorderLayout类中的代码,清楚地表明每次添加WEST组件时,成员变量WEST都会获得新值,而旧值会丢失: if ("Center"

我的循环和“边界布局”有问题。当我使用驱动程序编译并运行它时,add.west(etc)似乎正在被正在进行的add.west覆盖。我只剩下“南”面板中的第9个组件,“东”和“西”完全为空。如果我更改“for”(int I=0;I,一次只能在哪个位置有一个组件。 通过将另一个组件添加到同一位置(例如BorderLayout.WEST),可以删除以前的组件。这是BorderLayout类中的代码,清楚地表明每次添加WEST组件时,成员变量
WEST
都会获得新值,而旧值会丢失:

if ("Center".equals(name)) {
    center = comp;
} else if ("North".equals(name)) {
    north = comp;
} else if ("South".equals(name)) {
    south = comp;
} else if ("East".equals(name)) {
    east = comp;
} else if ("West".equals(name)) {
    west = comp;
} else if (BEFORE_FIRST_LINE.equals(name)) {
    firstLine = comp;
} else if (AFTER_LAST_LINE.equals(name)) {
    lastLine = comp;
} else if (BEFORE_LINE_BEGINS.equals(name)) {
    firstItem = comp;
} else if (AFTER_LINE_ENDS.equals(name)) {
    lastItem = comp;
} else {
    throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
}
  }
}
其他职位也是如此

我不仅仅是在三个位置添加一个面板吗

不,循环中的代码太多了

1) 每次执行循环时,您将创建3个新面板

  • 应该在循环开始之前创建3个西、东和南面板
2) 然后在循环结束时,将这些面板中的每一个添加到主面板

  • 三个面板应添加到回路外部的主面板

只有一个组件可以添加到BorderLayout中的特定位置。因此,您只能有5个组件。当然,您可以始终将JPanel添加到任何可以包含任意多个组件的位置。为了尽快获得更好的帮助,请发布“只有一个组件可以添加到BorderLayout中的特定位置”"... 我不仅仅是在这三个位置添加了一个面板吗?也不要调用
setPreferredSize(…)
而是覆盖
getPreferredSize()
,而是仅当您绘制到
JComponent
图形
对象时,使用正确的
LayoutManager
和调用
pack()
JFrame
上,在将其设置为可见之前,但在添加组件之后。实际上,它们不会被删除。它们堆起来了。组件保留在其父容器中,并保留LayoutManager设置的最后一个边界。这通常会导致MouseeEvent的闪烁和许多问题s@GuillaumePolet,它们将从布局中删除,并且从不闪烁。你可能有不同的问题。组件保留在容器的子列表中,但它们会从LayoutManager中删除,因此不会被绘制。实际上,在大多数情况下,所有子组件都会被绘制,即使它们彼此重叠。在这种情况下,你只能期待奇怪的结果。-1,@ATrubka,组件没有被移除。如果在设计时添加多个构件,则每个构件的大小为(0,0)。调用布局管理器时,仅调整最后添加的组件的大小。因此,看起来只有最后一个组件被绘制。但是,如果在框架可见后添加另一个组件,则布局管理器将调整此组件的大小,但现在您有两个大小相同的组件,因此您将获得意外的结果(即,上一个组件绘制在上一个添加的组件之上)。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;

public class CoinPanel extends JPanel{
  private JButton buttons[] = new JButton[9];
  private JLabel multiplySign[] = new JLabel[9];
  private JLabel coinCount[] = new JLabel[9];
  String [] names= {"1c", "2c", "5c", "10c", "20c", "50c", "€1", "€2", "Reset"};
  int [] values= {1, 2, 5, 10, 20, 50, 100, 200, 0};

  public CoinPanel(){
    for (int i=0; i<8; i++){
      buttons[i] = new JButton(names[i]);
      buttons[i].addActionListener(new BtnListener());
      coinCount[i] = new JLabel("0", JLabel.CENTER);
      coinCount[i].setBorder(BorderFactory.createLineBorder(Color.black));
      multiplySign[i] = new JLabel ("x", JLabel.CENTER);
      setLayout (new BorderLayout());
      JPanel west= new JPanel();
      west.setBackground(Color.BLACK);
      JPanel east= new JPanel();
      east.setBackground(Color.RED);
      JPanel south= new JPanel();
      south.setBackground(Color.BLUE);

      if(i<4){
        west.add (buttons[i]);
        west.add (multiplySign[i]);
        west.add (coinCount[i]);
      }
      else if(i<8){
        east.add (buttons[i]);
        east.add (multiplySign[i]);
        east.add (coinCount[i]);
      } 
      else{
        multiplySign[i].setText("TOTAL");
        south.add (multiplySign[i]);
        south.add (coinCount[i]);
        south.add (buttons[i]);
      }
      add(west, BorderLayout.WEST);     
      add(east, BorderLayout.EAST);
      add(south, BorderLayout.SOUTH);
     }

    setPreferredSize (new Dimension(450,300));
  }
private class BtnListener implements ActionListener{
    public void actionPerformed (ActionEvent event){
      String [] text = new String[9];
      int [] intArray = new int [9];
      double sum =0;
      for (int i=0; i<(intArray.length-1); i++){
        if(event.getSource() == buttons[i]){
          text[i] = coinCount[i].getText();
          intArray[i]=Integer.parseInt(text[i]);
          intArray[i] = ((intArray[i]) +1);
          coinCount[i].setText(intArray[i] + "");
       }    
       if(event.getSource() == buttons[8]){
         coinCount[i].setText("0");
       }
       sum += (Integer.parseInt(coinCount[i].getText())*values[i]);
       NumberFormat nf = NumberFormat.getCurrencyInstance();
       coinCount[8].setText(nf.format(sum/100)+"");
      }
    }
  }
}
import javax.swing.*;

public class CoinSorter{
   public static void main(String[] args){ 
      JFrame frame = new JFrame ("Coin Counter Example");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      CoinPanel panel = new CoinPanel();
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}
if ("Center".equals(name)) {
    center = comp;
} else if ("North".equals(name)) {
    north = comp;
} else if ("South".equals(name)) {
    south = comp;
} else if ("East".equals(name)) {
    east = comp;
} else if ("West".equals(name)) {
    west = comp;
} else if (BEFORE_FIRST_LINE.equals(name)) {
    firstLine = comp;
} else if (AFTER_LAST_LINE.equals(name)) {
    lastLine = comp;
} else if (BEFORE_LINE_BEGINS.equals(name)) {
    firstItem = comp;
} else if (AFTER_LINE_ENDS.equals(name)) {
    lastItem = comp;
} else {
    throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
}
  }
}