永发信息网

运行以下Java程序,窗口只有一小块,需要拖动窗口才能显示出组件。

答案:1  悬赏:0  手机版
解决时间 2021-01-25 11:13
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());
}

}
}
最佳答案
在public logintest ()这个方法中加入如下语句即可

this.setBounds(200, 100, 300, 200); //置窗体位置及大小
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
奥博灯饰电工超市怎么去啊,我要去那办事
如何更换郎逸汽车燃油泵
sweet你们说这句英语单词是什么意思呢?
女人自己开店算女强人吗?
妊娠期急性胰腺炎的诊断依据A.腹痛B.黄疸C.血
国内船厂用的最好的造船管理软件(或者ERP)
找媳妇最主要的要看她的哪方面?
申请信用卡五天内用两个保险公司的单位 可以
胸下面长了一个像火疖子似的包怎么回事
广州市越秀区回民小学怎么样
你们觉得20岁女的晚上在家里尿,姑姑的儿子今
【2013高考时间】2013年福建高考化学题2013高
已知向量e1,e2,e3不共面,设a=2e1+e2+e3,b=e1+
2017广东理科高考454能否上广东工业大学
老宝来发动公里数自动清零
推荐资讯
泰坦网吧(旗舰店)地址有知道的么?有点事想过
我干医疗器械销售到现在为止已经5个月了,到
长期触闻烧香的味道可否有害健康?如题 谢谢
微云移动到保险箱里面,在标签还能看见照片怎
我买了一台桑塔纳3000二手车,现在着车就熄火
仁川是哪个地方的?
历时5个月的“寻找最美村官”大型公益活动,
‘月光下最美的誓言九十首爱我到永远,月光下
亲爱的翻译官电视剧李嘉怡和王旭东第几集在一
樱花会地址在哪,我要去那里办事
(18分)鸦片战争以后,中国面临着“三千年未
混凝土构件受拉裂缝的机理分析有哪三种
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?