你要在这个类中写一个main函数才可以运行程序
public static void main(String[] args) {
xuanzechengshi x = new xuanzechengshi();
x.selectCity();
}
把xuanzejingdian类及相关的代码注释掉就可运行
while的语句改为如下
while(!str.equals("exit")){
System.out.println("请输入:");
str?=?sc.next();
在java中String的比较一般用equals,而不用==,这样是因为java对对象的比较不一样,可以查阅资料:java中equals和==的区别
String str = input.next() 获取的是字符串
所以当你获取到输入的字符串时
需要转换 str.charAt(0);
好了,全部的获取字符串第一个位置的字符
你代码中的 UI 主界面的类是不是没有 .setVisible(true) 显示出来?
main 函数需要先把 JFrame 设定成 visible.
你那代码也太难看了,以前写的简单供你参考
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Logon {
private JFrame f = new JFrame("Logon");
private JTextField username = new JTextField(10);
private JPasswordField password = new JPasswordField(10);
private JLabel user = new JLabel("User");
private JLabel pwd = new JLabel("Password");
private JButton logon = new JButton("Logon on");
private int count = 0;
public Logon(){
JPanel p = new JPanel();
p.add(user);
p.add(username);
p.add(pwd);
p.add(password);
f.add(p, BorderLayout.NORTH);
f.add(logon, BorderLayout.SOUTH);
logon.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
if(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
System.exit(0);
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.pack();
new Logon();
以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!