0

我正在尝试使用 Java 进行一些练习,并创建类似计算器的东西来用希伯来语形成动词。我的异常不起作用,无论我在那里添加什么,如果按下第一个按钮并且 JTextField 为空,我希望有错误消息。我应该在 catch 中添加什么作为例外?我仍然是一个非常初学者,我自学 Java,所以我不知道 JFormatted 文本字段,我不知道如何使用它,我希望你能帮助我修改我的代码。

先感谢您

这是我的代码

import java.awt.*;

public class Grid extends JFrame {

public static void main (String args[]) {
    Grid frame = new Grid();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setResizable(false);
}

public Grid() {
    setLayout(new GridLayout(4,2));
    JLabel FirstLetter = new JLabel("First Letter");
    final JTextField fl = new JTextField();
    fl.setHorizontalAlignment(SwingConstants.CENTER);
    fl.setFont(new Font("David", Font.PLAIN, 24));
    JLabel SecondLetter = new JLabel("Second Letter");
    final JTextField sl = new JTextField();
    sl.setHorizontalAlignment(SwingConstants.CENTER);
    sl.setFont(new Font("David", Font.PLAIN, 24));
    JLabel ThirdLetter = new JLabel("Second Letter");
    final JTextField tl = new JTextField();
    tl.setHorizontalAlignment(SwingConstants.CENTER);
    tl.setFont(new Font("David", Font.PLAIN, 24));
    JButton btn = new JButton ("Click Here");
    final JTextField ans = new JTextField();
    ans.setEditable(false);
    ans.setHorizontalAlignment(SwingConstants.CENTER);
    ans.setFont(new Font("David", Font.PLAIN, 24));

    add(FirstLetter);
    add(fl);
    add(SecondLetter);
    add(sl);
    add(ThirdLetter);
    add(tl);
    add(btn);
    add(ans);

    btn.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent e) {
            String getfl, getsl, gettl;
            String kmts, bth,shva,chereq,dagesh,pastEnding;
            kmts = "ָ";
            bth = "ַ";
            shva = "ְ";
            chereq = "ִ";
            dagesh = "ּ";
            pastEnding = "תִּי";
            String conc, conc1,conc2,conc3;

            try {
                getfl = fl.getText(); // Getting the first letter entered by the user
                getsl = sl.getText(); // Getting the second letter entered by the user
                gettl = tl.getText(); // Getting the third letter entered by the user
                conc = getfl.concat(kmts.concat(dagesh)); // adding kmts and dagest to the first letter
                conc1 = getsl.concat(bth); // adding bth to the second letter
                conc2 = conc.concat(conc1.concat(gettl)); // adding third letter to first and second letters.
                conc3 = conc2.concat(pastEnding);
                ans.setText(conc3); // to show the result
            } catch (Exception e1) {

            }
        }
    });

}

}

4

0 回答 0