java下面代码因为只能显示文本框而label和button两个属性不会显示出来呢?希望帮我解决下我出错的原因?
mportjava.awt.Color;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt...
mport java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserLogin extends JDialog{
Toolkit toolkit;
int WIDTH = 400;
int HEIGHT = 400;
JLabel jl1,jl2,jl3;
JButton jb1,jb2;
JTextField jtName;
JPasswordField jpwd;
Font f1 = new Font("宋体",Font.PLAIN,16);
public static void main(String [] args) {
new UserLogin();
}
public UserLogin() {
this.setLayout(null);
//创建一个BackImage对象
BackImage bi = new BackImage();
//把位置确定下
bi.setBounds(0, 0, WIDTH, HEIGHT);
//不使用上下框
this.setUndecorated(true);
//this.add(bi);
Container ct = this.getContentPane();
ct.add(bi);
toolkit = Toolkit.getDefaultToolkit();
this.setSize(WIDTH, HEIGHT);
//确定JWindow的初始位置
int width = toolkit.getScreenSize().width/2;
int height = toolkit.getScreenSize().height/2;
this.setLocation(width - WIDTH/2, height - HEIGHT/2);
Icon pjl1 = new ImageIcon("images/loginBt.jpg");
//创建各个组件
jl1 = new JLabel("用户名:");
jl1.setFont(f1);
jl1.setBounds(60, 290, 150, 30);
this.add(jl1);
//文本框
jtName = new JTextField(20);
jtName.setFocusable(true);
jtName.setBounds(180, 190, 120, 30);
jtName.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹
this.add(jtName);
//员工号
jl2 = new JLabel("(或员工号)");
jl2.setForeground(Color.red);
jl2.setFont(f1);
jl2.setBounds(100,210,100,30);
this.add(jl2);
//密码label
jl3 = new JLabel("密码:");
jl3.setFont(f1);
jl3.setBounds(60, 240, 150, 30);
this.add(jl3);
//密码框
jpwd = new JPasswordField(20);
jpwd.setFocusable(true);
jpwd.setBounds(180, 240, 120, 30);
jpwd.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹
this.add(jpwd);
//登陆按钮
Icon piture = new ImageIcon("images/loginBt.jpg");
jb1 = new JButton(piture);
//jb1.setFont(f1);
jb1.setBounds(110, 360, 70, 30);
this.add(jb1);
//取消按钮
jb2 = new JButton("取消");
jb2.setFont(f1);
jb2.setBounds(180, 360, 70, 30);
this.add(jb2);
this.setVisible(true);
}
//内部类
private class BackImage extends JPanel{
Image im;
//构造函数
public BackImage(){
try {
im = ImageIO.read(new File("images/login.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//必须注意大小写
public void paintComponent(Graphics g) {
g.drawImage(im, 0, 0, 400, 400, this);
}
}
} 展开
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserLogin extends JDialog{
Toolkit toolkit;
int WIDTH = 400;
int HEIGHT = 400;
JLabel jl1,jl2,jl3;
JButton jb1,jb2;
JTextField jtName;
JPasswordField jpwd;
Font f1 = new Font("宋体",Font.PLAIN,16);
public static void main(String [] args) {
new UserLogin();
}
public UserLogin() {
this.setLayout(null);
//创建一个BackImage对象
BackImage bi = new BackImage();
//把位置确定下
bi.setBounds(0, 0, WIDTH, HEIGHT);
//不使用上下框
this.setUndecorated(true);
//this.add(bi);
Container ct = this.getContentPane();
ct.add(bi);
toolkit = Toolkit.getDefaultToolkit();
this.setSize(WIDTH, HEIGHT);
//确定JWindow的初始位置
int width = toolkit.getScreenSize().width/2;
int height = toolkit.getScreenSize().height/2;
this.setLocation(width - WIDTH/2, height - HEIGHT/2);
Icon pjl1 = new ImageIcon("images/loginBt.jpg");
//创建各个组件
jl1 = new JLabel("用户名:");
jl1.setFont(f1);
jl1.setBounds(60, 290, 150, 30);
this.add(jl1);
//文本框
jtName = new JTextField(20);
jtName.setFocusable(true);
jtName.setBounds(180, 190, 120, 30);
jtName.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹
this.add(jtName);
//员工号
jl2 = new JLabel("(或员工号)");
jl2.setForeground(Color.red);
jl2.setFont(f1);
jl2.setBounds(100,210,100,30);
this.add(jl2);
//密码label
jl3 = new JLabel("密码:");
jl3.setFont(f1);
jl3.setBounds(60, 240, 150, 30);
this.add(jl3);
//密码框
jpwd = new JPasswordField(20);
jpwd.setFocusable(true);
jpwd.setBounds(180, 240, 120, 30);
jpwd.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹
this.add(jpwd);
//登陆按钮
Icon piture = new ImageIcon("images/loginBt.jpg");
jb1 = new JButton(piture);
//jb1.setFont(f1);
jb1.setBounds(110, 360, 70, 30);
this.add(jb1);
//取消按钮
jb2 = new JButton("取消");
jb2.setFont(f1);
jb2.setBounds(180, 360, 70, 30);
this.add(jb2);
this.setVisible(true);
}
//内部类
private class BackImage extends JPanel{
Image im;
//构造函数
public BackImage(){
try {
im = ImageIO.read(new File("images/login.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//必须注意大小写
public void paintComponent(Graphics g) {
g.drawImage(im, 0, 0, 400, 400, this);
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询