to:javahomer(小猪) 我是在a中也有package的。确实编译以后也放到b的目录下面。我的问题是当我执行A的时候总是出现这样的错误。 Exception in thread "main" java.lang.NullPointerException at com.weity.PRJ.SampleTextFrame.buildLayout(SampleTextFrame.java:139) at com.weity.PRJ.SampleTextFrame.<init>(SampleTextFrame.java:85) at com.weity.PRJ.SampleTextFrame.main(SampleTextFrame.java:76) Press any key to continue...我用的是jcreator pro
这里是源码 //this is a test Example For java GUI /* * @(#)SampleTextFrame.java 1.2 2002-03-14 * * Copyright 2002 Weity Microsystems, Inc. All Rights Reserved. * * */ package com.weity.PRJ; //import com.weity.Tools.P; //import com.weity.PRJ.*;
/** * The <code>SampleTextFrame</code> class Extends JFrame. * <code>SampleTextFrame</code> can RUN For Font's Property , * as FontSize,FontColor,FontBold,FontItalic and so on.<p> * 1.0 is bad for Method * 1.1 is Interface For Method * 1.2 is Child Class For Method * * @author Weity * @author mail:[email protected] * @version 1.2, 2002-03-13 * @since PRJ1.0 *///1.1 Version interface FontListener{ public void fontChanged(java.awt.Font newFont); }; //1.2 Version class FontPropertiesFrame extends SampleTextFrame {
public static void main(String[] args){ FontPropertiesFrame fpf = new FontPropertiesFrame(); fpf.setVisible(true); }
public FontPropertiesFrame(){ super(); FontPropertiesPanel fontPanel = (FontPropertiesPanel)propertiesPanel; displayArea.setFont(fontPanel.getSelectedFont()); fontPanel.setFontListener(this); }
protected void createComponents(){ propertiesPanel = new FontPropertiesPanel(); super.createComponents(); } }; //1.2 Version//1.0 Version //public class SampleTextFrame extends JFrame{
public class SampleTextFrame extends JFrame implements FontListener { //1.0 And 1.1 Version //protected FontPropertiesPanel propertiesPanel; //1.2 Version protected JPanel propertiesPanel; protected JTextField sampleText; protected JLabel displayArea;
public static void main(String[] args){ //P p = new P(); SampleTextFrame stf = new SampleTextFrame(); stf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); stf.setVisible(true); }
public SampleTextFrame(){ super(); createComponents(); createDocumentListener(); buildLayout(); //1.2 Version needn't these //1.1 Version Add New //displayArea.setFont(propertiesPanel.getSelectedFont()); //propertiesPanel.setFontListener(this); //1.1 Version Add New //1.0 Version //refreshDisplayFont(); pack(); }
protected void createComponents(){ //1.0 Version //propertiesPanel = new FontPropertiesPanel(this); //1.1 Version //propertiesPanel = new FontPropertiesPanel(); //Current is 1.2 Version
sampleText = new JTextField(40); displayArea = new JLabel("Welcome to Font Worlds"); displayArea.setPreferredSize(new Dimension(200,75)); displayArea.setMinimumSize(new Dimension(200,75)); }
protected void handleDocumentUpdate(){ displayArea.setText(sampleText.getText()); } //1.0 Version /* public void refreshDisplayFont(){ displayArea.setFont(getSelectedFont()); }
public Font getSelectedFont(){ String name = propertiesPanel.getSelectedFontName(); int style = 0; style += (propertiesPanel.isBoldSelected()?Font.BOLD:0); style += (propertiesPanel.isItalicSelected()?Font.ITALIC:0); int size = propertiesPanel.getSelectedFontSize(); return new Font(name,style,size); } */
//1.1 Version public void fontChanged(Font newFont){ displayArea.setFont(newFont); }}
------------------------------------------------------- /* * @(#)FontPropertiesPanel.java 1.0 2002-03-13 * * Copyright 2002 Weity Microsystems, Inc. All Rights Reserved. * * */package com.weity.PRJ;import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;//import java.awt.GridBagConstraints/** * The <code>FontPropertiesPanel</code> class Extends JPanel. * <code>FontPropertiesPanel</code> can set Font's Property , * as FontSize,FontColor,FontBold,FontItalic and so on.<p> * * * @author Weity * @author mail:[email protected] * @version 1.1, 2002-03-13 * @since PRJ1.0 */
//1.1 Version /*public interface FontListener{ public void fontChanged(java.awt.Font newFont); }*/public class FontPropertiesPanel extends JPanel { protected JList nameList; protected JComboBox sizeBox; protected JCheckBox boldBox; protected JCheckBox italicBox; //1.0 Version //protected SampleTextFrame frame; //1.1 Version protected FontListener listener;
public final static int[] fontSizes ={10,12,14,18,24,32,48,64};
//1.0 Version //public FontPropertiesPanel(SampleTextFrame stf){ //1.1 Version public FontPropertiesPanel(){ super(); //1.0 Version //frame = stf; createComponents(); buildLayout(); }
protected void buildLayout(){ JLabel label; GridBagConstraints gbc = new GridBagConstraints(); GridBagLayout gbl = new GridBagLayout(); setLayout(gbl);
gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(5,10,5,10);
gbc.gridx = 0; label = new JLabel("Name:",JLabel.LEFT); gbl.setConstraints(label,gbc); add(label); label = new JLabel("Size:",JLabel.LEFT); gbl.setConstraints(label,gbc); add(label); gbl.setConstraints(boldBox,gbc); add(boldBox);
protected void createComponents(){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] names = ge.getAvailableFontFamilyNames(); nameList = new JList(names); nameList.setSelectedIndex(0); nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); nameList.addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event){ handleFontPropertyChange(); } } ); Integer sizes[] = new Integer[fontSizes.length]; for(int i= 0 ; i < sizes.length ; i++){ sizes[i] = new Integer(fontSizes[i]); } sizeBox = new JComboBox(sizes); sizeBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ handleFontPropertyChange(); } } ); boldBox = new JCheckBox("Bold"); boldBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ handleFontPropertyChange(); } } ); italicBox = new JCheckBox("Italic"); italicBox.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ handleFontPropertyChange(); } } );
} protected void handleFontPropertyChange(){ //1.0 Version //frame.refreshDisplayFont(); //1.1 Version listener.fontChanged(getSelectedFont()); }
//1.1 Version Add New public void setFontListener(FontListener f1){ listener = f1; }
public Font getSelectedFont(){ //Changed all Method at 1.1 String name = (String)(nameList.getSelectedValue()); int style = 0; style += (boldBox.isSelected() ? Font.BOLD:0); style += (italicBox.isSelected() ? Font.ITALIC:0); int size = ((Integer)(sizeBox.getSelectedItem())).intValue(); return new Font(name,style,size); } //1.1 Version Add New
//1.0 Version /* public String getSelectedFontName(){ return (String)(nameList.getSelectedValue()); }
public int getSelectedFontSize(){ return ((Integer)(sizeBox.getSelectedItem())).intValue(); }
public boolean isBoldSelected(){ return boldBox.isSelected(); }
public boolean isItalicSelected(){ return italicBox.isSelected(); } */ }
在a.java头部指定它的包
package com.weity.PRJ;
原理:使A和B在同一个包中
因此,A.java编译后就在example/com/weity/PRJ/的目录下生成A.class
所以,下面这样的形式,也能运行在example/A.java的程序
example/A.java
example/com/weity/PRJ/A.class
example/com/weity/PRJ/B.class
example/com/weity/PRJ/A-child.class等
我是在a中也有package的。确实编译以后也放到b的目录下面。我的问题是当我执行A的时候总是出现这样的错误。
Exception in thread "main" java.lang.NullPointerException
at com.weity.PRJ.SampleTextFrame.buildLayout(SampleTextFrame.java:139)
at com.weity.PRJ.SampleTextFrame.<init>(SampleTextFrame.java:85)
at com.weity.PRJ.SampleTextFrame.main(SampleTextFrame.java:76)
Press any key to continue...我用的是jcreator pro
另,如果是 JCreatorPro,它会自己根据包结构建立目录的,并把类编译到相应的目录中去。
顺便再问一下,你的 A.java 中的包信息可以改吧……
等你的源码
搞定!
一个小疏忽,我给你发回去了!
收信吧!
哦sorry,对不起,我忘了,那是一个打印类,无关紧要。
不过,你看了我的代码了?
我现在的实现是1.2,要代码复用,1.0里面是用了初始化。
不过那样就高耦合了。
这是《java专业编程指南》那本书里面的开头的例子。
你再帮帮忙看看。
//this is a test Example For java GUI
/*
* @(#)SampleTextFrame.java 1.2 2002-03-14
*
* Copyright 2002 Weity Microsystems, Inc. All Rights Reserved.
*
*
*/
package com.weity.PRJ; //import com.weity.Tools.P;
//import com.weity.PRJ.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
/**
* The <code>SampleTextFrame</code> class Extends JFrame.
* <code>SampleTextFrame</code> can RUN For Font's Property ,
* as FontSize,FontColor,FontBold,FontItalic and so on.<p>
* 1.0 is bad for Method
* 1.1 is Interface For Method
* 1.2 is Child Class For Method
*
* @author Weity
* @author mail:[email protected]
* @version 1.2, 2002-03-13
* @since PRJ1.0
*///1.1 Version
interface FontListener{
public void fontChanged(java.awt.Font newFont);
};
//1.2 Version
class FontPropertiesFrame extends SampleTextFrame {
public static void main(String[] args){
FontPropertiesFrame fpf = new FontPropertiesFrame();
fpf.setVisible(true);
}
public FontPropertiesFrame(){
super();
FontPropertiesPanel fontPanel = (FontPropertiesPanel)propertiesPanel;
displayArea.setFont(fontPanel.getSelectedFont());
fontPanel.setFontListener(this);
}
protected void createComponents(){
propertiesPanel = new FontPropertiesPanel();
super.createComponents();
}
};
//1.2 Version//1.0 Version
//public class SampleTextFrame extends JFrame{
public class SampleTextFrame extends JFrame implements FontListener { //1.0 And 1.1 Version
//protected FontPropertiesPanel propertiesPanel;
//1.2 Version
protected JPanel propertiesPanel;
protected JTextField sampleText;
protected JLabel displayArea;
public static void main(String[] args){
//P p = new P();
SampleTextFrame stf = new SampleTextFrame();
stf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stf.setVisible(true);
}
public SampleTextFrame(){
super();
createComponents();
createDocumentListener();
buildLayout();
//1.2 Version needn't these
//1.1 Version Add New
//displayArea.setFont(propertiesPanel.getSelectedFont());
//propertiesPanel.setFontListener(this);
//1.1 Version Add New
//1.0 Version
//refreshDisplayFont();
pack();
}
protected void createComponents(){
//1.0 Version
//propertiesPanel = new FontPropertiesPanel(this);
//1.1 Version
//propertiesPanel = new FontPropertiesPanel();
//Current is 1.2 Version
sampleText = new JTextField(40);
displayArea = new JLabel("Welcome to Font Worlds");
displayArea.setPreferredSize(new Dimension(200,75));
displayArea.setMinimumSize(new Dimension(200,75));
}
protected void createDocumentListener(){
AbstractDocument document = (AbstractDocument) sampleText.getDocument();
document.addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent event){
handleDocumentUpdate();
}
public void insertUpdate(DocumentEvent event){
handleDocumentUpdate();
}
public void removeUpdate(DocumentEvent event){
handleDocumentUpdate();
}
}
);
}
protected void buildLayout(){
Container pane = getContentPane();
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
pane.setLayout(gbl);
gbc.insets = new Insets(5,10,5,10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridx = 0;
BevelBorder bb = new BevelBorder(BevelBorder.RAISED);
TitledBorder tb = new TitledBorder(bb,"Font");
propertiesPanel.setBorder(tb);
gbl.setConstraints(propertiesPanel,gbc);
pane.add(propertiesPanel);
gbl.setConstraints(sampleText,gbc);
pane.add(sampleText);
gbl.setConstraints(displayArea,gbc);
pane.add(displayArea);
}
protected void handleDocumentUpdate(){
displayArea.setText(sampleText.getText());
}
//1.0 Version
/*
public void refreshDisplayFont(){
displayArea.setFont(getSelectedFont());
}
public Font getSelectedFont(){
String name = propertiesPanel.getSelectedFontName();
int style = 0;
style += (propertiesPanel.isBoldSelected()?Font.BOLD:0);
style += (propertiesPanel.isItalicSelected()?Font.ITALIC:0);
int size = propertiesPanel.getSelectedFontSize();
return new Font(name,style,size);
}
*/
//1.1 Version
public void fontChanged(Font newFont){
displayArea.setFont(newFont);
}}
/*
* @(#)FontPropertiesPanel.java 1.0 2002-03-13
*
* Copyright 2002 Weity Microsystems, Inc. All Rights Reserved.
*
*
*/package com.weity.PRJ;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;//import java.awt.GridBagConstraints/**
* The <code>FontPropertiesPanel</code> class Extends JPanel.
* <code>FontPropertiesPanel</code> can set Font's Property ,
* as FontSize,FontColor,FontBold,FontItalic and so on.<p>
*
*
* @author Weity
* @author mail:[email protected]
* @version 1.1, 2002-03-13
* @since PRJ1.0
*/
//1.1 Version
/*public interface FontListener{
public void fontChanged(java.awt.Font newFont);
}*/public class FontPropertiesPanel extends JPanel
{
protected JList nameList;
protected JComboBox sizeBox;
protected JCheckBox boldBox;
protected JCheckBox italicBox;
//1.0 Version
//protected SampleTextFrame frame;
//1.1 Version
protected FontListener listener;
public final static int[] fontSizes ={10,12,14,18,24,32,48,64};
//1.0 Version
//public FontPropertiesPanel(SampleTextFrame stf){
//1.1 Version
public FontPropertiesPanel(){
super();
//1.0 Version
//frame = stf;
createComponents();
buildLayout();
}
protected void buildLayout(){
JLabel label;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5,10,5,10);
gbc.gridx = 0;
label = new JLabel("Name:",JLabel.LEFT);
gbl.setConstraints(label,gbc);
add(label);
label = new JLabel("Size:",JLabel.LEFT);
gbl.setConstraints(label,gbc);
add(label);
gbl.setConstraints(boldBox,gbc);
add(boldBox);
gbc.gridx++;
nameList.setVisibleRowCount(3);
JScrollPane jsp = new JScrollPane(nameList);
gbl.setConstraints(jsp,gbc);
add(jsp);
gbl.setConstraints(sizeBox,gbc);
add(sizeBox);
gbl.setConstraints(italicBox,gbc);
add(italicBox);
}
protected void createComponents(){
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
nameList = new JList(names);
nameList.setSelectedIndex(0);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
nameList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
handleFontPropertyChange();
}
}
);
Integer sizes[] = new Integer[fontSizes.length];
for(int i= 0 ; i < sizes.length ; i++){
sizes[i] = new Integer(fontSizes[i]);
}
sizeBox = new JComboBox(sizes);
sizeBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
handleFontPropertyChange();
}
}
);
boldBox = new JCheckBox("Bold");
boldBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
handleFontPropertyChange();
}
}
);
italicBox = new JCheckBox("Italic");
italicBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
handleFontPropertyChange();
}
}
);
}
protected void handleFontPropertyChange(){
//1.0 Version
//frame.refreshDisplayFont();
//1.1 Version
listener.fontChanged(getSelectedFont());
}
//1.1 Version Add New
public void setFontListener(FontListener f1){
listener = f1;
}
public Font getSelectedFont(){
//Changed all Method at 1.1
String name = (String)(nameList.getSelectedValue());
int style = 0;
style += (boldBox.isSelected() ? Font.BOLD:0);
style += (italicBox.isSelected() ? Font.ITALIC:0);
int size = ((Integer)(sizeBox.getSelectedItem())).intValue();
return new Font(name,style,size);
}
//1.1 Version Add New
//1.0 Version
/*
public String getSelectedFontName(){
return (String)(nameList.getSelectedValue());
}
public int getSelectedFontSize(){
return ((Integer)(sizeBox.getSelectedItem())).intValue();
}
public boolean isBoldSelected(){
return boldBox.isSelected();
}
public boolean isItalicSelected(){
return italicBox.isSelected();
}
*/
}
propertiesPanel
如上,你在class FontPropertiesFrame用到它,那么你用的propertiesPanel在何处声明的?
是直接调用class SampleTextFrame的吗?
可是class SampleTextFrame里的propertiesPanel不是类变量啊!
我不知道这本书上原话是怎么说的,问题是变量声明了,你没有初始化就使用,肯定会导致NullPoint的Exception,这个和代码复用又什么关系啊?
但是通过它,我在那里调用的FontPropertiesPanel。大家看到了吗?
这不是声明了吗?
//1.2 Version
class FontPropertiesFrame extends SampleTextFrame {
protected void createComponents(){
propertiesPanel = new FontPropertiesPanel();
super.createComponents();
}
我现在得到了源书的代码正在研究中。
请各位再帮帮忙!
to:rinehart(rinehart)
你可能说得对,我的代码肯定有错误,在请努把力。
谢谢各位的回答。