** risolto **Java concetto idea
Sono abbastanza nuovo a Java e finora mi piace!
Quindi sto solo chiedendo se qualcuno ha un'idea che potrebbe darmi una mano. Quindi ecco cosa mi piacerebbe fare.
Quello a cui sto lavorando in questo momento è un'applicazione che può interagire con il mio sito Web locale (cambiare titolo, contenuto, ecc.). Quindi quello che mi piace fare è mostrare un JOptionPane.showConfirmDialog
, e inserire un nome utente e una password.
Quindi, in pratica, se il nome utente o la password sono errati Vorrei visualizzare un JOptionPane.showMessageDialog
ma quando fanno clic Ok
per far loro sapere che l'informazione non è sbagliato il showConfirmDialog
scompare!
Qualche idea ragazzi ?! Ecco il mio codice.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class javaTesting extends JFrame {
public JFrame mrFrame;
public int enter;
public JPanel mrPanel;
public javaTesting() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
} catch (SQLException e){
System.out.println(e.getMessage());
}
mrFrame = new JFrame();
mrPanel = new JPanel();
mrPanel.setLayout(new GridLayout(4,1));
JLabel user = new JLabel("Username");
mrPanel.add(user);
JTextField user_input = new JTextField(30);
mrPanel.add(user_input);
JLabel pass = new JLabel("Password");
mrPanel.add(pass);
JTextField pw_input = new JPasswordField(30);
mrPanel.add(pw_input);
mrFrame.setSize(700,700);
mrFrame.setLocationRelativeTo(null);
mrFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//mrFrame.setVisible(true);
mrFrame.setResizable(false);
input();
if(enter == JOptionPane.OK_OPTION)
{
JOptionPane.showMessageDialog(null, "You clicked ok!");
input();
} else {
System.exit(1);
}
}
public void input()
{
enter = (int) JOptionPane.showConfirmDialog(mrFrame,mrPanel,"Login Cridantiels",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
}
public static void main(String agrs[]) throws Exception
{
new javaTesting();
}
}
Quindi questo è quello che ho fatto e sembra funzionare bene per me non so se la sua non corretta. Tuttavia funziona:]
do{
input();
if(enter == JOptionPane.OK_OPTION)
{
JOptionPane.showMessageDialog(null, "You clicked ok!");
} else {
System.exit(1);
}
} while(enter != JOptionPane.CANCEL_OPTION);
Fondamentalmente, è necessario un ciclo per gestire la cosa ... – shan
@shan grazie, ho fatto un do {} ciclo while(), e funziona benissimo. Grazie! –
un altro suggerimento. in Java tutte le classi dovrebbero iniziare con una lettera maiuscola :) se sei nuovo, è importante saperlo;) –