- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里遇到麻烦了,我正在尝试创建一个创建 JFormattedTextFields 的类,它可以工作,但我需要获取它的值,那就是我得到 NullPointerExeption 的时候,这是我的代码:
JDialog:
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);
}
}
类(class):
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();
}
}
只是为了测试,我评论了一些行,并尝试获取其中一个文本字段的值,但我得到了:
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 的值,但我现在不知道如何设置。
最佳答案
您正在创建一个新的 JFormattedTextField,但您没有将新对象分配给您的类所保存的变量,因此所有变量均为 null。事实上,您将该对象分配给 novoFtf 方法的参数,该方法不会自动将其分配给该类持有的 JFormattedTextField 变量。我想你可以这样做:
aL1C1 = textField.novoFtf(aL1C1, panel);
其他问题:
关于java - 使用类生成 JFormattedTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26455126/
我有一个带有 DateFormat 的 JFormattedTextField。格式为“ddMMyy”。这种格式允许快速输入。当焦点丢失时,我希望字段中的文本更改为 LocalDate,因为这样更易于
嘿伙计们,我想知道是否可以将 JFormattedTextField 设置为具有电子邮件输入的自动格式。例如,当用户输入电子邮件地址时,我希望它接受如下内容:jsmith1@smith.com 但我希
我在代码中使用时间时遇到了一些麻烦: txtDauer = new JFormattedTextField(); txtDauer.setFormatterFactory(new DefaultFor
我想使用 JFormattedTextField 将 float 格式化为百分比值,允许输入从 0 到 100%(转换为 0.0f-1.0f),始终显示百分号并不允许任何无效字符。 现在我对 Numb
我有此代码,但无法正确获取MaskFormatter maskformatter MaskFormatter formatter = null; try { formatter = new
我正在扩展 JFormattedTextField 以添加监听器。我已经完成了这项工作,尽管这可能不是最好的方法。没有办法使用单个泛型构造函数吗? public class TimeLineTextC
我想获取 JFormattedTextField 的值并将其转换为字符串这是我的 JFormattedTextField 代码 这是我的代码: public void formattedTextFie
我有一个字符数组列表,还有一个返回字符串的 JTextField。我想知道如何只允许输入字符?或者也许我如何只允许传递字符串的第一个字母? 最佳答案 所以您只想允许输入某些字符,即由ArrayList
我正在做这个作业,制作一个解决数独的程序。我有一个带有 SudokuTextBox 网格扩展 JFormattedTextField 的面板。我有一个 MaskFormatter,因此每个文本框只接受
我正在尝试在 Beanshell 中创建一个简单的对话框 - 它应该读取三个可编辑文本字段的内容,并在按下按钮时相应地执行一个简单的任务。我完全被一个错误所困扰,我无法阅读某些字段中的文本。 代码如下
我有一个用于“名称”字段的 jFormattedTextFiled。我必须限制字段只能输入 25 个字符。如果输入更多,则必须显示一些消息...对于消息我可以使用 JOptionpane.. 我该怎么
我有一个正在使用代码初始化的 JFormattedTextField JFormattedTextField f = new JFormattedTextField(createFormatter()
假设JFormattedTextField已启用并包含格式化程序,有没有办法使其只读?使用JTextField,我们只需要提供一个自定义文档并在那里进行所有控制,但是使用JFormattedTextF
我需要一个限制为小数点后 3 位的 JTextField。经过搜索,我发现了 JFormattedTextField,它看起来很棒。现在的代码是: try { double aux = 25.
我的 GUI 程序中有以下 JFormattedTextField。 DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); JFormattedTex
如何编写 JFormattedTextField 代码以接受不带小数的货币格式? 我尝试到处寻找答案。具体来说,是 Oracle、Google 和 Code Ranch。 我的问题:如何对格式化文本字
我在这里遇到麻烦了,我正在尝试创建一个创建 JFormattedTextFields 的类,它可以工作,但我需要获取它的值,那就是我得到 NullPointerExeption 的时候,这是我的代码:
我正在使用 GUI 制作一个小型数独游戏,并为 JFormattedTextFields 使用 MaskFormatter: formatter = new MaskFormatter(s); f
我一直在学习更多与 Java GUI 相关的知识。但是,我遇到了一个问题,不确定如何解决。 我有一部分程序(如下所示,但不相关的代码已被删除),除了 JFormattedTextField 变量 (s
由于有人错误地将我的另一个问题( NumberFormat parse not strict enough )标记为重复,并且尽管我指出它是不同的但没有删除重复的标签,所以我将再次发布该问题以及我的解
我是一名优秀的程序员,十分优秀!