Java 出于某种原因,动态更改程序中的Jlabel会导致它发送大量错误,但不会崩溃
当我尝试用'j1.setText(“”)动态更新我的jlabel1时它不工作,并导致它垃圾邮件出大量的错误,任何洞察这个问题的解决方案将不胜感激。出于测试目的,输入sin:130692544Java 出于某种原因,动态更改程序中的Jlabel会导致它发送大量错误,但不会崩溃,java,swing,jlabel,Java,Swing,Jlabel,当我尝试用'j1.setText(“”)动态更新我的jlabel1时它不工作,并导致它垃圾邮件出大量的错误,任何洞察这个问题的解决方案将不胜感激。出于测试目的,输入sin:130692544 import javax.swing.*; import java.awt.*; import java.awt.event.*; class SinChecker extends JFrame { //naming variables JTextField t1; static JLabel j, j
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SinChecker extends JFrame
{ //naming variables
JTextField t1;
static JLabel j, j1, j2, j3;
ButtonListener bl1;
ButtonListener2 bl2;
public SinChecker ()
{ //Get the container
Container c = getContentPane ();
//Set absolute layout
c.setLayout (null);
//Set Background Color
c.setBackground (Color.WHITE);
//Creating label Guess my number text
JLabel j = new JLabel ("Social Insurance Calculator");
j.setForeground (Color.BLUE);
j.setFont (new Font ("tunga", Font.BOLD, 24));
j.setSize (270, 20);
j.setLocation (30, 35);
//Creating label Enter a number.....
JLabel j1 = new JLabel ("Enter your S.I.N. below.");
j1.setFont (new Font ("tunga", Font.PLAIN, 17));
j1.setSize (270, 20);
j1.setLocation (66, 60);
//Creating a label Instuctions
JLabel j2 = new JLabel ("Enter a 9-digit Social Insurance Number");
j2.setFont (new Font ("tunga", Font.PLAIN, 17));
j2.setSize (270, 20);
j2.setLocation (10, 165);
//Creating a label Instuctions
JLabel j3 = new JLabel ("with no spaces between the digits please.");
j3.setFont (new Font ("tunga", Font.PLAIN, 17));
j3.setSize (270, 20);
j3.setLocation (10, 180);
//Creating TextField for x input guess
t1 = new JTextField (10);
t1.setSize (70, 30);
t1.setLocation (100, 80);
//creating 2 buttons
JButton b1 = new JButton ("Proceed");
b1.setSize (120, 30);
b1.setLocation (70, 200);
bl1 = new ButtonListener ();
b1.addActionListener (bl1);
JButton b2 = new JButton ("Re-enter");
b2.setSize (120, 30);
b2.setLocation (70, 250);
bl2 = new ButtonListener2 ();
b2.addActionListener (bl2);
//Place the components in the pane
c.add (j);
c.add (j1);
c.add (j2);
c.add (j3);
c.add (t1);
c.add (b1);
c.add (b2);
//Set the title of the window
setTitle ("Social Insurance Number Checker");
//Set the size of the window and display it
setSize (300, 350);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
//implement first action listener
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
int a = Integer.parseInt (t1.getText ());
boolean evenDigit = false; //alternates between true and false
int sum = 0; //accumulates the sum of the digits (as modified)
while (a > 0)
{
int nextDigit = a % 10; //grab the last digit
a = a / 10; //discard that digit
if (evenDigit)
{
//double it, then add the two digits of the result
nextDigit = 2 * nextDigit;
nextDigit = (nextDigit / 10) + (nextDigit % 10);
} // if(evenDigit)
sum = sum + nextDigit;
evenDigit = !evenDigit; //toggle the flag each time
} // end while
if (0 == sum % 10)
{
j1.setText ("That is a valid S.I.N.");
}
else
{
j1.setText ("That is not a valid S.I.N.");
}
t1.requestFocus ();
t1.selectAll ();
}
}
private class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//reset the label messages
t1.setText ("");
t1.requestFocus ();
t1.selectAll ();
}
}
public static void main (String[] args)
{
new SinChecker ();
}
}有两个问题
static
用户class SinChecker extends JFrame
{ //naming variables
// This is a BAD idea
static JLabel j, j1, j2, j3;
public SinChecker ()
{ //Get the container
//...
// Now what does SinCheck.j1 actually equal?
//Creating label Enter a number.....
JLabel j1 = new JLabel ("Enter your S.I.N. below.");
j1
在SinChecker
的构造函数中被重新声明为局部变量,使得SinCheker.j1
仍然null
即使您解决了这个问题,您也应该问自己这样一个问题:如果您创建了第二份SinCheck
,会发生什么?您现在引用的是哪个标签
首先删除构造函数中的静态
引用和标签声明
class SinChecker extends JFrame
{ //naming variables
private JLabel j, j1, j2, j3;
public SinChecker ()
{ //Get the container
//...
// Now what does SinCheck.j1 actually equal?
//Creating label Enter a number.....
j1 = new JLabel ("Enter your S.I.N. below.");
避免使用null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题
请仔细查看以了解更多详细信息有两个问题
static
用户class SinChecker extends JFrame
{ //naming variables
// This is a BAD idea
static JLabel j, j1, j2, j3;
public SinChecker ()
{ //Get the container
//...
// Now what does SinCheck.j1 actually equal?
//Creating label Enter a number.....
JLabel j1 = new JLabel ("Enter your S.I.N. below.");
j1
在SinChecker
的构造函数中被重新声明为局部变量,使得SinCheker.j1
仍然null
即使您解决了这个问题,您也应该问自己这样一个问题:如果您创建了第二份SinCheck
,会发生什么?您现在引用的是哪个标签
首先删除构造函数中的静态
引用和标签声明
class SinChecker extends JFrame
{ //naming variables
private JLabel j, j1, j2, j3;
public SinChecker ()
{ //Get the container
//...
// Now what does SinCheck.j1 actually equal?
//Creating label Enter a number.....
j1 = new JLabel ("Enter your S.I.N. below.");
避免使用null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题
请仔细查看以了解更多详细信息您正在方法中重新声明JLabel。您已经将它们声明为字段。它们被视为局部变量。 替换: 与
您正在方法中重新声明JLabel。您已经将它们声明为字段。它们被视为局部变量。 替换: 与
static
和null
布局不是你的朋友它不是“垃圾邮件出一大堆错误”,它的null指针异常@j1.setText………请参阅,以了解我不再费心修复的问题。1)请参阅&2)始终复制/粘贴错误和异常输出<代码>静态和空
布局不是你的朋友它不是“垃圾邮件出一大堆错误”,它的空指针异常@j1.setText………请参阅,了解一个我不再费心修复的问题。1)请参阅&2)始终复制/粘贴错误和异常输出!我不是7分钟前说的吗?我的错。。我试图发布整个代码,但格式太多了。(我花了一些时间编辑)感谢一个t:D的简单修复,但就我的一生而言,我没有注意到它xD。再次感谢:我不是7分钟前才这么说吗?我的错。。我试图发布整个代码,但格式太多了。(我花了一些时间编辑)感谢一个t:D的简单修复,但就我的一生而言,我没有注意到它xD。再次感谢:谢谢D我会马上看容器内的组件布局。非常感谢!:D我会马上看容器内的组件布局。