所以我有了这段代码,可以打开登录页面和一个简单的主页。 这也包括ActionListeners。
类Login扩展JFrame实现ActionListener{
JPanel panel;
JLabel user_label, password_label, message;
JTextField userName_text;
JPasswordField password_text;
JButton submit, cancel;
Login() {
user_label = new JLabel();
user_label.setText("Username :");
userName_text = new JTextField();
password_label = new JLabel();
password_label.setText("Password :");
password_text = new JPasswordField();
submit = new JButton("SUBMIT");
panel = new JPanel(new GridLayout(3, 1));
panel.add(user_label);
panel.add(userName_text);
panel.add(password_label);
panel.add(password_text);
message = new JLabel();
panel.add(message);
panel.add(submit);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
submit.addActionListener(this);
add(panel, BorderLayout.CENTER);
setTitle("Please Login Here !");
setSize(350,250);
setVisible(true);
}
public static void main(String[] args) {
new Login();
}
@Override
public void actionPerformed(ActionEvent ae) {
String userName = userName_text.getText();
@SuppressWarnings("deprecation")
String password = password_text.getText();
boolean args;
if (userName.trim().equals("LOL") && password.trim().equals("12345")) {
{JFrame frame = new JFrame("Welcome Back, (Insert username here)!");
frame.setVisible(true);
frame.setSize(1000,1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Total Steps",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(800, 300));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
JLabel textLabel2 = new JLabel("--- Steps Today!",SwingConstants.CENTER);
textLabel2.setPreferredSize(new Dimension(800, 350));
frame.getContentPane().add(textLabel2, BorderLayout.CENTER);
frame.setLocationRelativeTo(textLabel);
frame.pack();
frame.setVisible(true);
JLabel textLabel3 = new JLabel("Total Distance Walked",SwingConstants.CENTER);
textLabel3.setPreferredSize(new Dimension(800,450));
frame.getContentPane().add(textLabel3, BorderLayout.CENTER);
frame.setLocationRelativeTo(textLabel2);
frame.pack();
frame.setVisible(true);
JLabel textLabel4 = new JLabel("--- Kilometres",SwingConstants.CENTER);
textLabel4.setPreferredSize(new Dimension(800, 500));
frame.getContentPane().add(textLabel4, BorderLayout.CENTER);
frame.setLocationRelativeTo(textLabel3);
frame.pack();
frame.setVisible(true);
JLabel textLabel5 = new JLabel(" “All truly great thoughts are conceived while walking.” ",SwingConstants.CENTER);
textLabel5.setPreferredSize(new Dimension(800, 600));
frame.getContentPane().add(textLabel5, BorderLayout.CENTER);
frame.setLocationRelativeTo(textLabel4);
frame.pack();
frame.setVisible(true);
JLabel textLabel6 = new JLabel(" - Friedrich Nietzsche, Twilight of the Idols ",SwingConstants.CENTER);
textLabel6.setPreferredSize(new Dimension(800, 650));
frame.getContentPane().add(textLabel6, BorderLayout.CENTER);
frame.setLocationRelativeTo(textLabel5);
frame.pack();
frame.setVisible(true);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Global Leaderboards");
panel.add(button);
button.addActionListener (new Action1());
JButton button2 = new JButton("User Profile");
panel.add(button2);
button2.addActionListener (new Action2());}
}
else {
message.setText(" Invalid user.. ");
}
}
}
然而,当我按下用户配置文件按钮时,只显示框架,而不显示标签和文本字段。 两个都在类,我设法让全球排行榜工作(不包括在这里),只是似乎不能让用户Proflie正常工作。
public class Action2 extends JFrame implements ActionListener {
public Action2() {
setLayout(new GridLayout(11, 5, 5, 5));
add(new JLabel("Nickname"));
add(new JTextField(10));
add(new JLabel("Age"));
add(new JTextField(3));
add(new JLabel("Gender"));
add(new JTextField(8));
add(new JLabel("Height"));
add(new JTextField(4));
add(new JLabel("Weight"));
add(new JTextField(4));
add(new JLabel("Your Goal"));
add(new JRadioButton("Easy"));
add(new JRadioButton("Intermediate"));
add(new JRadioButton("Hard"));
add(new JButton("Update Profile"));
}
public void actionPerformed(ActionEvent e) {
Action2 frame = new Action2();
frame.setTitle("Profile");
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我能得到的任何帮助都非常感激。
不幸的是,你做了不少错误的事情。
JFrame
。 创建一个扩展JPanel
的类,并向其添加组件。 frame.pack();
frame.setVisible(true);
当您将要显示整个GUI时。 而不是在添加每个组件之后。
我建议您查看Java教程以获取更多信息。