gpt4 book ai didi

java - 显示一个窗口以获取用户输入

转载 作者:行者123 更新时间:2023-12-02 14:11:35 24 4
gpt4 key购买 nike

我是 Java 编程新手。

我有这两个小项目。

import javax.swing.*;

public class tutorial {

public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("hello");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);

JButton button = new JButton("Hello again");
panel.add(button);
}
}

还有这个:

import java.util.*;

public class Test {

public static void main(String[] args){
int age;
Scanner keyboard = new Scanner(System.in);
System.out.println("How old are you?");
age = keyboard.nextInt();
if (age<18)
{
System.out.println("Hi youngster!");
}
else
{
System.out.println("Hello mature!");
}
}

}

如何将第二个代码添加到第一个代码中,以便用户将看到一个窗口,显示“您多大了”,并且他们可以输入自己的年龄。

提前致谢!

最佳答案

我不确定你想要的所有东西,因为它是未定义的,但据我所知,你想要一个包含输入字段的 JFrame,你可以在其中输入值并显示适当的答案。我还建议您阅读教程,但如果您有疑问,请不要犹豫。我希望它与您想要的有点接近。

package example.tutorial;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
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.JTextField;

public class Tutorial extends JPanel {
private static final String YOUNG_RESPONSE = "Hi youngster!";
private static final String ADULT_RESPONSE = "Hello mature!";
private static final String INVALID_AGE = "Invalid age!";

private static final int MIN_AGE = 0;
private static final int MAX_AGE = 100;

private static JTextField ageField;
private static JLabel res;

private Tutorial() {
setLayout(new BorderLayout());

JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
JLabel label = new JLabel("How old are you ? ");
northPanel.add(label);

ageField = new JTextField(15);
northPanel.add(ageField);
add(northPanel, BorderLayout.NORTH);


JPanel centerPanel = new JPanel();
JButton btn = new JButton("Hello again");
btn.addActionListener(new BtnListener());
centerPanel.add(btn);

res = new JLabel();
res.setVisible(false);
centerPanel.add(res);

add(centerPanel, BorderLayout.CENTER);
}

public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new Tutorial());
frame.setVisible(true);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private static class BtnListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String content = ageField.getText();
int age = -1;
try{
age = Integer.parseInt(content);
if(isValid(age)) {
res.setText(age < 18 ? YOUNG_RESPONSE : ADULT_RESPONSE);
} else {
res.setText(INVALID_AGE);
}
if(!res.isVisible())
res.setVisible(true);
} catch(NumberFormatException ex) {
res.setText("Wrong input");
}
}

private boolean isValid(int age) {
return age > MIN_AGE && age < MAX_AGE;
}
}
}

关于java - 显示一个窗口以获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29826916/

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