| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 899 人关注过本帖
标题:这个程序怎么修改布局啊。
只看楼主 加入收藏
zwq5533
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-26
结帖率:0
收藏
已结贴  问题点数:20 回复次数:8 
这个程序怎么修改布局啊。
package study;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//import javax.swing.event.*;

public class CheckLogin extends JFrame implements ActionListener,ItemListener {
    JLabel user = new JLabel("用户:");
    JLabel pass = new JLabel("密码:");
    public String[] username = {"王平", "李四", "张三"};
    JComboBox users = new JComboBox(username);
    JButton login = new JButton("登录");
    JPasswordField password = new JPasswordField(10);
    //public char pw[] = {'d','d'};
    public CheckLogin(){
        JPanel pane = new JPanel();
        //Container cont = getContentPane();
        pane.add(user);
        pane.add(users);
        pane.add(pass);
        pane.add(password);
        pane.add(login);
        pane.setLayout(new FlowLayout());
        add(pane);
        password.addActionListener(this);
        users.addItemListener(this);
        login.addActionListener(this);
    }
    public static void main(String args[]){
        //Checklogin frame=new BorderLayoutTest();
        CheckLogin cl = new CheckLogin();
        cl.setTitle("用户登录页面");
        cl.setVisible(true);
        cl.setSize(500,300);
        cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public String setpwb(){
        int flag = 0;
        String pwb[] = {"123456", "112233", "123123"};
        String pw;
        while(true){
            if (users.getSelectedItem() == username[flag]){
                pw = pwb[flag];
                break;
            }
            flag++;
        }
        return pw;
    }
    public void itemStateChanged(ItemEvent ex) {
        // TODO Auto-generated method stub
    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String passw = new String(password.getPassword());
        if (e.getSource() == login){
            if (passw.equals(setpwb())){
                String dialogTitle = "提示";
                String dialogMessage = "登录成功!";
                int dialogType = JOptionPane.INFORMATION_MESSAGE;
                JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
                return;
            }
        String dialogTitle = "提示";
        String dialogMessage = "登录失败!";
        int dialogType = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
        }
    }
}




把这个程序该成  



用户名          名字



密码              输入密码的文本框     



       登陆   
搜索更多相关主题的帖子: password 密码 public package 
2013-05-26 20:07
zwq5533
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-26
收藏
得分:0 
我是个初学者。
2013-05-26 20:09
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:7 
同学,我帮你整合了一下 ,这样应该就可以了,不懂再问我。
 class CheckLogin extends JPanel implements ActionListener,ItemListener {
    GridBagConstraints c;//定义网格布局变量
    BorderLayout bor;
    JLabel user;
    JLabel pass;
    JComboBox users;
    JButton login;
    JPanel pane,pane1,pane2;
    JPasswordField password;
    public String[] username = {"王平", "李四", "张三"};
    public CheckLogin (){
        pane = new JPanel();
        pane1=new JPanel();
        pane2=new JPanel();
        user = new JLabel("用   户:");
        pass = new JLabel("密   码:");
        users = new JComboBox(username);
        users.setPreferredSize(new Dimension(200, 50));//这条语句是强行改变组件的大小 的,下同。
        login = new JButton("登录");
        login.setPreferredSize(new Dimension(200, 55));
        password = new JPasswordField(18);
        password.setPreferredSize(new Dimension(200, 50));
        pane.add(user);
        pane.add(users);
        pane1.add(pass);
        pane1.add(password);
        pane2.add(login);
      
        password.addActionListener(this);
        users.addItemListener(this);
        login.addActionListener(this);
        c=setGUI(0, 0, 100, 60, GridBagConstraints.CENTER);//这个是调用自定义的方法来设置网格布局,下同
        add(pane,c);
        c=setGUI(0, 2, 100, 60, GridBagConstraints.CENTER);
        add(pane1,c);
        c=setGUI(0, 6, 100, 60, GridBagConstraints.CENTER);
        add(pane2,c);
    }
    //我帮你定义了一个新的方法,用来布局的,以后要记住这个方法哦,很有用。
    public GridBagConstraints setGUI(int gridx,int gridy,int gridwidth,
            int gridheight,int anchor){
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=gridx;
        c.gridy=gridy;
        c.ipadx=c.ipady=0;
        c.insets=new Insets(5,5,5,5);
        c.gridwidth=gridwidth;
        c.gridheight=gridheight;
        c.anchor = anchor;
        return c;
    }
    public String setpwb(){
        int flag = 0;
        String pwb[] = {"123456", "112233", "123123"};
        String pw;
        while(true){
            if (users.getSelectedItem() == username[flag]){
                pw = pwb[flag];
                break;
            }
            flag++;
        }
        return pw;
    }
   
    public void itemStateChanged(ItemEvent ex) {
        // TODO Auto-generated method stub
    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String passw = new String(password.getPassword());
        if (e.getSource() == login){
            if (passw.equals(setpwb())){
                String dialogTitle = "提示";
                String dialogMessage = "登录成功!";
                int dialogType = JOptionPane.INFORMATION_MESSAGE;
                JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
                return;
            }
        String dialogTitle = "提示";
        String dialogMessage = "登录失败!";
        int dialogType = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
        }
    }
}
 class CheckLogins extends JFrame {//这里是设置一个窗体的显示类,用于显示
     Toolkit tk=Toolkit.getDefaultToolkit();
     Dimension d=tk.getScreenSize();
    public CheckLogins (){
        CheckLogin cl = new CheckLogin ();//这里调用前面的组件类,添加到窗体
        setTitle("用户登录页面");
        this.add(cl);//这里是添加到窗体的操作
        setSize(450,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
 public class Enter{//这是入口函数,调用显示窗体来显示
     public static void main(String[] args){
         CheckLogins fuck=new CheckLogins ();
         fuck.setVisible(true);
     }
 }

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-26 21:38
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:7 
可以用网格布局\绝对布局还有网格包布局实现

有心者,千方百计;无心者,千难万难。
2013-05-27 01:03
zwq5533
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-26
收藏
得分:0 
回复 3楼 Kingbox_tang
好像 有点错误。无法运行。
2013-05-28 18:13
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:0 
什么错,不是有界面出来了么?


旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-28 19:47
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:7 
简单搞了下,试试看
package swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//import javax.swing.event.*;

public class CheckLogin extends JFrame implements ActionListener,ItemListener
{
    //定义
    JLabel user,pass;
    JComboBox users;
    JButton login;
    JPasswordField password;
    JPanel pane,pane1;
    String[] username = {"王平", "李四", "张三"};
 
    //public char pw[] = {'d','d'};
    public CheckLogin()
    {
        //创建
        user = new JLabel("用户:",JLabel.CENTER);
        pass = new JLabel("密码:",JLabel.CENTER);
        login = new JButton("登录");
        password = new JPasswordField(15);
        users = new JComboBox(username);
        pane = new JPanel();
        pane1 = new JPanel();
        
        //布局
        this.add(pane);
        this.add(pane1,BorderLayout.SOUTH);
        
        pane.setLayout(new GridLayout(2,2));
        pane.add(user);
        pane.add(users);
        pane.add(pass);
        pane.add(password);
        
        pane1.add(login,JPanel.CENTER_ALIGNMENT);
        password.addActionListener(this);
        users.addItemListener(this);
        login.addActionListener(this);
    }
    public static void main(String args[]){
        //Checklogin frame=new BorderLayoutTest();
        CheckLogin cl = new CheckLogin();
        cl.setTitle("用户登录页面");
        cl.setVisible(true);
        cl.pack();
        cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public String setpwb(){
        int flag = 0;
        String pwb[] = {"123456", "112233", "123123"};
        String pw;
        while(true){
            if (users.getSelectedItem() == username[flag]){
                pw = pwb[flag];
                break;
            }
            flag++;
        }
        return pw;
    }
    public void itemStateChanged(ItemEvent ex) {
        // TODO Auto-generated method stub
    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String passw = new String(password.getPassword());
        if (e.getSource() == login){
            if (passw.equals(setpwb())){
                String dialogTitle = "提示";
                String dialogMessage = "登录成功!";
                int dialogType = JOptionPane.INFORMATION_MESSAGE;
                JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
                return;
            }
        String dialogTitle = "提示";
        String dialogMessage = "登录失败!";
        int dialogType = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog((Component)null, dialogMessage, dialogTitle, dialogType);
        }
    }
}
2013-05-30 00:19
zwq5533
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-26
收藏
得分:0 
回复 6楼 Kingbox_tang
没有  界面。如果你有软件的话 可以试试。
2013-06-02 13:05
zwq5533
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-5-26
收藏
得分:0 
回复 7楼 hsjjgm
可不可以把 文本框和密码框的大小 叫他们固定下来。
2013-06-02 13:10
快速回复:这个程序怎么修改布局啊。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038770 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved