编译无问题 但是运行后只有标题框在桌面左上角?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Login extends JFrame{
JLabel l1;
JLabel l2;
JTextField t1;
JTextField t2;
JButton b1;
JButton b2;
public void Log()
{
l1=new JLabel("账号");
l2=new JLabel("密码");
t1=new JTextField(10);
t2=new JTextField(10);
b1=new JButton("确定");
b2=new JButton("取消");
Container c=getContentPane();
setSize(300,200);
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3,Toolkit.getDefaultToolkit().getScreenSize().width/3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.add(l1);
c.add(t1);
c.add(l2);
c.add(t2);
c.add(b1);
c.add(b2);

}

}
class afkja
{
public static void main(String[] args)
{
Login log=new Login();
log.setVisible(true);
}
}

守着一只汪
浏览 115回答 3
3回答

jeck猫

public void Log()建议把这一句改成类的构造函数Login(){。。。。。。},如果对布局有要求,那么加个布局管理器c.setLayout(new FlowLayout());setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/3,Toolkit.getDefaultToolkit().getScreenSize().width/3);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);开头加个this.

largeQ

要这样写:log.setBounds(0, 0, 300, 300);log.setVisible(true);

翻阅古今

import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;class Login extends JFrame implements ActionListener{JLabel accountLabel=new JLabel("账号:");JLabel pwdLabel=new JLabel("密码:");JTextField name=new JTextField(10);JPasswordField password=new JPasswordField();JButton butnSure=new JButton("确定");JButton butnCancel= new JButton("取消");;public Login(String s) {super(s);setLayout(null);add(accountLabel);accountLabel.setBounds(45, 20, 100, 25);add(pwdLabel);pwdLabel.setBounds(45, 60, 100, 25);add(name);name.setBounds(105, 20, 110, 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);setVisible(true);setSize(300, 200);setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 3,Toolkit.getDefaultToolkit().getScreenSize().width / 6);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);validate();//刷新}public static void main(String[] args) {Login log = new Login("用户登陆");}public void actionPerformed(ActionEvent e) {if (e.getSource() ==butnSure){System.out.println("用户名:"+name.getText());System.out.println("密码:"+name.getText());if("wyjwsj".equals(name.getText().trim())&&"12345".equals(password.getText().trim())){this.dispose();new MainFrm("用户界面",name.getText().trim(),password.getText().trim());}else {JOptionPane.showMessageDialog(this, "用户名或密码不对,用户名为 wyjwsj,密码为 12345");}}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();}}
打开App,查看更多内容
随时随地看视频慕课网APP