作者:xjq520 | 来源:互联网 | 2023-12-13 22:09
本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。
public class cuowu extends JFrame implements ItemListener{
.........
}这样写有错吗?
为什么总是提示我类错误!还有就是我用到AdjustmentListener(还是同样的方法)。
16 个解决方案
请检查下面的问题:
1。你保存的文件名是cuowu.java吗?注意大小写!
2。这个类实现了public void itemStateChanged(ItemEvent e){};吗?
如果你有
import javax.swing.*;
import java.awt.event.*;
就可以这么用
public class cuowu extends JFrame implements ItemListener{
.........
}
有呀,程序如下:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class SelectItem extends JFrame implements ItemListener{
BorderLayout bord = new BorderLayout();
JTextField result = new JTextField(27);
JComboBox pick = new JComboBox();
public SelectItem(){
super("Select Item");
pick.addItemListener(this);
pick.addItem("aaaaaaa");
pick.addItem("bbbbb");
pick.addItem("ccccc");
pick.setEditable(false);
result.setHorizontalAlignment(SwingConstants.CENTER);
result.setEditable(false);
JPanel pane = new JPanel();
pane.setLayout(bord);
pane.add(result,"south");
pane.add(pick,"Center");
setContentPane(pane);
}
public static void main(String args[]){
SelectItem frm = new SelectItem();
frm.pack();
frm.setVisible(true);
}
public void itemstateChanged(ItemEvent evt){
Object source = evt.getSource();
if (source==pick){
Object newPick = evt.getItem();
result.setText(newPick.toString()+" is the selection.");
}
repaint();
}
}
运行是javac SelectItem.java 提示四行有类的错误!
不知道怎么回事
public void itemstateChanged(ItemEvent evt)
写错了
public void itemStateChanged(ItemEvent evt)
public void itemstateChanged(ItemEvent evt){
改为public void itemStateChanged(ItemEvent evt){
's'大写!
谢谢啊
又有新问题了:
C:\java>java SelectItem
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to lay
out: unknown constraint: south
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:437)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:399)
at java.awt.Container.addImpl(Container.java:627)
at java.awt.Container.add(Container.java:490)
at SelectItem.(SelectItem.java:19)
at SelectItem.main(SelectItem.java:24)
C:\java>
不明白是怎么回事!
pane.add(result,"south");
^^^^^
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WellAdjusted extends JFrame implements AdjustmentListener{
JTextField value = new JTextField("50",30);
JScrollBar bar = new JScrollBar(SwingConstants.HORIZONTAL,50,10,0,100);
public WellAdjusted(){
super("WellAdjusted");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350,100);
bar.addAdjustmentListener(this);
value.setHorizontalAlignment(SwingConstants.CENTER);
value.setEditable(false);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(value,"Center");
pane.add(bar,"South");
setContentPane(pane);
}
public static void main(String args[]){
WellAdjusted frm = new WellAdjusted();
frm.show();
}
public void adjustmentValueChange(AdjustmentEvent evt){
Object source = evt.getSource();
if (source==bar){
int newValue = bar.getValue();
value.setText(""+newValue);
}
repaint();
}
}
这个又是有什么毛病了?
还是第四行错误!
public void adjustmentValueChange(AdjustmentEvent evt){
改为
public void adjustmentValueChanged(AdjustmentEvent evt){
少个‘d’
我晕,大哥我建议你使用jcreator,出错了也知道是什么错误!!
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WellAdjusted extends JFrame implements AdjustmentListener{
JTextField value = new JTextField("50",30);
JScrollBar bar = new JScrollBar(SwingConstants.HORIZONTAL,50,10,0,100);
public WellAdjusted()
{
super("WellAdjusted");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(350, 100);
bar.addAdjustmentListener(this);
value.setHorizontalAlignment(SwingConstants.CENTER);
value.setEditable(false);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(value, "Center");
pane.add(bar, "South");
setContentPane(pane);
}
public void adjustmentValueChanged(AdjustmentEvent evt)
{
Object source = evt.getSource();
if (source == bar)
{
int newValue = bar.getValue();
value.setText("" + newValue);
}
repaint();
}
public static void main(String args[])
{
WellAdjusted frm = new WellAdjusted();
frm.show();
}
}
faint,两个错,
1、public void adjustmentValueChanged(AdjustmentEvent evt)
2、JFrame 没有 EXIT_ON_CLOSE,只有JFrame.DISPOSE_ON_CLOSE