gpt4 book ai didi

java - 创建您自己的 Swing 组件

转载 作者:行者123 更新时间:2023-12-01 13:10:42 24 4
gpt4 key购买 nike

需要一个软件组件,它允许在给定的段落中查找给定的单词,每当该组件在段落中找到所需的单词时,该组件将调用所有附加到它的监听器,以表明它已找到所需的单词。该组件必须在基于 Swing UI 的应用程序中使用,该应用程序向用户显示一个用于获取单词的文本字段和一个用于 channel 的文本区域。必须有一个标签会更新其值,显示给定单词在给定段落中出现的次数。

这是我的代码,它是 displya gui,但计数字段未更改或设置。

import java.awt.EventQueue;

public class compomentex implements ActionListener{

private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextArea textArea;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
compomentex window = new compomentex();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public interface wordsearcherinterface
{
public void wordsearch();
}
public class wordsearcher implements wordsearcherinterface
{
public String word;
public String paragraph;

public void addwordsearch(wordsearcherinterface obj)
{
obj.wordsearch();
}

public void wordsearch()
{
//if(event.getSource()==Check)
String word;
String Words;
String [] Words2;
int disp=0;
word=textField.getText();

Words=textArea.getText();
Words2=Words.split(" ");

for(int i=0;i<Words2.length;i++)
{
if(Words2[i].equals(word))
{
disp++;


}
}

String show;
show=disp+"";
textField_1.setText(show);
if(disp==0)
{
JOptionPane.showMessageDialog(null, "The Word is not present int Passage");
}


}

}

* Create the application.
public compomentex() {
initialize();

}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblWord = new JLabel("WORD");
lblWord.setBounds(29, 81, 46, 14);
frame.getContentPane().add(lblWord);

textField = new JTextField();
textField.setBounds(121, 78, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);

JLabel lblPassage = new JLabel("PASSAGE");
lblPassage.setBounds(29, 143, 46, 14);
frame.getContentPane().add(lblPassage);

JTextArea textArea = new JTextArea();
textArea.setBounds(97, 161, 196, 90);
frame.getContentPane().add(textArea);

JButton btnCheck = new JButton("CHECK");
btnCheck.setBounds(326, 213, 98, 23);
frame.getContentPane().add(btnCheck);



JLabel lblSeeker = new JLabel("SEEKER");
lblSeeker.setBounds(161, 11, 46, 14);
frame.getContentPane().add(lblSeeker);

JLabel lblCount = new JLabel("COUNT");
lblCount.setBounds(349, 81, 46, 14);
frame.getContentPane().add(lblCount);

textField_1 = new JTextField();
textField_1.setBounds(325, 106, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);

}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}

提前致谢

最佳答案

您的代码中有很多问题。我在下面提到了更正的代码。请指正。

第一个 - actionPerformed 方法为空。

@Override
public void actionPerformed(ActionEvent arg0) {
new wordsearcher().wordsearch();
}
<小时/>

第二个 - 实例变量 textArea 未初始化。

textArea = new JTextArea();
<小时/>

第三 - ActionListener 未添加到按钮 btnCheck

btnCheck.addActionListener(this);

关于java - 创建您自己的 Swing 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22891727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com