首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用类生成JFormattedTextField

用类生成JFormattedTextField
EN

Stack Overflow用户
提问于 2014-10-19 20:35:44
回答 1查看 50关注 0票数 0

我在这里遇到了麻烦,我试图创建一个创建JFormattedTextFields的类,它可以工作,但是我需要获得它的价值,那就是我得到NullPointerExeption的时候,有我的代码:

JDialog:

代码语言:javascript
复制
package br.edu.faculdadedosguararapes;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;

import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Det4x4 extends JDialog {

    private JFormattedTextField aL1C1;
    private JFormattedTextField aL1C2;
    private JFormattedTextField aL1C3;
    private JFormattedTextField aL1C4;
    private JFormattedTextField aL2C1;
    private JFormattedTextField aL2C2;
    private JFormattedTextField aL2C3;
    private JFormattedTextField aL2C4;
    private JFormattedTextField aL3C1;
    private JFormattedTextField aL3C2;
    private JFormattedTextField aL3C3;
    private JFormattedTextField aL3C4;
    private JFormattedTextField aL4C1;
    private JFormattedTextField aL4C2;
    private JFormattedTextField aL4C3;
    private JFormattedTextField aL4C4;
    private JTextField tfDeterminante;
    private GeraObjeto textField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Det4x4 dialog = new Det4x4();
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Det4x4() {
        setTitle("Matriz 4x4");
        setModalityType(ModalityType.APPLICATION_MODAL);
        setResizable(false);
        setBounds(100, 100, 487, 362);
        getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Matriz", TitledBorder.CENTER, TitledBorder.TOP, null, null));
        panel.setBounds(12, 12, 143, 130);
        getContentPane().add(panel);
        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        textField = new GeraObjeto();

        textField.novoFtf(aL1C1, panel);
        textField.novoFtf(aL1C2, panel);
        textField.novoFtf(aL1C3, panel);
        textField.novoFtf(aL1C4, panel);

        textField.novoFtf(aL2C1, panel);
        textField.novoFtf(aL2C2, panel);
        textField.novoFtf(aL2C3, panel);
        textField.novoFtf(aL2C4, panel);

        textField.novoFtf(aL3C1, panel);
        textField.novoFtf(aL3C2, panel);
        textField.novoFtf(aL3C3, panel);
        textField.novoFtf(aL3C4, panel);

        textField.novoFtf(aL4C1, panel);
        textField.novoFtf(aL4C2, panel);
        textField.novoFtf(aL4C3, panel);
        textField.novoFtf(aL4C4, panel);

        JPanel panelDet = new JPanel();
        panelDet.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Determinante", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
        panelDet.setBounds(12, 169, 125, 52);
        getContentPane().add(panelDet);

        tfDeterminante = new JTextField();
        tfDeterminante.setHorizontalAlignment(SwingConstants.CENTER);
        tfDeterminante.setEditable(false);
        tfDeterminante.setColumns(5);
        panelDet.add(tfDeterminante);

        JButton btnCalcular = new JButton("Calcular");
        btnCalcular.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                int a[][] = new int[4][4];  
                //CalculaDet determinante = new CalculaDet();

                a[0][0] = Integer.parseInt(textField.getFtfContent(aL1C1));
                /*a[0][1] = Integer.parseInt(textField.getFtfContent(aL1C2));
                a[0][2] = Integer.parseInt(textField.getFtfContent(aL1C3));
                a[0][3] = Integer.parseInt(textField.getFtfContent(aL1C4));
                a[1][0] = Integer.parseInt(textField.getFtfContent(aL2C1));
                a[1][1] = Integer.parseInt(textField.getFtfContent(aL2C2));
                a[1][2] = Integer.parseInt(textField.getFtfContent(aL2C3));
                a[1][3] = Integer.parseInt(textField.getFtfContent(aL2C4));             
                a[2][0] = Integer.parseInt(textField.getFtfContent(aL3C1));
                a[2][1] = Integer.parseInt(textField.getFtfContent(aL3C2));
                a[2][2] = Integer.parseInt(textField.getFtfContent(aL3C3));
                a[2][3] = Integer.parseInt(textField.getFtfContent(aL3C4));
                a[3][0] = Integer.parseInt(textField.getFtfContent(aL4C1));
                a[3][1] = Integer.parseInt(textField.getFtfContent(aL4C2));
                a[3][2] = Integer.parseInt(textField.getFtfContent(aL4C3));
                a[3][3] = Integer.parseInt(textField.getFtfContent(aL4C4));*/

                tfDeterminante.setText(String.valueOf(a[0][0]));

                //determinante.Determinante(a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2], a[2][0], a[2][1], a[2][2],
                    //  tfDiagonalPrincipal, tfDiagonalSecundaria, tfDeterminante);
            }
        });

        btnCalcular.setBounds(187, 154, 99, 23);
        getContentPane().add(btnCalcular);

        JButton btnLimpar = new JButton("Limpar");
        btnLimpar.setBounds(187, 189, 99, 23);
        getContentPane().add(btnLimpar);


    }
}

类:

代码语言:javascript
复制
package br.edu.faculdadedosguararapes;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class GeraObjeto {

        public JFormattedTextField novoFtf(JFormattedTextField nome, JPanel panel, int bound1, int bound2){
            nome = new JFormattedTextField();
            nome.setBounds(bound1, bound2, 22, 20);
            nome.setHorizontalAlignment(SwingConstants.CENTER);
            nome.setColumns(2);
            nome.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent arg0) {
                    char c = arg0.getKeyChar();
                    if (!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE || Character.toString(c).equals("-"))){
                        arg0.consume();
                    }
                }
            });

            panel.add(nome);
            return nome;
        }

        public JFormattedTextField novoFtf(JFormattedTextField nome, JPanel panel){
            nome = new JFormattedTextField();
            nome.setHorizontalAlignment(SwingConstants.CENTER);
            nome.setColumns(2);
            nome.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent arg0) {
                    char c = arg0.getKeyChar();
                    if (!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE || Character.toString(c).equals("-"))){
                        arg0.consume();
                    }
                }
            });

            panel.add(nome);
            return nome;
        }

        public String getFtfContent(JFormattedTextField nome){
            return nome.getText();
        }

}

为了测试,我注释了一些行,并尝试获取其中一个文本字段的值,但我得到了:

代码语言:javascript
复制
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at br.edu.faculdadedosguararapes.GeraObjeto.getFtfContent(GeraObjeto.java:50)
    at br.edu.faculdadedosguararapes.Det4x4$2.actionPerformed(Det4x4.java:110)

我确信,我需要的是创建一种方法来设置JFT的值,但我现在不知道如何设置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-19 20:40:40

您正在创建一个新的JFormattedTextField,但是没有将新对象分配给类所持有的变量,因此所有变量都为null。实际上,您正在将对象赋值给novoFtf方法的参数,该方法不会自动将其分配给类所持有的JFormattedTextField变量。我想你可以:

代码语言:javascript
复制
aL1C1 = textField.novoFtf(aL1C1, panel);

其他问题:

  • 您的代码似乎不必要地复杂,使调试变得困难。我会重构和简化。
  • 如果您需要一个字段网格,使用一个数组或ArrayList,这将有助于我上面的建议,简化。
  • 您正在使用setBounds和null布局。我建议您不要这样做,因为这使得GUI变得非常不灵活,虽然它们在一个平台上看起来很好,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新和维护。相反,您需要学习和学习布局管理器,然后嵌套JPanels,每个布局管理器都使用自己的布局管理器来创建令人愉悦和复杂的GUI,这些GUI在所有操作系统上看起来都很不错。
  • 例如,矩阵字段应该由一个使用4乘4 JPanel的GridLayout保存,这个JPanel可以放在使用GridBagLayout的主JPanel中。
  • 避免将KeyListener与JTextComponent (如JTextField或JFormattedTextField )一起使用,因为这会导致重大且难以调试的问题。还有更好的方法来做这类事情,包括使用DocumentListener,DocumentFilter,InputVerifier.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26455126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档