import javax.swing.*;
import java.awt.*;
import java.net.*;
class myFrame extends JFrame{
private JMenuBar bar = new JMenuBar();
private JLabel userName = new JLabel("用户名");
private JLabel userPassword = new JLabel("密 码");
private JTextField user = new JTextField(20);
private JPasswordField password = new JPasswordField(20);
private String menuName[][] = {
{"文件","打开","保存","关闭"},
{"编辑","复制","粘贴","放弃"},
{"退出","嗯"}};
public myFrame(){
this.setSize(300,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//实例化一个图像
URL url = this.getClass().getResource("/0001.png");
Image img = Toolkit.getDefaultToolkit().getImage(url);
this.setIconImage(img);
//添加菜单栏
this.setJMenuBar(bar);
for(int i =0 ;i<menuName.length ; i++){
JMenu menu = new JMenu(menuName[i][0]);
bar.add(menu);
for (int j=1;j<menuName[i].length ;j++ ){
menu.add(new JMenuItem(menuName[i][j]));
}
}
this.setLayout(new FlowLayout());
this.getContentPane().add(userName);
this.getContentPane().add(user);
this.getContentPane().add(userPassword);
this.getContentPane().add(password);
this.getContentPane().setBackground(Color.GREEN);
this.setVisible(true);
}
}
public class GuiTest
{
public static void main(String[] args)
{
new myFrame();
}
}
简单的可视化