0
메신저 프로그램을 만들고 난이 일을 작동하게하는 방법을 모르고도 없어 :자바의 JInternalFrame
이package itneizapenoitseg;
public class mainFrame extends JFrame {
private dbconnection database = new dbconnection(); // DB connetion data (login , passw ..)
private JDesktopPane desktop = new JDesktopPane(); // Need this to make Frame inside Frame
public mainFrame() {
super("");
setLayout(null);
setSize(850, 700);
setLocation(500, 280);
Login login = new Login(database);// I want to perform a login
desktop.add(login);
try {
login.setSelected(true);
}
catch (Exception e) {
// TODO: handle exception
}
setContentPane(desktop);
/////////////////////////////////////////
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String [] args) {
new mainFrame();
}
}
로그인 클래스 :
package itneizapenoitseg;
import... //
public class Login extends JInternalFrame {
// User and password fields
JTextField usernameForm = new JTextField(15);
JPasswordField passwordForm = new JPasswordField(15);
JButton login = new JButton("Login");
public Login(dbconnection database) {
super("Login", true, true, true, true);
setSize(300, 200);
// The login panel
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel username = new JLabel("username :");
JLabel password = new JLabel("password :");
// Position
username.setBounds(70, 10, 80, 11);
password.setBounds(70, 55, 80, 17);
usernameForm.setBounds(70, 30, 150, 20);
passwordForm.setBounds(70, 75, 150, 20);
login.setBounds(105, 100, 80, 20);
// Addings elements to display panel
panel.add(usernameForm);
panel.add(passwordForm);
panel.add(login);
panel.add(password);
panel.add(username);
getContentPane().add(panel);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// Doens't work
setVisible(true);
actionLogin(database);
}
// When pressing Login button...
private void actionLogin(dbconnection database) {
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//TODO Have to change to MySql query
String usname = usernameForm.getText();
String passw = new String(passwordForm.getPassword());
// Checking credencial
if(usname.equals("test") && passw.equals("1234")){
// Here i want to call a mainFrame funcion (createGUI)
dispose();Destroy this panel
} else{
JOptionPane.showMessageDialog(null, "Username or password wrong!");
usernameForm.setText("");
passwordForm.setText("");
usernameForm.requestFocus();
}
}
});
}
}
내가 때 원하는 내가 메인 프레임을 을 누군가 성공적으로 로그인하면 mainFrame의 함수 인 createGui가 호출되거나 mainFrame에 알립니다. 내 의도는 sombody 성공적으로 로그인하고 다음 내용을 표시 할 때까지 mainFrame을 비우는 것입니다.
작업 원인이 actionListener 내부에 없습니다. – HaZe
Ok - 익명의 클래스를 사용했기 때문에 그렇습니다. 그건 나쁘지 만 세상 끝은 아닙니다! 'this' 대신'(Component) e.getSource()'를 사용하고 그것이 작동하는지 확인하십시오. –
답변도 편집 됨 :) –