qiujialong
qiujialong
2010-08-20 18:11

请问我该如何处理这些异常

已采纳

各位好,小弟刚学java一个月,更是刚接触swing没几天,现遇到一些问题,希望大家能够帮忙指导下,下面的代码是学习班我们组刚开的一个项目,是做的航空公司柜员系统,我负责界面设计,因为我看了论坛上各位大大用SWING做的界面很帅~很羡慕,所以就自告奋勇接了下来,下面的代码是设置背景图片,改变外观的代码,编译无错,运行出界面,但也报了很多异常,小弟实在不知道怎么处理,望有人帮忙

package clientframe;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**

  • @author dj2041
  • @version V1.0
  • /
    public class CloginFrame {
    private JFrame mainFrame;
    private JPanel loginJpanel;
    private JLabel idJlabel;
    private JLabel passwordJlabel;
    private JTextField idTextField;
    private JPasswordField passwordField;
    private JButton enterButton;
    private JButton cancerButton;
    public CloginFrame() {
    mainFrame=new JFrame();
    idJlabel=new JLabel("工号");
    idTextField=new JTextField(15);
    passwordJlabel=new JLabel("密码");
    passwordField=new JPasswordField(15);
    enterButton=new JButton("确定");
    cancerButton=new JButton("取消");
    loginJpanel=new JPanel();
    ImageIcon img = new ImageIcon("背景.jpg");
    setBackground(mainFrame,img);
    init();
    eventhandler();
    }
    /
    *

    • @param frame 被设置的容器
    • @param img 被设置为背景的图片
      */
      public void setBackground(JFrame frame,ImageIcon img){
      JLabel imgLabel=new JLabel(img);//将图片放入一个JLabel中
      imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());//设置图片所在的JLabel的属性
      frame.getLayeredPane().add(imgLabel,new Integer(Integer.MIN_VALUE));//将图片添加到LayeredPane层
      ((JPanel) frame.getContentPane()).setOpaque(false);//使内容层透明
      }
      private void init(){
      mainFrame.setLayout(null);//设置为自定义布局
      mainFrame.add(loginJpanel);//添加登陆界面
      loginJpanel.add(idJlabel);
      loginJpanel.add(idTextField);
      loginJpanel.add(passwordJlabel);
      loginJpanel.add(passwordField);
      loginJpanel.add(enterButton);
      loginJpanel.add(cancerButton);//顺序添加组件
      loginJpanel.setSize(200, 120);//设置大小
      loginJpanel.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-120, Toolkit.getDefaultToolkit().getScreenSize().height/2+60);
      //根据分辨率设置登陆界面相对位置
      loginJpanel.setOpaque(false);//将登陆界面设置为透明
      }
      public void showMe(){

      mainFrame.setVisible(true);//设置frame 可见
      mainFrame.setLocation(0,0);//设置frame的位置

      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭按钮命令
      mainFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);//根据系统分辨率设置frame大小
      try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//设置外观风格
      SwingUtilities.updateComponentTreeUI(mainFrame);
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      public void eventhandler(){
      //?????
      cancerButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      System.exit(0);
      }

      });
      }
      public static void main(String[] args) {
      new CloginFrame().showMe();
      }
      }

异常如下:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthLookAndFeel.paintRegion(SynthLookAndFeel.java:387)
at javax.swing.plaf.synth.SynthLookAndFeel.update(SynthLookAndFeel.java:351)
at javax.swing.plaf.synth.SynthRootPaneUI.update(SynthRootPaneUI.java:77)
at javax.swing.JComponent.paintComponent(JComponent.java:763)
at javax.swing.JComponent.paint(JComponent.java:1027)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5122)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
at javax.swing.JComponent._paintImmediately(JComponent.java:5070)
at javax.swing.JComponent.paintImmediately(JComponent.java:4880)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • huntor huntor 11年前

    语句顺序问题。
    init方法中:
    移动
    mainFrame.add(loginJpanel);//添加登陆界面

    loginJpanel.setOpaque(false);//将登陆界面设置为透明

    showMe方法中:
    移动
    mainFrame.setVisible(true);//设置frame 可见
    到方法的末尾。

    这样修改后,运行时正常了

    点赞 评论 复制链接分享
  • oyj7454103 oyj7454103 11年前

    com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

    这个需要 JDK 6 把

    点赞 评论 复制链接分享

相关推荐