先声明个JComboBox jcb;
然后初始化里 jcb = new JComboBox();
jcb.addActionListener(new Main_jcb_actionAdapter(this));使用过程中
File f1 = xxxx
File f2 = xxxx
jcb.addItem(f1);
jcb.addItem(f2);为什么在 jcb.removeAllItem();的时候出现空指针错误呢?
java.lang.NullPointerException想不明白了,救个命。
然后初始化里 jcb = new JComboBox();
jcb.addActionListener(new Main_jcb_actionAdapter(this));使用过程中
File f1 = xxxx
File f2 = xxxx
jcb.addItem(f1);
jcb.addItem(f2);为什么在 jcb.removeAllItem();的时候出现空指针错误呢?
java.lang.NullPointerException想不明白了,救个命。
解决方案 »
- mysql存储过程,循环插入接
- 诚心求教!程序最后一个无法解决的Bug!
- scjp的问题
- 如何在一个编辑框中,同时显示文字与图片?????
- 【Constructor】无参,一参,多参数构造器内部相互调用,如何优先选择?
- system.out.print(4|7);的输出结果是多少?为什么?
- 我考试的最后一道题! 我写的答案有一个错误 不知道怎么改 能帮帮我吗?
- 請教如何得到一個子符的內碼.
- (毕业设计呀,好惨!)两个JBulider应用程序(未打包)之间互相传递信息(比如说用户名,用户密码,登陆日期等等),除了用Socket,还有其他方法吗?(
- 什么地方能下到Install Anywhere?? 还有JET??
- 如何用java实现类似QQ自动隐藏窗体的功能
- 如何将某网页上的图片保存到本地??
没定义ComboBoxModel[code]
public class Main extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
ImageIcon image1 = new ImageIcon(insertjs.Main.class.getResource(
"openFile.png"));
ImageIcon image2 = new ImageIcon(insertjs.Main.class.getResource(
"closeFile.png"));
ImageIcon image3 = new ImageIcon(insertjs.Main.class.getResource("help.png"));
JLabel statusBar = new JLabel();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel3 = new JPanel();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
JPanel jPanel4 = new JPanel();
Border border1 = BorderFactory.createLineBorder(Color.white, 2);
JButton jButton7 = new JButton();
GridLayout gridLayout1 = new GridLayout();
TitledBorder titledBorder1 = new TitledBorder("");
TitledBorder titledBorder2 = new TitledBorder("");
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JComboBox jComboBox1;
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
JButton jButton10 = new JButton();
static JTextField jTextField1 = new JTextField();
JPanel jPanel5 = new JPanel();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
BorderLayout borderLayout3 = new BorderLayout();
BorderLayout borderLayout4 = new BorderLayout();
BorderLayout borderLayout5 = new BorderLayout();
FlowLayout flowLayout2 = new FlowLayout();
JLabel jLabel1 = new JLabel();
static JTextPane jTextPane1 = new JTextPane();
static JTextPane jTextPane2 = new JTextPane();
Border border2 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
Color.white, Color.white, new Color(103, 101, 98),
new Color(148, 145, 140));
Border border3 = new TitledBorder(border2, "111");
JPanel jPanel8 = new JPanel();
JPanel jPanel9 = new JPanel();
BorderLayout borderLayout6 = new BorderLayout();
BorderLayout borderLayout7 = new BorderLayout();
Border border4 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.white, new Color(148, 145, 140));
Border border5 = new TitledBorder(border4, "修改前");
Border border6 = BorderFactory.createEmptyBorder();
Border border7 = new TitledBorder(border6, "修改后");
Border border8 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.white, new Color(148, 145, 140));
Border border9 = new TitledBorder(border8, "修改前");
public Main() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
new Color(148, 145, 140)), "修改后");
border9 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,
new Color(148, 145, 140)), "修改前");
border5 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,
new Color(148, 145, 140)), "修改前");
Public_Var pv = new Public_Var();
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
this.setIconImage(null);
setSize(new Dimension(640, 480));
setTitle("代码嵌入工具");
statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
statusBar.setText(" ");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new Main_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new Main_jMenuHelpAbout_ActionAdapter(this));
jPanel1.setLayout(borderLayout2);
jPanel2.setLayout(gridLayout1);
jPanel3.setLayout(flowLayout1);
jButton4.setText("保存当前文件");
jButton4.addActionListener(new Main_jButton4_actionAdapter(this));
jButton5.setText("保存全部文件");
jButton5.addActionListener(new Main_jButton5_actionAdapter(this));
jButton6.setText(" 退出程序 ");
jButton6.addActionListener(new Main_jButton6_actionAdapter(this));
jPanel4.setLayout(borderLayout3);
jPanel2.setBorder(BorderFactory.createLoweredBevelBorder());
jButton7.setPreferredSize(new Dimension(83, 23));
jButton7.setText("添加文件");
jButton7.addActionListener(new Main_jButton7_actionAdapter(this));
jComboBox1 = new JComboBox();
jComboBox1.addActionListener(new Main_jComboBox1_actionAdapter(this));
jButton1.addActionListener(new Main_jButton1_actionAdapter(this));
jComboBox1.setMinimumSize(new Dimension(100, 21));
jComboBox1.setPreferredSize(new Dimension(33, 21));
jComboBox1.setToolTipText("");
jButton8.setPreferredSize(new Dimension(83, 23));
jButton8.setToolTipText("");
jButton8.setText("移除文件");
jButton8.addActionListener(new Main_jButton8_actionAdapter(this));
jButton9.setPreferredSize(new Dimension(83, 23));
jButton9.setText("重新选择");
jButton9.addActionListener(new Main_jButton9_actionAdapter(this));
jButton10.setPreferredSize(new Dimension(83, 23));
jButton10.setText("全部移除");
jButton10.addActionListener(new Main_jButton10_actionAdapter(this));
jButton2.addActionListener(new Main_jButton2_actionAdapter(this));
flowLayout1.setAlignment(FlowLayout.RIGHT);
jTextField1.setPreferredSize(new Dimension(72, 20));
jTextField1.setText("1234");
jTextField1.addActionListener(new Main_jTextField1_actionAdapter(this));
jPanel6.setLayout(borderLayout4);
jPanel5.setLayout(borderLayout5);
jPanel4.setMinimumSize(new Dimension(463, 30));
jPanel4.setPreferredSize(new Dimension(462, 25));
jPanel5.setMinimumSize(new Dimension(6, 20));
jPanel5.setPreferredSize(new Dimension(72, 20));
jPanel7.setMinimumSize(new Dimension(357, 25));
jPanel7.setPreferredSize(new Dimension(357, 26));
jPanel7.setLayout(flowLayout2);
flowLayout2.setVgap(1);
borderLayout5.setHgap(3);
borderLayout5.setVgap(3);
borderLayout4.setHgap(3);
borderLayout4.setVgap(3);
jLabel1.setText("MyID");
jTextPane1.setEditable(false);
jPanel8.setLayout(borderLayout6);
jPanel9.setLayout(borderLayout7);
jPanel8.setBorder(border7);
jPanel9.setBorder(border9);
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuFileExit);
jMenuBar1.add(jMenuHelp);
jMenuHelp.add(jMenuHelpAbout);
setJMenuBar(jMenuBar1);
jButton1.setIcon(image1);
jButton1.setToolTipText("打开文件");
jButton2.setIcon(image2);
jButton2.setToolTipText("关闭文件");
jButton3.setIcon(image3);
jButton3.setToolTipText("Help");
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jToolBar, java.awt.BorderLayout.NORTH);
contentPane.add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);
jPanel2.add(jPanel9);
jPanel2.add(jPanel8);
jScrollPane2.getViewport().add(jTextPane2);
jPanel1.add(jPanel3, java.awt.BorderLayout.SOUTH);
jPanel3.add(jButton4, null);
jPanel3.add(jButton5, null);
jPanel3.add(jButton6, null);
jPanel1.add(jPanel4, java.awt.BorderLayout.NORTH);
jPanel4.add(jPanel6, java.awt.BorderLayout.CENTER);
jPanel4.add(jPanel7, java.awt.BorderLayout.EAST);
jPanel4.add(jPanel5, java.awt.BorderLayout.WEST);
jPanel6.add(jComboBox1, java.awt.BorderLayout.CENTER);
jPanel7.add(jButton7);
jPanel7.add(jButton8);
jPanel7.add(jButton9);
jPanel7.add(jButton10);
jPanel5.add(jTextField1, java.awt.BorderLayout.CENTER);
jPanel5.add(jLabel1, java.awt.BorderLayout.WEST);
jPanel9.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel8.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextPane1);
} /**
* File | Exit action performed.
*
* @param actionEvent ActionEvent
*/
void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
* Help | About action performed.
*
* @param actionEvent ActionEvent
*/
void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
Main_AboutBox dlg = new Main_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
} public void jComboBox1_actionPerformed(ActionEvent e) {
fileChange();
} public void fileChange(){
File f = (File)this.jComboBox1.getSelectedItem();
if (f.exists()) {
try {
int size = (int) f.length();
if (size > 1024 * 1024 * 1) {
jTextPane1.setText("文件过大,建议人工修改!");
return;
}
int chars_read = 0;
FileReader fr = new FileReader(f);
char[] data = new char[size];
while (fr.ready()) {
chars_read += fr.read(data, chars_read, size - chars_read);
}
fr.close();
String tot = new String(data);
tot = tot.trim();
jTextPane1.setText(tot);
jTextPane1.setCaretPosition(0); if(tot.indexOf(functions.getJS(this.jTextField1.getText()))!=-1){
jTextPane2.setText(tot);
jTextPane1.setCaretPosition(tot.replaceAll("\r","").lastIndexOf(functions.getJS(this.jTextField1.getText())));
return;
}
int where = tot.replaceAll("\r","").lastIndexOf("</body>");
if(where==-1){
where = tot.replaceAll("\r","").length();
} StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, Color.black);
jTextPane2.setCharacterAttributes(aset, false);
jTextPane2.setText(tot); aset = sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, Color.red);
jTextPane2.setCaretPosition(where); //定位
jTextPane2.setCharacterAttributes(aset, false);
jTextPane2.replaceSelection(functions.getJS(this.jTextField1.getText()));
} catch (Exception ex) {
statusBar.setText("Error opening:" + ex);
System.out.println("select:" + ex);
}
} } public void jButton1_actionPerformed(ActionEvent e) {
chooseFiles(false);
} public void chooseFiles(boolean del) {
String ID = Main.jTextField1.getText();
if(!functions.isNum(ID)){
functions.messageBox("请正确输入您的唯一合法ID,确保嵌入代码的正确。");
return;
}
Public_Var.centerid=ID;
JFileChooser jfc = new JFileChooser();
jfc.setDialogType(JFileChooser.FILES_ONLY);
jfc.setMultiSelectionEnabled(true); ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension("txt");
filter.addExtension("htm");
filter.addExtension("html");
filter.addExtension("shtml");
filter.addExtension("shtm");
filter.addExtension("jsp");
filter.addExtension("asp");
filter.addExtension("php");
filter.addExtension("do");
filter.addExtension("aspx");
filter.setDescription("各种文本文档");
jfc.setFileFilter(filter); int result = jfc.showOpenDialog(this);
if (result == jfc.APPROVE_OPTION) {
if (del) { //重新添加全部
this.jComboBox1.removeAllItems();
}
File[] files = jfc.getSelectedFiles();
for (int i = 0; i < files.length; i++) {
this.jComboBox1.addItem(files[i].getAbsoluteFile());
if(i==files.length-1){
this.jComboBox1.setSelectedItem(files[i].getAbsoluteFile());
}
}
fileChange();
} }
public void jButton7_actionPerformed(ActionEvent e) {
chooseFiles(false);
} public void jButton9_actionPerformed(ActionEvent e) {
chooseFiles(true);
} public void jButton10_actionPerformed(ActionEvent e) {
removeall();
} public void jButton8_actionPerformed(ActionEvent e) {
remove();
} public void remove() {
if(jComboBox1.getItemCount()==1){
removeall();
}
else{
Object o = this.jComboBox1.getSelectedItem();
try {
this.jComboBox1.removeItem(o);
} catch (Exception e) {
System.out.println("remove():" + e);
}
}
} public void removeall() {
try{
//System.out.println(this.jComboBox1.isValid());
this.jComboBox1.removeAllItems();
jTextPane1.setText("");
jTextPane2.setText("");
}catch(Exception e){
System.out.println("removeall():"+e);
}
} public void jButton2_actionPerformed(ActionEvent e) {
remove();
} public void jTextField1_actionPerformed(ActionEvent e) { } public void jButton6_actionPerformed(ActionEvent e) {
if(functions.confirmBox("确认退出?")==0){
System.exit(0);
}
} public void jButton4_actionPerformed(ActionEvent e) {
if(this.jComboBox1.getItemCount()>0){
saveFile();
}
else{
functions.messageBox("当前未打开任何文件。");
}
}
public void saveFile(){
JFileChooser jfc = new JFileChooser();
jfc.setSelectedFile((File)this.jComboBox1.getSelectedItem());
if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String fcontent = Main.jTextPane2.getText();
if (f.canWrite()) {
try {
FileWriter fw = new FileWriter(f, true);
fw.write(fcontent);
fw.close();
} catch (Exception ex) {
System.out.println("saveFile:" + ex);
}
functions.messageBox("当前文件保存完毕。");
} else {
functions.messageBox("当前文件无法保存,请关闭其他操作本文件的程序。");
}
}
} public void saveAllFiles(){
for(int i=0;i<this.jComboBox1.getItemCount();i++){
try{
File f = (File)this.jComboBox1.getItemAt(i);
int size = (int) f.length();
if (size > 1024 * 1024 * 1) {
jTextPane1.setText("文件过大,建议人工修改!");
return;
}
int chars_read = 0;
FileReader fr = new FileReader(f);
char[] data = new char[size];
while (fr.ready()) {
chars_read += fr.read(data, chars_read, size - chars_read);
}
fr.close();
String tot = new String(data);
tot = tot.trim();
if(tot.indexOf(functions.getJS(this.jTextField1.getText()))!=-1){
continue;
}
int b = tot.lastIndexOf("</body>");
if(b!=-1){
String tot1=tot.substring(0,b);
String tot2=tot.substring(b);
tot = tot1 + functions.getJS(this.jTextField1.getText()) + tot2;
}
else{
tot = tot + functions.getJS(this.jTextField1.getText());
}
FileWriter fw = new FileWriter(f, true);
fw.write(tot);
fw.close();
}
catch(Exception e){
System.out.println("saveAllFiles():"+e);
}
}
functions.messageBox("修改完毕,共"+this.jComboBox1.getItemCount()+"个文件。");
} public void jButton5_actionPerformed(ActionEvent e) {
if(functions.confirmBox("确认保存所有打开文件?")==0){
saveAllFiles();
}
}
}说我 回复太快!如果你是恶意刷楼,将会受到严厉惩罚! ???
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at insertjs.Main.fileChange(Main.java:271)
at insertjs.Main.jComboBox1_actionPerformed(Main.java:266)
at insertjs.Main_jComboBox1_actionAdapter.actionPerformed(Main.java:623)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1197)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1268)
at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1288)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
at javax.swing.JComboBox.removeAllItems(JComboBox.java:745)
at insertjs.Main.removeall(Main.java:396)
at insertjs.Main.jButton10_actionPerformed(Main.java:372)
at insertjs.Main_jButton10_actionAdapter.actionPerformed(Main.java:575)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
处理后
程序不会崩溃,但再添加几个file到这个jComboBox里后,onchange事件就不响应了。