我是java的新手。虽然我的代码太简单了,但我不规律地遇到了这个错误。我没有使用任何数组,我只有4个Jbuttons,一个Jtextpane没有执行任何事件!
private JToolBar bar = new JToolBar(JToolBar.HORIZONTAL);
JMenuBar _bar = new JMenuBar();
JTextPane _txt = new JTextPane();
JScrollPane _scr = new JScrollPane(_txt,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JButton _save = new JButton("Save");
JButton _cancel = new JButton("Cancel");
JButton _color = new JButton("Color");
JColorChooser _choser = new JColorChooser();
JButton _calculater = new JButton("Calculater");
JPanel _pnl = new JPanel();
Color c = getForeground();
//constructor
public NotePade() {
//set size and position
setSize(800,375);
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-(getSize().width/2), Toolkit.getDefaultToolkit().getScreenSize().height/2 - +(getSize().height/2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagLayout _grd = new GridBagLayout();
_pnl.setLayout(_grd);
_pnl.setBackground(Color.LIGHT_GRAY);
JMenu _file = new JMenu("File");
JMenuItem _open = new JMenuItem("Open");
JMenuItem _close = new JMenuItem("Close");
_file.add(_open);
_file.add(_close);
JMenu _edit = new JMenu("Edit");
JMenuItem _cut = new JMenuItem("Cut");
JMenuItem __copy = new JMenuItem("Copy");
_edit.add(_cut);
_edit.add(__copy);
_bar.add(_file);
_bar.add(_edit);
setJMenuBar(_bar);
GridBagConstraints _cons = new GridBagConstraints();
_cons.gridx=0;
_cons.gridy=0;
_cons.gridwidth=8;
_cons.gridheight=4;
_cons.weightx=80;
_cons.weighty=80;
_cons.fill=GridBagConstraints.BOTH;
_cons.anchor=GridBagConstraints.CENTER;
_txt.setSize(400, 400);
_txt.setFont(new Font("Times New Roman",Font.BOLD,22));
_txt.setForeground(Color.darkGray);
_grd.setConstraints(_scr, _cons);
_pnl.add(_scr);
_cons = new GridBagConstraints();
_cons.gridx=9;
_cons.gridy=0;
_cons.gridwidth=2;
_cons.gridheight=2;
_cons.weightx=20;
_cons.weighty=40;
_cons.fill=GridBagConstraints.NONE;
_cons.anchor=GridBagConstraints.CENTER;
_color.addActionListener(this);
_grd.setConstraints(_color, _cons);
_pnl.add(_color);
_cons = new GridBagConstraints();
_cons.gridx=9;
_cons.gridy=2;
_cons.gridwidth=2;
_cons.gridheight=2;
_cons.weightx=20;
_cons.weighty=40;
_cons.fill=GridBagConstraints.NONE;
_cons.anchor=GridBagConstraints.CENTER;
_grd.setConstraints(_calculater, _cons);
_pnl.add(_calculater);
_cons = new GridBagConstraints();
_cons.gridx=0;
_cons.gridy=5;
_cons.gridwidth=2;
_cons.gridheight=1;
_cons.weightx=30;
_cons.weighty=10;
_cons.fill=GridBagConstraints.NONE;
_cons.anchor=GridBagConstraints.CENTER;
_save.addActionListener(this);
_grd.setConstraints(_save, _cons);
_pnl.add(_save);
_cons = new GridBagConstraints();
_cons.gridx=2;
_cons.gridy=5;
_cons.gridwidth=2;
_cons.gridheight=1;
_cons.weightx=30;
_cons.weighty=10;
_cons.fill=GridBagConstraints.NONE;
_cons.anchor=GridBagConstraints.CENTER;
_cancel.addActionListener(this);
_grd.setConstraints(_cancel, _cons);
_pnl.add(_cancel);
getContentPane().add(_pnl);
setVisible(true);
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at javax.swing.text.BoxView.updateLayoutArray(BoxView.java:214)
at javax.swing.text.BoxView.replace(BoxView.java:185)
at javax.swing.text.View.append(View.java:418)
at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:473)
at javax.swing.text.FlowView.layout(FlowView.java:201)
at javax.swing.text.BoxView.setSize(BoxView.java:397)
at javax.swing.text.BoxView.updateChildSizes(BoxView.java:366)
at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:348)
at javax.swing.text.BoxView.layout(BoxView.java:708)
at javax.swing.text.BoxView.setSize(BoxView.java:397)
at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1714)
at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:912)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1651)
at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1330)
at javax.swing.JViewport.getViewSize(JViewport.java:1002)
at javax.swing.plaf.basic.BasicScrollPaneUI.syncScrollPaneWithViewport(BasicScrollPaneUI.java:278)
at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.stateChanged(BasicScrollPaneUI.java:1033)
at javax.swing.JViewport.fireStateChanged(JViewport.java:1370)
at javax.swing.JViewport$ViewListener.componentResized(JViewport.java:1289)
at java.awt.Component.processComponentEvent(Component.java:6331)
at java.awt.Component.processEvent(Component.java:6285)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
提前致谢
答案 0 :(得分:1)
在代码更改的最后两行
getContentPane().add(_pnl);
// to
setContentPane(_pnl)
我遇到了同样的例外,并将代码更改为。它工作正常。