jframe如何获取字符串数据的错误

时间:2018-03-01 12:34:22

标签: java swing jframe

我是java的新手这是我的第一个jframe类,我的任务是当有人输入字符串值或字符和某些信息时出错,这样我就能理解代码而不是复制粘贴但是现在我必须创建一个当用户输入字符串而不是整数值时显示用户友好消息的异常 这是我现在使用JFrame和所有内容的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyOwn extends JFrame{
    JLabel l1, l2, l3, l4;
    JTextField t1, t2;
    JButton b1;
    public MyOwn(){}
    public MyOwn(String s){
        super(s);
    }
    public void setComponents(){
        l1=new JLabel("Addition of Two Numbers");
        l2=new JLabel("Enter First Number");
        l3=new JLabel("Enter Second Numbers");
                l4=new JLabel();
        t1=new JTextField();
        t2=new JTextField();
        b1=new JButton("Add Value");
        setLayout(null);
        l1.setBounds(100,50,200,20);
        l2.setBounds(100,80,150,20);
        t1.setBounds(250,80,200,20);
        l3.setBounds(100,100,150,20);
        t2.setBounds(250,100,200,20);
        b1.setBounds(80,180,100,20);
        l4.setBounds(50,200,200,20);
        b1.addActionListener(new Handler());
        add(l1);
        add(l2);
        add(l3);
        add(l4);
        add(t1);
        add(t2);
        add(b1);
    }
    class Handler implements ActionListener{
            @Override
        public void actionPerformed(ActionEvent e){
            int a = Integer.parseInt(t1.getText());
            int b = Integer.parseInt(t2.getText());
            int r = a+b;
            l4.setText("Answer"+r);
        }
    }
    public static void main(String[] args){
        MyOwn MyJf=new MyOwn("text frame");
        MyJf.setComponents();
        MyJf.setSize(500,500);
        MyJf.setVisible(true);
        MyJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在异常时显示错误消息。像这样:

def delete(self, request, id_):
    repository = self.get_object(id_, owner=request.user)
    repository.delete()
    return Response(status=status.HTTP_204_NO_CONTENT, headers={"web_words": request.user.profile.web_words, "repo_words": request.user.profile.repo_words, "files": request.user.profile.files})