我的应用程序有一个主框架,我想要另一个用于登录的框架。所以我写了两个类:一个 Main 类和一个 LoginFrame 类。
/*Imported classes*/
public class Main {
private JFrame frame;
private LoginFrame loginFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("Main Frame");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel mainPanel = new JPanel();
mainPanel.setBounds(10, 10, 426, 243);
frame.getContentPane().add(mainPanel);
mainPanel.setLayout(null);
loginFrame = new LoginFrame("Login...");
loginFrame.setVisible(true);
loginFrame.pack();
}
}
/*Imported classes*/
public class LoginFrame extends JFrame {
private JFrame frame;
private JTextField usernameField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame window = new LoginFrame("Login...");
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the application.
*/
public LoginFrame(String title) {
/**
* Initialize the contents of the frame.
*/
frame = new JFrame(title);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 10, 426, 243);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel userLabel = new JLabel("User:");
userLabel.setFont(new Font("Dialog", Font.BOLD, 14));
userLabel.setEnabled(true);
userLabel.setBounds(41, 78, 45, 13);
panel.add(userLabel);
JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
passwordLabel.setBounds(41, 124, 76, 13);
panel.add(passwordLabel);
usernameField = new JTextField();
usernameField.setBounds(165, 75, 173, 19);
panel.add(usernameField);
usernameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.setBounds(165, 123, 173, 19);
panel.add(passwordField);
JLabel loginLabel = new JLabel("Enter Username and Password");
loginLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
loginLabel.setBounds(79, 22, 259, 13);
panel.add(loginLabel);
JButton resetLoginButton = new JButton("Reset");
resetLoginButton.setFont(new Font("Tahoma", Font.BOLD, 14));
resetLoginButton.setBounds(318, 189, 85, 21);
panel.add(resetLoginButton);
JButton okLoginButton = new JButton("Ok");
okLoginButton.setFont(new Font("Tahoma", Font.BOLD, 14));
okLoginButton.setBounds(217, 189, 85, 21);
panel.add(okLoginButton);
}
}
当我打开登录框架时,没有任何内容(按钮和文本字段)。问题是什么?这是实现登录框架的正确方法吗?
我预计有两个不同的框架,主框架是空的。第二个有两个字段,一个用于用户名,另一个用于密码和两个按钮。
当我打开登录框架时,没有任何内容(按钮和文本字段)。
当然。
有什么问题吗?
您永远不会向您的
LoginFrame
添加任何组件。相反,您创建一个 separate JFrame
,向其中添加一些组件,然后不对其进行任何进一步操作。