运行以下Java程序,窗口只有一小块,需要拖动窗口才能显示出组件。
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclasslogintestexten...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class logintest extends JFrame
{
private JLabel userNameLabel;
private JLabel passwordLabel;
private JButton login;
private JButton cancel;
public static void main(String[] args)
{
new logintest();
}
public logintest ()
{
setTitle("登录进销存管理系统");
loginPanel loginPanel = new loginPanel();
loginPanel.setLayout(null);
loginPanel.setBounds(450,200,loginPanel.getWidth(), loginPanel.getHeight());
getContentPane().add(loginPanel);
userNameLabel = new JLabel();
userNameLabel.setText("用户名");
userNameLabel.setBounds(100,135,200,25);
loginPanel.add(userNameLabel);
passwordLabel = new JLabel();
passwordLabel.setText("密码");
passwordLabel.setBounds(100,165,200,25);
loginPanel.add(passwordLabel);
login = new JButton();
login.setText("登录");
login.setBounds(180, 195, 60, 25);
loginPanel.add(login);
cancel = new JButton();
cancel.setText("取消");
cancel.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
});
cancel.setBounds(260, 195, 60, 25);
loginPanel.add(cancel);
setVisible(true);
setResizable(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public class loginPanel extends JPanel
{
public ImageIcon icon;
public int width, height;
public loginPanel()
{
super();
icon = new ImageIcon("res/login.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
setSize(width, height);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, getParent());
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
public class logintest extends JFrame
{
private JLabel userNameLabel;
private JLabel passwordLabel;
private JButton login;
private JButton cancel;
public static void main(String[] args)
{
new logintest();
}
public logintest ()
{
setTitle("登录进销存管理系统");
loginPanel loginPanel = new loginPanel();
loginPanel.setLayout(null);
loginPanel.setBounds(450,200,loginPanel.getWidth(), loginPanel.getHeight());
getContentPane().add(loginPanel);
userNameLabel = new JLabel();
userNameLabel.setText("用户名");
userNameLabel.setBounds(100,135,200,25);
loginPanel.add(userNameLabel);
passwordLabel = new JLabel();
passwordLabel.setText("密码");
passwordLabel.setBounds(100,165,200,25);
loginPanel.add(passwordLabel);
login = new JButton();
login.setText("登录");
login.setBounds(180, 195, 60, 25);
loginPanel.add(login);
cancel = new JButton();
cancel.setText("取消");
cancel.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
});
cancel.setBounds(260, 195, 60, 25);
loginPanel.add(cancel);
setVisible(true);
setResizable(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public class loginPanel extends JPanel
{
public ImageIcon icon;
public int width, height;
public loginPanel()
{
super();
icon = new ImageIcon("res/login.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
setSize(width, height);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, getParent());
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询