gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:06:18 26 4
gpt4 key购买 nike

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

框架仅与“开始”按钮一起出现。这是注册用户的登录屏幕,带有一个文本框和 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

最佳答案

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

最简单的方法是将 JPanel 与 FlowLayout 结合使用。阅读 Swing 教程中关于 How to Use FlowLayout 的部分用于工作示例。

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

关于java - 无法在单个框架中显示多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com