[英]can someone help me how to make the button from another frame work?
有人可以幫忙解決這個問題嗎? 這是一個您登錄並彈出另一個框架的系統。 在那個框架里你可以放兩個詞,框架會告訴你作品是否相同。 但該按鈕由於某種原因不起作用。 我已經這樣做了 4 天,我真的需要一些幫助。
//代碼
公共類 Userinter 實現 ActionListener {
//first frame
private static JLabel userLabel;
private static JTextField userText;
private static JLabel passwordLabel;
private static JPasswordField passwordText;
private static JButton button;
private static JLabel success;
//second frame
private static JLabel label1;
private static JLabel label2;
private static JTextField WordNum1;
private static JTextField WordNum2;
private static JButton button2;
private static JLabel success2;
public static void main(String[] args) {
//frame
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
//username
userLabel = new JLabel("Username");
userLabel.setBounds(80,80,80,25);
panel.add(userLabel);
userText = new JTextField(20);
userText.setBounds(180,80,165,25);
panel.add(userText);
//password
passwordLabel = new JLabel("password");
passwordLabel.setBounds(80,150,80,25);
panel.add(passwordLabel);
passwordText = new JPasswordField();
passwordText.setBounds(180,150,165,25);
panel.add(passwordText);
//button
button = new JButton("Login");
button.setBounds(80,190,80,25);
button.addActionListener((ActionListener) new Userinter());
panel.add(button);
success = new JLabel("set");
success.setBounds(80,250,300,25);
panel.add(success);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String user = userText.getText();
String password = passwordText.getText();
if (user.equals("Gal") && password.equals("Skr")){
// second frame
JPanel Panel2 = new JPanel();
JFrame frame2 = new JFrame();
frame2.setSize(400,300);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.add(Panel2);
Panel2.setLayout(null);
label1 = new JLabel("Set word/number here:");
label1.setBounds(10,20,200,25);
Panel2.add(label1);
WordNum1 = new JTextField();
WordNum1.setBounds(100,50,165,25);
Panel2.add(WordNum1);
label2 = new JLabel("Set word/number here:");
label2.setBounds(10,90,200,25);
Panel2.add(label2);
WordNum2 = new JTextField();
WordNum2.setBounds(100,130,165,25);
Panel2.add(WordNum2);
JButton Button2 = new JButton("Check");
Button2.setBounds(100,200,80,25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e1) {
String word = WordNum1.getText();
String word2 = WordNum2.getText();
if(word.equals(word2)){
success2.setText("yes");
}
}
});
Panel2.add(Button2);
success = new JLabel("set");
success.setBounds(80,250,300,25);
Panel2.add(success);
}else
success.setText("incorrect username or/and password.");
}
}
你真的需要看看在容器內布置組件,它會為你節省大量時間和頭痛。
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
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;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
private LoginPane loginPane;
private WordPane wordPane;
private CardLayout cardLayout;
public MainPane() {
cardLayout = new CardLayout();
setLayout(cardLayout);
loginPane = new LoginPane();
loginPane.addLoginListener(new LoginPane.LoginListener() {
@Override
public void didLoginSuccessfully() {
cardLayout.show(MainPane.this, "words");
}
@Override
public void didCancelLogin() {
SwingUtilities.windowForComponent(MainPane.this).dispose();
}
});
wordPane = new WordPane();
add(loginPane, "login");
add(wordPane, "words");
}
}
public class WordPane extends JPanel {
public WordPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = gbc.REMAINDER;
gbc.insets = new Insets(8, 8, 8, 8);
add(new JLabel("Set word/number here:"), gbc);
gbc.gridy++;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("First word:"), gbc);
gbc.gridy++;
add(new JLabel("Second word:"), gbc);
gbc.gridx++;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.LINE_START;
JTextField firstWordField = new JTextField(10);
JTextField secondWordField = new JTextField(10);
add(firstWordField, gbc);
gbc.gridy++;
add(secondWordField, gbc);
JButton checkButton = new JButton("Check");
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = gbc.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
add(checkButton, gbc);
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (firstWordField.getText().equals(secondWordField.getText())) {
JOptionPane.showMessageDialog(WordPane.this, "Words match", "Match", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(WordPane.this, "Words do not match", "Does Not Match", JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
public class LoginPane extends JPanel {
public interface LoginListener extends EventListener {
// Probably should pass in the User which was authenticiated,
// but I'll leave that to you to figure out
public void didLoginSuccessfully();
public void didCancelLogin();
}
private JTextField userNameField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton cancelButton;
public LoginPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(8, 8, 8, 8);
add(new JLabel("User name:"), gbc);
gbc.gridy++;
add(new JLabel("Password:"), gbc);
userNameField = new JTextField(10);
passwordField = new JPasswordField(10);
DocumentListener documentListener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
fieldsDidChange();
}
@Override
public void removeUpdate(DocumentEvent e) {
fieldsDidChange();
}
@Override
public void changedUpdate(DocumentEvent e) {
fieldsDidChange();
}
};
userNameField.getDocument().addDocumentListener(documentListener);
passwordField.getDocument().addDocumentListener(documentListener);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
add(userNameField, gbc);
gbc.gridy++;
add(passwordField, gbc);
loginButton = new JButton("Login");
loginButton.setEnabled(false);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performLogin();
}
});
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performCancel();
}
});
JPanel buttonPane = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = gbc.HORIZONTAL;
buttonPane.add(loginButton, gbc);
gbc.gridx++;
buttonPane.add(cancelButton, gbc);
gbc = new GridBagConstraints();
gbc.gridy = 2;
gbc.gridx = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(buttonPane, gbc);
}
public void addLoginListener(LoginListener listener) {
listenerList.add(LoginListener.class, listener);
}
public void removeLoginListener(LoginListener listener) {
listenerList.remove(LoginListener.class, listener);
}
protected void fireDidLoginSuccessfully() {
LoginListener[] listeners = listenerList.getListeners(LoginListener.class);
System.out.println(listeners.length);
if (listeners.length == 0) {
return;
}
for (LoginListener listener : listeners) {
listener.didLoginSuccessfully();
}
}
protected void fireDidCancel() {
LoginListener[] listeners = listenerList.getListeners(LoginListener.class);
if (listeners.length == 0) {
return;
}
for (LoginListener listener : listeners) {
listener.didCancelLogin();
}
}
protected void fieldsDidChange() {
if (userNameField.getText().length() > 0 && passwordField.getPassword().length > 0) {
loginButton.setEnabled(true);
} else {
loginButton.setEnabled(false);
}
}
protected void performLogin() {
// Peform the authentication
// In a production system, it would be fesible to have this
// done via a different class passed into this class
fireDidLoginSuccessfully();
}
protected void performCancel() {
fireDidCancel();
}
}
}
至於你的“問題”
這個...
button.addActionListener((ActionListener) new Userinter());
將在 UI 上顯示的內容與您的代碼將要執行的內容之間產生斷開連接,因為您有兩個Userinter
實例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.