线程"AWT-EventQueue-0"中的java异常; java.lang.IllegalArgumentException [英] java exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
问题描述
我正在用Java swing尝试第一个代码,但遇到很多错误.我的代码是:
I was trying my first code in java swing and got many errors. my code is:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Swinging extends JFrame
{
JTextField ans;
int count =0;
static final long serialVersionUID = 1L;
Swinging()
{
Container cp= getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("value",7));
ans=new JTextField("0",10);
cp.add(ans);
JButton inc= new JButton("increment");
cp.add(inc);
inc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
++count;
ans.setText(count+"");
}
});
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Usingswing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Swinging(); // Let the constructor do the job
}
});
}
}
,错误如下:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: horizontalAlignment
at javax.swing.JLabel.checkHorizontalKey(Unknown Source)
at javax.swing.JLabel.setHorizontalAlignment(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at hopeso.Swinging.<init>(Usingswing.java:16)
at hopeso.Usingswing$1.run(Usingswing.java:45)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我尝试使用其他人发布的问题来解决我的问题,但是没有锻炼.请帮忙.
i tried solving my problem using the questions posted by other people but it didn't workout. please help.
推荐答案
由于以下行而发生错误:
The error occurs because of the following line:
cp.add(new JLabel("value",7));
您正在使用JLabel
的构造函数,该构造函数接收文本和水平对齐方式.对齐方式是int
,但是必须是以下常量之一,否则它将抛出IllegalArgumentException
:
You're using JLabel
's constructor that receives the text and the horizontal alignment. The alignment is an int
, but it has to be one of the following constants, otherwise it will throw the IllegalArgumentException
:
- 左(2)
- 中心(0)
- 右(4)
- 领先(10)
- 追踪(11)
这些常量在SwingConstants
中定义,因此您可以编写如下内容:
These constants are defined in SwingConstants
, so you can just write something like this:
cp.add(new JLabel("value", SwingConstants.CENTER));
这篇关于线程"AWT-EventQueue-0"中的java异常; java.lang.IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!