我正在尝试使用jformdesigner为一个简单的loggin表单进行设计,但是在显示我所做的工作时遇到了问题。我在这里搜索了其他主题,但找不到代码的解决方案。我试着分析丢失的东西,但没用。这也是我第一次使用这个设计师,它和intellij的swing有点不同,我似乎无法理解正在发生的一切。
public class Login extends JFrame {
public Login() {
initComponents();
}
private void loginButtonActionPerformed(ActionEvent e) {
// TODO add your code here
Operations operations = new Operations();
try {
String usernameStr = usernameField.getText();
String passwordStr = passwordField.getText();
String userTypeStr = userType.getSelectedItem().toString();
if (operations.isLoggedIn(usernameStr, passwordStr, userTypeStr, this)) {
new Dashboard().setVisible(true);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "Please type correct Username/Password.");
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(this, "Please type correct info.");
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - unknown
frame1 = new JFrame();
label3 = new JLabel();
label2 = new JLabel();
label4 = new JLabel();
usernameField = new JTextField();
label5 = new JLabel();
passwordField = new JTextField();
label6 = new JLabel();
userType = new JComboBox<>();
loginButton = new JButton();
closeButton = new JButton();
//======== frame1 ========
{
frame1.setBackground(new Color(225, 85, 113));
var frame1ContentPane = frame1.getContentPane();
frame1ContentPane.setLayout(new MigLayout(
"hidemode 3",
// columns
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[87,fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[99,fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[95,fill]" +
"[fill]" +
"[fill]" +
"[83,fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[36,fill]" +
"[fill]" +
"[0,fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]",
// rows
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]"));
//---- label3 ----
label3.setText("Login Form");
label3.setFont(label3.getFont().deriveFont(label3.getFont().getStyle() | Font.BOLD, label3.getFont().getSize() + 12f));
label3.setHorizontalAlignment(SwingConstants.CENTER);
frame1ContentPane.add(label3, "cell 0 0 44 1");
//---- label2 ----
label2.setIcon(new ImageIcon(getClass().getResource("/Images/lock.png")));
label2.setHorizontalTextPosition(SwingConstants.CENTER);
label2.setHorizontalAlignment(SwingConstants.CENTER);
frame1ContentPane.add(label2, "cell 0 1 45 1");
//---- label4 ----
label4.setText("Username");
label4.setFont(new Font("Tw Cen MT", Font.PLAIN, 17));
frame1ContentPane.add(label4, "cell 3 3 4 1");
frame1ContentPane.add(usernameField, "cell 8 3 34 1");
//---- label5 ----
label5.setText("Password");
label5.setFont(new Font("Tw Cen MT", Font.PLAIN, 17));
frame1ContentPane.add(label5, "cell 3 5 4 1");
frame1ContentPane.add(passwordField, "cell 8 5 34 1");
//---- label6 ----
label6.setText("Login as");
label6.setFont(new Font("Tw Cen MT", Font.PLAIN, 17));
frame1ContentPane.add(label6, "cell 3 7 4 1");
//---- userType ----
userType.setModel(new DefaultComboBoxModel<>(new String[] {
"Admin",
"Sales Representative"
}));
frame1ContentPane.add(userType, "cell 8 7 34 1");
//---- loginButton ----
loginButton.setText("Login");
loginButton.addActionListener(e -> loginButtonActionPerformed(e));
frame1ContentPane.add(loginButton, "cell 3 8 39 2");
//---- closeButton ----
closeButton.setText("Close");
frame1ContentPane.add(closeButton, "cell 3 10 39 3");
frame1.pack();
frame1.setLocationRelativeTo(frame1.getOwner());
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - unknown
private JFrame frame1;
private JLabel label3;
private JLabel label2;
private JLabel label4;
private JTextField usernameField;
private JLabel label5;
private JTextField passwordField;
private JLabel label6;
private JComboBox<String> userType;
private JButton loginButton;
private JButton closeButton;
// JFormDesigner - End of variables declaration //GEN-END:variables
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Login GUI = new Login();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
}
}
代码主要由设计器生成,我添加了main方法。如果你能帮我一把就好了。
1条答案
按热度按时间xqnpmsa81#
之所以什么都看不到,是因为在生成的代码中,内容被添加到
frame1
. 但是你的GUI
,属于Login
也extends JFrame
,所以你在那里很困惑。你曾经GUI.setVisible(true)
即使这个jframe实际上并不包含生成的组件。要解决这个问题,你有两个选择。
不要让你的
Login
扩展jframe,而不是设置包含的JFrame
可见于initComponents()
方法。将所有组件添加到
Login
示例,意义this
,在initComponents
方法,并除去frame1
. 然后你可以保持主方法的原样。