【问题标题】:What should I do about my buttons我应该怎么处理我的按钮
【发布时间】:2016-05-03 13:34:08
【问题描述】:

我对 Panel 上的输出有疑问。我想按钮不在面板上

我的 java 代码

for(int i = 0;i < 100;i++)
{
    if(buttonG[i] == null)
        buttonG[i] = new JButton();
}

int location = 5;

for(int i = 0;i < 10;i++)
{
    buttonG[i].setText("Group-"+(i+1));
    buttonG[i].setSize(80,50);
    buttonG[i].setLocation(location,10);
    buttonG[i].setVisible(true);

    location = location + 95;
    jPanel1.add(buttonG[i]);

    buttonG[i].addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {

        }
    });
}

输出:

【问题讨论】:

  • 你有一个固定的 X 坐标。改变它。
  • 请使用布局管理器。但是 y 总是相同的。为什么。
  • 您可能希望使用布局管理器来处理这个问题(FlowLayout 可以满足您的需求)。

标签: java button panel


【解决方案1】:
BtnAction action = new BtnAction();
for(int i = 0;i < 10;i++) {
      buttonG[i].addActionListener(action);
}



class BtnAction implement ActionListener{

      public void actionPerformed(ActionEvent e){
         if(e.getActionCommand==enter){
            //enterButtonCode. i assume that you have a btn named enter
        }
      {
}

您所做的并没有错,但是没有意义,因为您无法在 actionPerformed 方法中添加任何内容。创建一个实现 ActionListner 的内部类,并使用 addActionListner 方法传递 BtnAction 的对象。尝试按上述方法进行。

【讨论】:

    猜你喜欢
    • 2012-09-29
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多