java 窗口

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ImageIcon;

import javax.swing.JPanel;
public class Login {

public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}

class LoginFrm extends JFrame implements ActionListener{
     JLabel nameLabel=new JLabel("用户名:");
     JLabel pwdLabel=new JLabel("密码:");
     JTextField name=new JTextField(10), password=new JTextField(10);
     JButton butnSure=new JButton("确定");
     JButton butnCancel=new JButton("取消");
public LoginFrm() {
     super("登陆");
     setBounds(500, 200, 280, 220);
     setVisible(true);
     setLayout(null);//在构造函数里设置布局管理器
     nameLabel.setBounds(45, 20, 100, 25);// x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 组件的长度 height:组件的
     add(nameLabel);
     add(name);
     name.setBounds(105, 20, 110, 25);
     add(pwdLabel);
     pwdLabel.setBounds(45, 60, 100, 25);
     add(password);
     password.setBounds(105, 60, 110, 25);
     add(butnSure);
     butnSure.setBounds(45, 100, 80, 25);
     add(butnCancel);
     butnCancel.setBounds(135, 100, 80, 25);
     butnSure.addActionListener(this);//加一个监听器
     butnCancel.addActionListener(this);//加一个监听器
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     validate();//刷新这是确保组件具有有效的布局。 不写这句代码,也没有问题的。
}

public void actionPerformed(ActionEvent e)
{
      if (e.getSource() ==butnSure)
      {                         //获取事件所作用的对象
        System.out.println("用户名:"+name.getText());
        System.out.println("密码:"+name.getText());
        if("love".equals(name.getText().trim())&&"12345".equals(password.getText().trim()))
        {  //trim()去掉字符串中的空格
           this.dispose();
           //new MainFrm("用户界面",name.getText().trim(),password.getText().trim());
           Inco ik = new Inco();
        }
        else
        {
          JOptionPane.showMessageDialog(this, "用户名或密码不对!");
         }
        }
      else if(e.getSource()==butnCancel)
      {
      System.exit(1);
      }
}

class MainFrm extends JFrame{
private JLabel info;

public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}

/*public class MainTest {

public static void main(String[] args) {
Inco ik = new Inco();
}

}*/
class Inco extends JFrame{//            加载图片用ImageIcon类  把此类弄到一个标签上 把标签弄到面板上

JButton[] buttons = new JButton[12];
public Inco()
{
super("Inco");
setSize(1000,1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
//这里我用了图片的绝对路径,根据你的图片写路径就可以了
ImageIcon ic = new ImageIcon("D:\5.jpg");
JLabel label=new JLabel();
label.setIcon(ic);
//panel.add(label);
//setContentPane(panel);
//show();
add(label);
setVisible(true);

}
}

原文地址:https://www.cnblogs.com/zhangdashuai/p/3443923.html