如何在关键侦听器范围之外使用布尔值
Posted
技术标签:
【中文标题】如何在关键侦听器范围之外使用布尔值【英文标题】:How to use a boolean outside the scope of a key listener 【发布时间】:2020-07-24 06:50:16 【问题描述】:我创建了一个购物车登录 JFrame 并添加了一个“shopkeeperToggle”,以便在按下它时用户登录到店主的 JFrame,否则登录到购物者的 jframe。问题是我不知道如何实现它,每当在“shopkeeperToggle”键侦听器中释放键时,我都尝试将布尔值“pressed”设置为 false,显然我无法使用 press 的值 inside登录按钮。
这是切换的代码:
shopkeeperToggle = new JToggleButton("Shopkeeper");
shopkeeperToggle.addKeyListener(new KeyAdapter()
@Override
public void keyReleased(KeyEvent e)
pressed = false;
);
这就是我在登录按钮中尝试做的事情:
signinButton = new JButton("Sign in ");
signinButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
try
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/shoppingCart","root","");
// select the users that have the inputted credentials from the database
String sql = "SELECT * FROM users WHERE userUsername = ? AND userEmail =?AND userPassword = ? ";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,usernamelogin.getText());
ps.setString(2, emaillogin.getText());
ps.setString(3,passwordlogin.getText());
ResultSet rs = ps.executeQuery();
// if query executed and
if (rs.next())
// if login succ show window log succ, and go to home shopping page
JOptionPane.showMessageDialog(null,"Login successful! :)");
/////////////////this is where I fail////////////////////
if (pressed)
OwnerHomePage ownerhome = new OwnerHomePage();
ownerhome.setVisible(true);
setVisible(false);
else
UserHomePage home = new UserHomePage();
home.setVisible(true);
setVisible(false);
else
JOptionPane.showMessageDialog(null,"Wrong Username or Email or Password :(");
catch (Exception e1)
JOptionPane.showMessageDialog(null,e1);
【问题讨论】:
boolean pressed = signinButton.isSelected();
它让你成为救世主
【参考方案1】:
这可能有助于解决您的问题。这是一个完全可编译的演示。进行了以下更改。
使用Actions
代替KeyListener
。设置起来有点复杂,但可以将它们配置为仅监控某些键。
使用JButton
代替JToggleButton
。没有具体原因,可以更改。
为侦听器创建了单独的内部类。倾向于减少混乱并且通常更具可读性。
根据模式更改了按钮的名称。
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class ActionMapAndButtons
JFrame frame = new JFrame("Demo");
public static void main(String[] args)
SwingUtilities
.invokeLater(() -> new ActionMapAndButtons().start());
public void start()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyClass cls = new MyClass();
frame.add(cls);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
class MyClass extends JPanel
JButton button = new JButton("Shopper Sign in");
boolean pause = false;
public MyClass()
setPreferredSize(new Dimension(300, 300));
button.addActionListener(new ButtonListener());
add(button);
InputMap map = getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
for (int i = 0; i < 256; i++)
map.put(KeyStroke.getKeyStroke((char)i), "anyKey");
getActionMap().put("anyKey", new MyAction());
setFocusable(true);
private class ButtonListener implements ActionListener
public void actionPerformed(ActionEvent ae)
Object obj = ae.getSource();
if (obj instanceof JButton)
JButton b = (JButton) obj;
pause = !pause;
if (pause)
b.setText("Shopkeeper Sign in");
else
b.setText("Shopper Sign in");
private class MyAction extends AbstractAction
public void actionPerformed(ActionEvent ae)
String cmd = ae.getActionCommand();
if (pause)
System.out.println("Shopkeeper - " + cmd);
else
System.out.println("Shopper - " + cmd);
Inner
(或nested
)类和action
映射包含在The Java Tutorials中
【讨论】:
【参考方案2】:您可以将pressed定义为静态值;
class c
static boolean pressed = true;
...
你可以在任何地方访问;
c.pressed; //will return true if you don't change
【讨论】:
我认为静态最适合外部访问。 它给了我一个错误,上面写着“非法修饰符参数”以上是关于如何在关键侦听器范围之外使用布尔值的主要内容,如果未能解决你的问题,请参考以下文章
如何在具有 Animator 的 GameObject 之外添加 AnimationEvent 接收器/侦听器?