无法在单个框架中显示多个按钮

时间:2015-10-02 15:52:11

标签: java jframe

我有点学习java。我试图使用JFrame在单个框架中显示多个按钮,但第二个按钮在屏幕上不可见。没有例外或错误发生。

框架仅显示“go”按钮。这是具有文本框和2个按钮的注册用户的登录屏幕。 未显示的“后退”按钮仅用于返回上一帧。

我认为最初问题出在setBounds函数上。 也可以通过设置按钮visible=true来尝试。这也似乎不起作用。 请帮我纠正下面的代码:

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Toolkit;

public class askuser implements ActionListener {

    int x, y, x1, y1;
    Font f;
    JLabel l1;
    JTextField user;
    JButton b1;
    JButton b2;
    RandomAccessFile file;
    //Enumeration<String> names;
    //Hashtable<String,String> tempdb=new Hashtable<String,String>();
    public JFrame fref;
    public JFrame frame;
    String str1, str2, str3, str4;
    FileWriter file_write_uid = null;
    int k = 0, count = 0;
    boolean flag = false;

    askuser(JFrame jfrm) {
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension d = tk.getScreenSize();
        x = d.width;
        y = d.height;
        System.out.println("in askuser.java");
        fref = jfrm;
        fref.setVisible(false);
        frame = new JFrame("now login");
        //declaration
        frame.setSize(x, y);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f = new Font("Arial", Font.PLAIN, 16);
        Container c = frame.getContentPane();
        Color c1 = new Color(243, 238, 207);
        Color c2 = new Color(227, 214, 134);
        c.setBackground(c2);
        l1 = new JLabel("Enter the user name :");
        user = new JTextField("");
        b1 = new JButton("go");
        b2 = new JButton("Back");
        l1.setFont(f);
        b1.setFont(f);
        b2.setFont(f);
        user.setFont(f);
        b1.setBackground(c1);
        b2.setBackground(c1);
        user.setBackground(c1);
        c.add(l1);
        c.add(user);
        c.add(b1);
        x1 = (int) ((x / 2.5) - 100);
        y1 = (int) ((y / 2.5) - 60);
        b1.addActionListener(this);
        b2.addActionListener(this);
        user.addActionListener(this);
        l1.setBounds(x1, y1, 150, 20);
        user.setBounds(x1 + 150, y1, 150, 40);
        b1.setBounds(x1 + 100, y1 + 150, 150, 40);
        b1.setBounds(x1 + 200, y1 + 150, 150, 40);

        frame.setVisible(true);
    }//constr

    public void actionPerformed(ActionEvent ae) {
        String what2 = ae.getActionCommand();
        if (what2.equals("Back")) {
            new RL(fref);
        }

        flag = false;

        System.out.println("in askuser.java ap()");

        str1 = user.getText();
        System.out.println("got login id" + str1);

        if (str1.equals("")) {//System.out.println("please enter a user name");
            CustomDialog myDialog = new CustomDialog(frame, true, "please enter a user name");
            flag = true;
            user.setText("");
        }//if

        if (flag == false) {
            try {

                file = new RandomAccessFile("rafDB.txt", "r");

                do {
                    str2 = file.readUTF();
                    str3 = file.readUTF();
                    str4 = file.readUTF();
                } while (!str1.equals(str2));

            }//try
            catch (Exception e) {
            }

            System.out.println(str1);
            System.out.println(str2);

            if (!str2.equals(str1)) {
                CustomDialog myDialog = new CustomDialog(frame, true, "user not exist");
                user.setText("");

            }//if

            try {

                if (str2.equals(str1)) {

                    file_write_uid = new FileWriter("store_uid.txt");

                    while (count < str2.length()) {
                        count++;
                        file_write_uid.write(str2.charAt(k));
                        k++;
                    }//while

                    //file_write_uid.write(str2.toCharArray());
                    new Pswddiv(str3.toCharArray(), frame);//pass the password of user
                }//if

            }//try
            catch (Exception e) {
            }

            try {
                file_write_uid.close();
                file.close();
            }//try
            catch (Exception e) {
            }

        }//flag if

    }//ap()

}

    //askuser

1 个答案:

答案 0 :(得分:1)

摆脱null布局和setBounds(...)方法。

最简单的方法是使用带有FlowLayout的JPanel。有关工作示例,请阅读How to Use FlowLayout上Swing教程中的部分。

从工作示例开始并进行更改。如果您遵循教程示例,您的代码将更好地构建。