英文:
Swing Application not appearing
问题
以下是您要翻译的内容:
嗨,我正在进行Java项目的开发,我需要为用户创建一个登录页面,当登录成功时,应该弹出一个提示框告诉他们登录已成功。然而,我遇到了一些问题,因为我的应用根本没有显示出来,我认为问题可能出在我编写的代码上。以下是代码:
public class UserLoginPage implements ActionListener {
// 在这里放置所有的JLabels、Frames和Buttons等
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel userLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
JTextField userText = new JTextField();
JTextField passwordText = new JTextField();
JButton loginButton = new JButton("Login");
// 用于成功登录的Label
JLabel success = new JLabel("Login Successful");
// 默认构造函数以添加frames和panels等
public UserLoginPage(){
panel.setLayout(null);
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
userText.setBounds(100,20,165,25);
panel.add(userText);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
loginButton.setBounds(10,80,80,25);
loginButton.addActionListener(new UserLoginPage());
panel.add(loginButton);
success.setBounds(10,110,300,25);
panel.add(success);
frame.setSize(500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserLoginPage();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
}
我认为问题可能出在loginButton.addActionListener(new UserLoginPage());
,但我可能是错误的,如果可以的话,请告诉我如何解决这个问题,谢谢。
英文:
Hi im am working on my Java project which I have to create a login page for users to enter, and when login is successful, an alert is supposed to tell them that the login was ok. However, I am having some troubles as my application is not showing up at all, I think it is something to do with the code that I have written. Below is the code:
public class UserLoginPage implements ActionListener {
//Put all JLabels,Frames and buttons here etc
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel userLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
JTextField userText = new JTextField();
JTextField passwordText = new JTextField();
JButton loginButton = new JButton("Login");
//Label for successful login
JLabel success = new JLabel("Login Successful");
//Default Constructor to add the frames and panels etc
public UserLoginPage(){
panel.setLayout(null);
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
userText.setBounds(100,20,165,25);
panel.add(userText);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
loginButton.setBounds(10,80,80,25);
loginButton.addActionListener(new UserLoginPage());
panel.add(loginButton);
success.setBounds(10,110,300,25);
panel.add(success);
//success.setText();
frame.setSize(500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserLoginPage();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
I think the problem lies with loginButton.addActionListener(new UserLoginPage());
But I might be wrong, do let me know of how to solve the problem, thank you.
答案1
得分: 4
你的程序中存在多个问题:
-
panel.setLayout(null);
和.setBounds(...)
。你不应该使用“null布局”,因为Swing必须处理多个PLAF(平台外观和感觉)、操作系统、屏幕尺寸和分辨率。你可能会遇到像这个问题一样的问题。像素完美的布局可能看起来是创建复杂UI的最简单方法,但事实并非如此,它会导致无休止的问题。相反,使用布局管理器或它们的组合。 -
loginButton.addActionListener(new UserLoginPage());
你每次都在创建一个新的程序实例,并且在每个实例中,你都在构造函数内创建一个新对象。不要这样做!这是一个递归调用,最终会导致java.lang.StackOverflowError
错误。为了解决这个问题,使用this
代替new UserLoginPage()
-
frame.setVisible(true);
这行代码应该始终是程序中的最后一行,在你将所有内容添加到JFrame
之后,而不是之前。
根据上述建议,这是你代码的更新版本:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UserLoginPage implements ActionListener {
// 在这里放置所有的JLabels、Frames和buttons等
private JPanel panel;
private JFrame frame;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userText;
private JTextField passwordText;
private JButton loginButton;
// 用于成功登录的Label
private JLabel success;
// 默认构造函数,用于添加frames和panels等
public UserLoginPage() {
}
private void createAndShowGUI() {
frame = new JFrame(getClass().getSimpleName());
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
userLabel = new JLabel("Username");
passwordLabel = new JLabel("Password");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("Login");
success = new JLabel("Login Successful");
loginButton.addActionListener(this);
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
frame.add(panel);
frame.add(success, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserLoginPage().createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
}
英文:
There are multiple issues in your program:
-
panel.setLayout(null);
and.setBounds(...)
. You shouldn't be usingnull-layouts
, as Swing has to deal with multiple PLAFs, OS, screen sizes and resolutions. You might end up with issues like this one. Pixel-perfect layouts might seem like the easiest way to create complex UIs but it's not, it'll just lead you to endless issues. Instead use layout managers or combinations of them. -
loginButton.addActionListener(new UserLoginPage());
You're creating a new instance of your program every time, and on every instance of it you're creating a new object because all your code is inside the constructor. Just, don't! It's a recursive call that finally creates ajava.lang.StackOverflowError
, to solve this usethis
instead ofnew UserLoginPage()
-
frame.setVisible(true);
this line should always be the last line in your program, after you've added everything to yourJFrame
, not before.
With the above recommendations, here's an updated version of your code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UserLoginPage implements ActionListener {
// Put all JLabels,Frames and buttons here etc
private JPanel panel;
private JFrame frame;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userText;
private JTextField passwordText;
private JButton loginButton;
// Label for successful login
private JLabel success;
// Default Constructor to add the frames and panels etc
public UserLoginPage() {
}
private void createAndShowGUI() {
frame = new JFrame(getClass().getSimpleName());
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
userLabel = new JLabel("Username");
passwordLabel = new JLabel("Password");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("Login");
success = new JLabel("Login Successful");
loginButton.addActionListener(this);
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
frame.add(panel);
frame.add(success, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserLoginPage().createAndShowGUI();;
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
}
答案2
得分: 2
你说得对。loginButton.addActionListener(new UserLoginPage())
会导致 java.lang.StackOverflowError
。构造函数总是在没有基本默认情况的情况下调用自身。你应该将UserLoginPage
的实例作为参数传递,而不是一个新的实例。
使用以下代码替代:
loginButton.addActionListener(this);
英文:
You were correct. loginButton.addActionListener(new UserLoginPage())
causes a java.lang.StackOverflowError
. The constructor is always calling itself without a base case to default to. You should be passing that very instance of the UserLoginPage
as a parameter, not a new instance.
Use this code instead:
loginButton.addActionListener(this);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论