Witam, mam taki problem potrzebuje aby przycisk mógł zostać kliknięty tylko raz, tzn. jeżeli login i hasło są poprawne to przycisk zaloguj można kliknąć tylko raz, natomiast gdy zły to dopóki nie zaloguje się poprawnie. Głównie chodzi o to żeby można było tylko raz przycisnąć JButtona.
0
1
No to w czym problem ? jesli dane poprawne chowaj buttona, ewentualnie dezaktywuj , odpinaj sluchacza co kto woli
0
już w niczym, przycisk został schowany :)
0
Znalazłem chwilkę czasu by stworzyć jakiś przykładowy kod, który pozwoli zrozumieć zasadę działania :
- Schowanie dezorganizuje widok - w zależności od layoutu
- Pamiętaj, że jakiekolwiek zmiany w elementach widoku powinny być wykonywane w tym samym wątku
package button;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Button extends JFrame implements ActionListener {
private int increment;
private JButton loginButton;
private JButton hideButton;
private JButton disableButton;
private JButton unmountButton;
private JTextField text;
public Button() {
init();
}
private void init() {
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
text = new JTextField(8);
loginButton = createButton("Login");
hideButton = createButton("Hide");
disableButton = createButton("Disable");
unmountButton = createButton("Unmount");
topPanel.add(loginButton);
topPanel.add(text);
bottomPanel.add(hideButton);
bottomPanel.add(disableButton);
bottomPanel.add(unmountButton);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Button");
setSize(300, 150);
setVisible(true);
}
private JButton createButton(String name) {
JButton temp = new JButton(name);
add(temp);
temp.addActionListener(this);
return temp;
}
public static void main(String[] args) {
new Button();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(loginButton)) {
text.setText("Loguje " + ++increment);
} else if (e.getSource().equals(hideButton)) {
if (hideButton.getText().equals("Hide")) {
changeButton(hideButton, "Unhide", false);
} else {
changeButton(hideButton, "Hide", true);
}
} else if (e.getSource().equals(disableButton)) {
if (disableButton.getText().equals("Disable")) {
changeButton(disableButton, "Enable", false);
} else {
changeButton(disableButton, "Disable", true);
}
} else if (e.getSource().equals(unmountButton)) {
if (unmountButton.getText().equals("Unmount")) {
changeButton(unmountButton, "Mont", false);
} else {
changeButton(unmountButton, "Unmount", true);
}
}
}
private void changeButton(JButton button, String name, boolean value) {
if (button.equals(hideButton))
loginButton.setVisible(value);
else if (button.equals(disableButton)) {
loginButton.setEnabled(value);
} else if (button.equals(unmountButton)) {
if (!value) {
loginButton.removeActionListener(this);
text.setText("Odpięty");
} else
loginButton.addActionListener(this);
}
button.setText(name);
}
}