学java的新手,还不会用myeclipse调试,在网上看了一个教GUI基础的视频,自己写代码时编译通过但运行时一大堆异常,不知道是哪里出错了,还望各位大哥指正!
以下是代码
package xiangmu;
import java.awt.*;
import javax.swing.*;
public class Txh5 extends JFrame{
JPanel jp1,jp2,jp3,jp4;
JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8;
JButton jb1,jb2,jb3,jb4,jb5;
JCheckBox jcb1,jcb2;
JTextField jtf1;
JPasswordField jpf1;
JTabbedPane jtp1;
public static void main(String[] args){
Txh5 tx=new Txh5();
}
Txh5(){
jl1=new JLabel(new ImageIcon("tupian/2345截图20151023035807.png"));
jl2=new JLabel("QQ账号",JLabel.CENTER);
jl3=new JLabel("密码",JLabel.CENTER);
jl4=new JLabel("清除密码",JLabel.CENTER);
jl4.setFont(new Font("宋体",Font.PLAIN,16));
jl5=new JLabel("<html><a href='www.qq.com'>申请密码保护</a>");
jl5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
jtf1=new JTextField(10);
jpf1=new JPasswordField(10);
jb1=new JButton(new ImageIcon("tupian/2345截图20151023035825_看图王.png"));
jb2=new JButton(new ImageIcon("tupian/2345截图20151023035825_看图王.png2"));
jb2=new JButton(new ImageIcon("tupian/2345截图20151023035825_看图王.png3"));
jb2=new JButton(new ImageIcon("tupian/2345截图20151023035825_看图王.png4"));
// jb5=new JButton("清除密码",JButton.CENTER);
jcb1=new JCheckBox("隐身登录");jcb2=new JCheckBox("记住密码");
jp1= new JPanel();
jp1.setLayout(new GridLayout(3,3));
jp1.add(jl2);jp1.add(jtf1);jp1.add(jb1);jp1.add(jl3);jp1.add(jpf1);jp1.add(jl4);jp1.add(jcb1);jp1.add(jcb2);jp1.add(jl5);
jp2=new JPanel();
jp2.setBackground(Color.blue);
jp3=new JPanel();
jp3.setBackground(Color.GREEN);
jtp1=new JTabbedPane();
jtp1.add("普通用户",jp1);add("QQ会员",jp2);add("管理员",jp3);
jp4=new JPanel();
jp4.add(jb2);jp4.add(jb3);jp4.add(jb4);
add(jl1,BorderLayout.NORTH);
add(jtp1,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setTitle("登录界面");
setSize(800,600);
setLocation(300,200);
setIconImage(new ImageIcon("tupian/2345截图20151022063025.png").getImage());
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
代码的效果就是这张图片:
还有老师讲课的正确代码:
import java.awt.*;
import javax.swing.*;
public class L5_12 extends JFrame
{
JLabel bq1; //北部
JButton an1,an2,an3; JPanel mb1;//南部
JTabbedPane xxk; //中部
JPanel mb2,mb3,mb4;
JLabel bq2,bq3,bq4,bq5;
JTextField wbk;
JPasswordField mmk;
JButton an4;
JCheckBox fxk1,fxk2;
public static void main(String[] args)
{
L5_12 lx=new L5_12();
}
public L5_12()
{
bq2=new JLabel("qq号码",JLabel.CENTER);
bq3=new JLabel("qq密码",JLabel.CENTER);
bq4=new JLabel("忘记密码",JLabel.CENTER);
bq4.setFont(new Font("宋体",Font.PLAIN,16));
bq4.setForeground(Color.BLUE);
bq5=new JLabel("<html><a href='www.qq.com'>申请密码保护</a>");
bq5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
wbk=new JTextField();
mmk=new JPasswordField();
an4=new JButton(new ImageIcon("image/an1.jpg"));
fxk1=new JCheckBox("隐身登陆");
fxk2=new JCheckBox("记住密码");
bq1=new JLabel(new ImageIcon("image/qqq.jpg")); //北部
mb1=new JPanel(); //南部
an1=new JButton(new ImageIcon("image/an2.jpg"));
an2=new JButton(new ImageIcon("image/an3.jpg"));
an3=new JButton(new ImageIcon("image/an4.jpg"));
xxk=new JTabbedPane(); //中部
mb2=new JPanel(); mb3=new JPanel();
mb3.setBackground(Color.BLUE);
mb4=new JPanel();
mb4.setBackground(Color.GREEN);
xxk.add("普通用户",mb2); xxk.add("QQ会员",mb3); xxk.add("管理员",mb4);
mb2.setLayout(new GridLayout(3,3));
mb1.add(an1); mb1.add(an2); mb1.add(an3);
mb2.add(bq2); mb2.add(wbk); mb2.add(an4); mb2.add(bq3); mb2.add(mmk);
mb2.add(bq4); mb2.add(fxk1); mb2.add(fxk2); mb2.add(bq5);
this.add(mb1,BorderLayout.SOUTH);
this.add(bq1,BorderLayout.NORTH);
this.add(xxk,BorderLayout.CENTER);
ImageIcon tp1=new ImageIcon("image/qq.jpg");
this.setIconImage(tp1.getImage());
this.setTitle("用户登录");
this.setSize(340,270);
this.setLocation(300,280);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
------解决思路----------------------
add("QQ会员",jp2);add("管理员",jp3);
改成 jtp1.add("QQ会员",jp2);jtp1.add("管理员",jp3);
jp4.add(jb2);jp4.add(jb3);jp4.add(jb4);
没有jb3,jb4实例
要不要这么马虎