GUI显示但不起作用

时间:2018-06-01 15:40:49

标签: java swing user-interface

我必须编写一些Java代码来创建GUI,这个想法就是这样。用户将输入两个数字然后他将选择一个radiobutton。如果他选择“zgjidh”,他应该点击一个按钮,根据按钮的计算类型进行计算。 “Fshij”按钮有点像重置以清除所有字段。如果他选择“稀释”,窗口将关闭。由于某种原因,它似乎不起作用。有人能帮助我吗?

   import java.awt.*;
   import java.awt.event.ActionListener;
   import java.awt.event.ItemEvent;
   import javax.swing.*;
   import java.awt.event.ActionEvent;

public class Gui extends JFrame implements ActionListener{
  JButton plus,minus,fshi;
  JTextField vn1,vn2,vrez;
  JLabel n1,n2,rezultati;
  JRadioButton zgjidh,dil;
  ButtonGroup grupi;

  public Gui(){
    setLayout(new FlowLayout());
    JPanel veri=new JPanel();
    veri.setLayout(new GridLayout(3,2));

    n1=new JLabel("Jep numrin e 1");
    veri.add(n1);

    vn1=new JTextField(10);
    veri.add(vn1);

    n2=new JLabel("Jep numrin e 2");
    veri.add(n2);

    vn2=new JTextField(10);
    veri.add(vn2);

    rezultati=new JLabel("Rezultati");
    veri.add(rezultati);

    vrez=new JTextField(10);
    veri.add(vrez);

    add(veri,BorderLayout.NORTH);

    JPanel qender=new JPanel();
    qender.setLayout(new GridLayout(2,1));

    zgjidh=new JRadioButton("Zgidh Veprimin");
    zgjidh.addActionListener(this);
    dil=new JRadioButton("Dil");
    dil.addActionListener(this);
    grupi=new ButtonGroup();

    grupi.add(zgjidh);
    grupi.add(dil);

    qender.add(zgjidh);
    qender.add(dil);

    add(qender,BorderLayout.CENTER);

    JPanel jug=new JPanel();
    jug.setLayout(new GridLayout(1,3));
    plus=new JButton("+");
    plus.addActionListener(this);
    jug.add(plus);

    minus=new JButton("-");
    minus.addActionListener(this);
    jug.add(minus);

    fshi=new JButton("Fshij");
    fshi.addActionListener(this);
    jug.add(fshi);

    add(jug,BorderLayout.SOUTH);

  }
  public void actionPerformed (ActionEvent e){ 
    double n1=Double.parseDouble(vn1.getText());
    double n2=Double.parseDouble(vn2.getText());
    if (e.getSource()==zgjidh) {
        if (e.getSource()==plus) {
            vrez.setText(""+(n1+n2));
        }
        else if (e.getSource()==minus) {
            vrez.setText(""+(n1-n2));
        }
        else if (e.getSource()==fshi) {
            vn1.setText("");
            vn2.setText("");
            vrez.setText("");
        }
    }
    else if(e.getSource()==dil) {
        System.exit(0);
    }
  }

  public static void main(String args[]) {
    Gui ob=new Gui();
    ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ob.setSize(500,500);
    ob.setVisible(true);
  }
}

1 个答案:

答案 0 :(得分:0)

能够解决它。这是修改后的代码

public void actionPerformed(ActionEvent e){ 
    double n1=Double.parseDouble(vn1.getText());
    double n2=Double.parseDouble(vn2.getText());
    if (zgjidh.isSelected()) {
        if (e.getSource()==plus) {
            vrez.setText(""+(n1+n2));
        }
        else if (e.getSource()==minus) {
            vrez.setText(""+(n1-n2));
        }
        else if (e.getSource()==fshi) {
            vn1.setText("");
            vn2.setText("");
            vrez.setText("");
        }
    }
    else if(dil.isSelected()) {
        System.exit(0);
    }
}