Java的一半JButtons停止在JPanel中工作

时间:2016-04-26 19:02:29

标签: java jpanel jbutton

我正在尝试使用jpanel中的Jbuttons进行单位转换程序。到目前为止,当我运行程序时,弹出第一个窗口,我选择“长度”。现在,它在每一侧显示8个按钮,选择其中一个按钮后,一半按钮将不再工作,只有每一侧的右列工作。

public class Gui extends JFrame {

private JButton Subject[] = new JButton[8];
private String SubjNames[] = {"Length", "Mass", "Currency", "Temperature", "Time", "Speed", "Data", "Cooking"};
private JButton Length1[] = new JButton[8];
private JButton Length2[] = new JButton[8];
private String LengNames[] = {"inches", "feet", "yards", "miles", "millimeters", "centimeters", "meters", "kilometers"};
private JTextField convertedFrom;
private JTextField amountFrom;
private JTextField convertedTo;
private JTextField amountTo;
private String from;
private String CTo;
private String ATo;
private int SubjectLocX = 40;
private int SubjectLocY = 50;
private int Length1LocX = 40;
private int Length1LocY = 150;
private int Length2LocX = 330;
private int Length2LocY = 150;
private int t = 0;

public Gui (){

    super("Converter");
    setLayout(null);

    System.out.println("yes");

    for (int i = 0; i<8; i++) {
    Subject[i] = new JButton(SubjNames[i]);
    Subject[i].setLocation(SubjectLocX,SubjectLocY);
    Subject[i].setSize(200,50);
    add(Subject[i]);
    if (i < 3) {
        SubjectLocX = 40;
        SubjectLocY += 100;
    } else if (i == 3) {
        SubjectLocX = 330;
        SubjectLocY = 50;
    } else if (i > 3) {
        SubjectLocY += 100;
        }
    }

    HandlerClass handler = new HandlerClass();

    for (int i = 0; i<8; i++) {
    Subject[i].addActionListener(handler);
    }


    for (int i = 0; i<8; i++) {
    Length1[i] = new JButton(SubjNames[i]);
    Length2[i] = new JButton(SubjNames[i]);
    }

    for (int i = 0; i<8; i++) {
        Length1[i].addActionListener(handler);
        Length2[i].addActionListener(handler);
        }

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600,500);
    setLocation(400,200);
    setVisible(true);
}

public void Step2() {

    for (int i = 0; i<8; i++) {
        remove(Subject[i]);
    }
    for (int i = 0; i<8; i++) {
        remove(Length1[i]);
        remove(Length2[i]);
    }

    HandlerClass handler = new HandlerClass();

    convertedFrom = new JTextField(from, 20);
    convertedFrom.setEditable(false);
    convertedFrom.setLocation(40,50);
    convertedFrom.setSize(200,30);
    add(convertedFrom);
    convertedTo = new JTextField(CTo, 20);
    convertedTo.setEditable(false);
    convertedTo.setLocation(330,50);
    convertedTo.setSize(200,30);
    add(convertedTo);
    amountFrom = new JTextField("amount", 20);
    amountFrom.setLocation(40,100);
    amountFrom.setSize(200,30);
    add(amountFrom);
    amountTo = new JTextField(ATo, 20);
    amountTo.setEditable(false);
    amountTo.setLocation(330,100);
    amountTo.setSize(200,30);
    add(amountTo);

    for (int i = 0; i<8; i++) {
        Length1[i].setLocation(Length1LocX, Length1LocY);
        Length1[i].setSize(90, 50);
        add(Length1[i]);
        if (i < 3) {
            Length1LocX = 40;
            Length1LocY += 100;
        } else if (i == 3) {
            Length1LocX = 150;
            Length1LocY = 150;
        } else if (i > 3) {
            Length1LocY += 100;
            }
        Length2[i].setLocation(Length2LocX, Length2LocY);
        Length2[i].setSize(90, 50);
        add(Length2[i]);
        if (i < 3) {
            Length2LocX = 330;
            Length2LocY += 100;
        } else if (i == 3) {
            Length2LocX = 440;
            Length2LocY = 150;
        } else if (i > 3) {
            Length2LocY += 100;
            }
    } 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600,600);
    setLocation(400,200);
    setVisible(true);
}
private class HandlerClass implements ActionListener {
    public void actionPerformed(ActionEvent event) {

        for (int i = 0; i<8; i++) {
            if (event.getSource() == Length1[i]) {
                from = event.getActionCommand();
            }
            if (event.getSource() == Length2[i]) {
                CTo = event.getActionCommand();
            }
        }

        Step2();
    }
}
}`

(注意:我在上面的代码中没有包含import或main方法,但是在实际程序中)。构造函数创建JButtons和actionlisteners。 step2()方法是创建和重新创建按钮大小和位置以及窗口的位置。 (长度1和长度2 JButton是这里搞乱的)。我不知道为什么有一半按钮在第二次停止工作后会停止工作。

2 个答案:

答案 0 :(得分:2)

您正在创建和添加JTextField UI项目,并在同一位置多次添加它们,并且永远不会删除它们:

  • convertedFrom
  • amountFrom
  • convertedTo
  • amountTo

这些将堆叠在之前或之下;用户可以向顶部文本添加文本,并且程序可以向下面的模糊文本写入或读取,这将阻止程序按预期运行。

答案 1 :(得分:2)

当您选择16个按钮中的一个时,将再次调用方法Step2(),这将重新定位您的按钮,而不重置您的位置值。当您在Step2()方法中重置locationvariables时,按钮将起作用。像这样:

step2()
   Length1LocX = 40;
   Length1LocY = 150;
   Length2LocX = 330;
   Length2LocY = 150;
   [rest of method....]

但是当然,每次点击按钮时你都不想调用step2()......