Java 如何使JButton在再次单击时重复其操作?
我试图让“生成”按钮在每次按下时在文本区域中放置一个新词。我尝试过循环,这导致我的GUI停止响应 用户输入示例:单击“生成”按钮3次 文本区域中的示例输出:AppleStatocCumber 有什么建议吗 代码如下:Java 如何使JButton在再次单击时重复其操作?,java,swing,user-interface,Java,Swing,User Interface,我试图让“生成”按钮在每次按下时在文本区域中放置一个新词。我尝试过循环,这导致我的GUI停止响应 用户输入示例:单击“生成”按钮3次 文本区域中的示例输出:AppleStatocCumber 有什么建议吗 代码如下: public Generator(){ String[] words = {apple, pear, cucumber, lettuce, tomato, potato}; String random = (words[new Random().nextInt(w
public Generator(){
String[] words = {apple, pear, cucumber, lettuce, tomato, potato};
String random = (words[new Random().nextInt(words.length)]);
setBackground(Color.CYAN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
SpringLayout sl_contentPane = new SpringLayout();
contentPane.setLayout(sl_contentPane);
JLabel lblNewLabel = new JLabel("Random Word Generator");
sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 10, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 27, SpringLayout.WEST, contentPane);
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 21));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblNewLabel);
JLabel lblInfo = new JLabel("Result will be printed in the white space.");
sl_contentPane.putConstraint(SpringLayout.NORTH, lblResultsWillBe, 73, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, lblResultsWillBe, 124, SpringLayout.WEST, contentPane);
lblResultsWillBe.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(lblInfo);
JTextArea textArea = new JTextArea();
textArea.setWrapStyleWord(true);
sl_contentPane.putConstraint(SpringLayout.NORTH, textArea, 150, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, textArea, 141, SpringLayout.WEST, contentPane);
sl_contentPane.putConstraint(SpringLayout.SOUTH, textArea, 12, SpringLayout.SOUTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, textArea, -124, SpringLayout.EAST, contentPane);
contentPane.add(textArea);
JButton btnGenerate = new JButton("Generate");
sl_contentPane.putConstraint(SpringLayout.NORTH, btnGenerate, 41, SpringLayout.NORTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.WEST, btnGenerate, 163, SpringLayout.WEST, contentPane);
contentPane.add(btnGenerate);
btnGenerate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
textArea.setText(random);
Object source = e.getSource();
if (source == btnGenerate){
textArea.setText(random);
}
}});
JLabel txtResults = new JLabel();
sl_contentPane.putConstraint(SpringLayout.SOUTH, txtResults, -100,
SpringLayout.SOUTH, contentPane);
sl_contentPane.putConstraint(SpringLayout.EAST, txtResults, -143, SpringLayout.EAST, contentPane);
contentPane.add(txtResults);
}
文本区域中的示例输出:AppleStatocCumber
不要使用setText()
方法。它将替换现有文本
而是使用:
textArea.append(random);
因此,每次单击按钮时,新文本都会附加到文本区域。
textArea.setText(textArea.getText()+random)
也要去掉对象source=e.getSource()
;在这种情况下,if语句是不需要的;如果您在actionPerformed()或其他在事件中被调用的方法中做了错误的事情(从这个角度来看,您可以做的大多数事情都是错误的)。。。你冻结了你的用户界面。你的代码没有编译。我试过了,但是“生成”按钮会重复相同的随机值,并在每次单击时将其添加到以前的值中。例如,用户单击按钮五次,输出为“AppleAppleAppleAppleAppleAppleApple”。我希望每次按下按钮时,该按钮都能将从数组中提取的新随机字符串放入文本区域。@JavaWeenis,您需要在每次单击按钮时确定一个新的随机字符串。因此,随机代码需要放在ActionListener中。