【问题标题】:Looping Issues in JavaJava 中的循环问题
【发布时间】:2013-08-08 21:14:20
【问题描述】:

我正在制作一个简单的货币转换器 GUI(没什么花哨的),因为我将尝试在用户每次打开应用程序时合并实时汇率更新。在创建布局时,我决定简单转换 3 种货币(英镑、美元和欧元)。我在 2 列中有各自的标志,每列都有 3 个标志之一。一栏供用户选择初始货币,另一栏是想要兑换的货币;如下图

我创建了一个字符串数组,其中包含“英镑”、“美元”和“欧元”这几个词,我想将这些标签放在标志的左侧(为了用户的应用程序清晰,不是每个用户可能知道哪种货币属于哪个国家。

我创建了一个循环,它将创建一个标签并将其分配到标志的左侧,它应该制作一个“磅”标签,然后是一个“美元”,然后是一个“欧元”,每次穿过 Y 轴向南以便它们与标志对齐,然后它将重置数组计数以返回正确的字符串,沿 x 轴移动并再次重复。然而,它根本没有这样做,我得到的唯一结果是第一面英国国旗左侧的文字“磅”;如下图:

下面是我的代码,如果有人知道为什么会发生这种情况。

这是将标志添加到面板的代码

    addToMain(GBP1, mainPage, 100,100,100,100); //alligns a United Kingdom Flag to left Column
    addToMain(GBP2, mainPage, 375,100,100,100); //alligns a United Kingdom Flag to right Column
    addToMain(USD1, mainPage, 100,200,100,100); //alligns a United States Flag to left Column
    addToMain(USD2, mainPage, 375,200,100,100); //alligns a United States Flag to right Column
    addToMain(EUR1, mainPage, 100,300,100,100); //alligns a European Union Flag to left Column
    addToMain(EUR2, mainPage, 375,300,100,100); //alligns a European Union Flag to right Column

这是应将文本标签添加到标志左侧的循环

    currencyName = new String [] {"Pounds", "Dollars", "Euros"};

    for(int i = 0; i <= 7; i++)
    {
        int count = 0; //declares a counter for the position in the currencyName array to grab the correct text for label
        xLabelAlign = 50;
        yLabelAlign = 100;

        if(count == 3)
          {
              count = 0; //resets to create both columns of labels in the application moves to the next column.
              xLabelAlign = 325;
              yLabelAlign = 100;
          }

          JLabel temp = new JLabel(currencyName[count]); //creates a new label and names it the string at position count
          temp.setFont(new Font("SERIF", Font.BOLD, 20));
          temp.setForeground(Color.WHITE);
          addToMain(temp, mainPage, xLabelAlign, yLabelAlign ,100,100); //adds it to the panel 

          yLabelAlign +=100; //moves position ready for the next text label.
          count++;  //grabs the next label in the currencyName string array.         

    }

这是向面板添加内容的方法。我已经使用 set bounds 方法将东西添加到面板中,以便我可以轻松地将它们定位在我想要的位置

  private void addToMain(JComponent c, JPanel p, int x, int y, int w, int h)
{
    c.setBounds(x,y,w,h);
    p.add(c);
}

任何帮助将不胜感激。

【问题讨论】:

  • R.I.P.布局管理器
  • 忘记了setBounds(x, y, w, h),改用LayoutManager。这是Oracle tutorial :)
  • 你应该使用LayoutManager,你只是用这种方式在你的屏幕上编程;)
  • hmm,有些东西告诉我应该看看布局管理器,我没有设置边界,可能会让这里的一些人身体不适。

标签: java arrays loops user-interface count


【解决方案1】:

快速解决方案:将您的 int count = 0; xLabelAlign = 50; yLabelAlign = 100; 移出 for 循环。在 [0,5] 范围内循环。

好的解决方案:Java layouts tutorial

【讨论】:

  • 我认为xLabelAlign也是如此。
  • 已编辑。无论如何,我希望作者能够自己修复其他代码,因为我怀疑所有这些数字是否正确,并将标签放置在正确的位置。
  • 我不得不将 xLabelAlign、yLabelAlign 和我的计数移到循环之外,它才能正常工作。不过,我现在会研究 LayoutManagers,看看我是否可以使用更实用的设计,感谢你们的帮助:) imgur.com/h3OjbZZ
  • 你也应该修复循环范围。即使在屏幕截图中,也可以看到第二列中的英镑和美元被添加了两次。
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多