你要在这个类中写一个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和==的区别
因为jdk1.7以上才支持单个字符的switch
现在一般都是用的1.6
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.setLayout(new GridLayout(2, 2));
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(count 3){
if(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
}
if(username.getText().equals("admin") password.getText().equals("abc123")){
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
}
}else{
JOptionPane.showMessageDialog(null, "You have tried 3 times. Program exit!");
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.setBounds(200, 200, 400, 400);
f.pack();
}
public static void main(String[] args) {
new Logon();
}
}