【Java】利用Swing实现登录页面
Main.java
class Main { public static void main(String[] args) { new FrameTest(); } }
FrameTest.java
package com.company; import javax.swing.*; import java.awt.*; class FrameTest { JFrame frame =new JFrame("登录"); Container c = frame.getContentPane();//创建视图 JLabel userLabel=new JLabel("用户名"); JTextField username= new JTextField(); JLabel passwdLabel=new JLabel("密码"); JPasswordField password=new JPasswordField(); JButton okbutton = new JButton("确定"); JButton cancelbttton = new JButton("取消"); public FrameTest(){ frame.setBounds(600, 200, 300, 220);//设置窗体位置&大小 c.setLayout(new BorderLayout());//设置视图的布局 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置❌功能 init();//初始化,把控件放在布局里 frame.setVisible(true);//设置窗体可见 } public void init(){ // 标题——上方 JPanel titlePanel =new JPanel();//创建一个放置标题的面板 titlePanel.setLayout(new FlowLayout()); titlePanel.add(new JLabel("欢哥OS")); c.add(titlePanel, "North");//加入视图中 // 输入框——中间 JPanel inputPanel =new JPanel(); inputPanel.setLayout(null); userLabel.setBounds(50, 20, 50, 20);//标签位置 passwdLabel.setBounds(50, 60, 50, 20); inputPanel.add(userLabel); inputPanel.add(passwdLabel); username.setBounds(110, 20, 120, 20); password.setBounds(110, 60, 120, 20); inputPanel.add(username); inputPanel.add(password); c.add(inputPanel, "Center"); // 按钮底部 JPanel buttonPanel =new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(okbutton); buttonPanel.add(cancelbttton); c.add(buttonPanel, "South"); } }
本文来自博客园,作者:木子欢儿,转载请注明原文链接:https://www.cnblogs.com/HGNET/p/15344996.html