[英]java exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException
I was trying my first code in java swing and got many errors. 我正在用Java swing尝试第一个代码,但遇到很多错误。 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
}
});
}
}
and the errors are as follows: 错误如下:
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));
You're using JLabel
's constructor that receives the text and the horizontal alignment. 您正在使用
JLabel
的构造函数,该构造函数接收文本和水平对齐方式。 The alignment is an int
, but it has to be one of the following constants, otherwise it will throw the IllegalArgumentException
: 对齐方式是
int
,但必须是以下常量之一,否则它将抛出IllegalArgumentException
:
These constants are defined in SwingConstants
, so you can just write something like this: 这些常量是在
SwingConstants
中定义的,因此您可以编写如下代码:
cp.add(new JLabel("value", SwingConstants.CENTER));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.