程序要实现的功能:
在文本框中输入一个英语单词,文本域中就添加一个英语单词。左边的选择框中是26个大写的英文字母。选择左边选择框的一项,右边选择框将显示所有以左边选择框中所选的字母开头的单词。
我把26个字母放在26个CheckBox里,每个CheckBox里添加一个接口,如果有个CheckBox选中,
就在右边的文本框里输出以这个字母开头的单词
现在不知道怎样把26个字母一个个存在数组a[]里面,所以都先存个f,但是最主要的是现在这个监听实现不了
大家帮忙看看源代码吧:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Word extends JFrame
{
public int b1,b2,n,t,j=0,i;
String b[]=new String[10];
private JLabel inputJLabel;
private JTextField inputJTextField; private JButton enterJButton;
private JTextArea displayJTextArea; private JCheckBox a[]=new JCheckBox[26]; private JTextField display;
public Word()
{
createUserInterface();
}
private void createUserInterface()
{
int kl=190,kh=20;
Container contentPane=getContentPane();
contentPane.setLayout(null);
inputJLabel=new JLabel();
inputJLabel.setBounds(100,20,80,30);
inputJLabel.setText("请输入单词");
contentPane.add(inputJLabel);
inputJTextField=new JTextField();
inputJTextField.setBounds(200,20,100,30);
contentPane.add(inputJTextField);
/*inputJTextField.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
inputJTextFieldActionPerformed(e);
}
}
);**/ enterJButton=new JButton();
enterJButton.setBounds(300,20,30,30);
enterJButton.setText("添加");
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
enterJButtonActionPerformed(e);
}
}
);
displayJTextArea=new JTextArea();
displayJTextArea.setBounds(100,70,230,100);
contentPane.add(displayJTextArea);
for(i=0;i<=25;i++)
{
a[i]=new JCheckBox();
a[i].setBounds(kh,kl,50,20);
a[i].setText("f");
contentPane.add(a[i]);
a[i].addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(a[i].isSelected()==true)
{
b1=Integer.parseInt(a[i].getText());
for(n=0;n<=j;n++)
{
b2=b[j].charAt(0);
if(b1==b2)
display.setText(b[j]);
}
}
}
}
);
if(kl==340)
{kl=190;kh=kh+50;}
else
kl=kl+30;
} display=new JTextField();
display.setBounds(280,265,80,30);
contentPane.add(display);
setTitle("Word");
setSize(430,400);
setVisible(true);
} private void enterJButtonActionPerformed(ActionEvent e)
{
b[j]=inputJTextField.getText();
displayJTextArea.append(b[j]+"\n");
j++;
}
/**private void aJCheckBoxItemStateChanged(ItemEvent e)
{
if(e.getItemSelectable==a[i])
{ b1=Integer.parseInt(a[i].getText());
for(n=0;n<=j;n++)
{
b2=b[j].charAt(0);
if(b1==b2)
display.setText(b[j]);
}
}
}*/
public static void main(String[] args)
{
Word application=new Word();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
在文本框中输入一个英语单词,文本域中就添加一个英语单词。左边的选择框中是26个大写的英文字母。选择左边选择框的一项,右边选择框将显示所有以左边选择框中所选的字母开头的单词。
我把26个字母放在26个CheckBox里,每个CheckBox里添加一个接口,如果有个CheckBox选中,
就在右边的文本框里输出以这个字母开头的单词
现在不知道怎样把26个字母一个个存在数组a[]里面,所以都先存个f,但是最主要的是现在这个监听实现不了
大家帮忙看看源代码吧:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Word extends JFrame
{
public int b1,b2,n,t,j=0,i;
String b[]=new String[10];
private JLabel inputJLabel;
private JTextField inputJTextField; private JButton enterJButton;
private JTextArea displayJTextArea; private JCheckBox a[]=new JCheckBox[26]; private JTextField display;
public Word()
{
createUserInterface();
}
private void createUserInterface()
{
int kl=190,kh=20;
Container contentPane=getContentPane();
contentPane.setLayout(null);
inputJLabel=new JLabel();
inputJLabel.setBounds(100,20,80,30);
inputJLabel.setText("请输入单词");
contentPane.add(inputJLabel);
inputJTextField=new JTextField();
inputJTextField.setBounds(200,20,100,30);
contentPane.add(inputJTextField);
/*inputJTextField.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
inputJTextFieldActionPerformed(e);
}
}
);**/ enterJButton=new JButton();
enterJButton.setBounds(300,20,30,30);
enterJButton.setText("添加");
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
enterJButtonActionPerformed(e);
}
}
);
displayJTextArea=new JTextArea();
displayJTextArea.setBounds(100,70,230,100);
contentPane.add(displayJTextArea);
for(i=0;i<=25;i++)
{
a[i]=new JCheckBox();
a[i].setBounds(kh,kl,50,20);
a[i].setText("f");
contentPane.add(a[i]);
a[i].addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(a[i].isSelected()==true)
{
b1=Integer.parseInt(a[i].getText());
for(n=0;n<=j;n++)
{
b2=b[j].charAt(0);
if(b1==b2)
display.setText(b[j]);
}
}
}
}
);
if(kl==340)
{kl=190;kh=kh+50;}
else
kl=kl+30;
} display=new JTextField();
display.setBounds(280,265,80,30);
contentPane.add(display);
setTitle("Word");
setSize(430,400);
setVisible(true);
} private void enterJButtonActionPerformed(ActionEvent e)
{
b[j]=inputJTextField.getText();
displayJTextArea.append(b[j]+"\n");
j++;
}
/**private void aJCheckBoxItemStateChanged(ItemEvent e)
{
if(e.getItemSelectable==a[i])
{ b1=Integer.parseInt(a[i].getText());
for(n=0;n<=j;n++)
{
b2=b[j].charAt(0);
if(b1==b2)
display.setText(b[j]);
}
}
}*/
public static void main(String[] args)
{
Word application=new Word();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
监听起作用了,但下面这段代码逻辑错误:
public void itemStateChanged(ItemEvent e) {
if (a[i].isSelected() == true) {变量i的值没有正确设置。
package action;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class SecondApplication extends JFrame
{
JButton myButton = new JButton("Click me");//定义按钮,按钮标题为Click me
Container myContainer;//定义一个容器
//类构建器
public SecondApplication()
{
super("SecondApplication");//窗体定义
myContainer = getContentPane();
myContainer.setLayout(new FlowLayout(FlowLayout.CENTER));//设置布局为居中
myContainer.add("center",myButton);//将按钮加入容器
setSize(300,100);//设置窗口大小
setVisible(true);//设置窗体为可见
myButton.addActionListener(new MyActionListener());//给按钮添加事件
this.addWindowListener(new myWindowListener());
}
//按钮事件监听器
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"You have clicked the button!");//弹出消息框
}
}
//窗体事件监听器
class myWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//退出程序
}
}
//主方法
public static void main(String[] args)
{
SecondApplication myApp=new SecondApplication();
myApp.setVisible(true);
}
}
public void itemStateChanged(ItemEvent e)
{
if(a[i].isSelected()==true)
{
b1=Integer.parseInt(a[i].getText());
for(n=0;n<=j;n++)
{
b2=b[j].charAt(0);
if(b1==b2)
display.setText(b[j]);
}
}
}
想在这26个CheckBox中都添加一个监听事件,来判断是不是被选中了,然后再在右边的文本框中显示以这个字母开头的b[]中的单词,大家可以先把CheckBox的监听注释掉,就可以运行看到我的设计界面a[i].addItemListener()就是注释掉这个方法运行一下
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Word extends JFrame
{
public int b1,b2,n,t,j=0,i;
String b[]=new String[10];
String all = "abcdefghijklmnopqrstuvwxyz";
private JLabel inputJLabel;
private JTextField inputJTextField; private JButton enterJButton;
private JTextArea displayJTextArea; private JCheckBox a[]=new JCheckBox[26]; private JTextField display;
public Word()
{
createUserInterface();
}
private void createUserInterface()
{
int kl=190,kh=20;
Container contentPane=getContentPane();
contentPane.setLayout(null);
inputJLabel=new JLabel();
inputJLabel.setBounds(100,20,80,30);
inputJLabel.setText("请输入单词");
contentPane.add(inputJLabel);
inputJTextField=new JTextField();
inputJTextField.setBounds(200,20,100,30);
contentPane.add(inputJTextField); enterJButton=new JButton();
enterJButton.setBounds(300,20,30,30);
enterJButton.setText("添加");
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
enterJButtonActionPerformed(e);
}
}
);
displayJTextArea=new JTextArea();
displayJTextArea.setBounds(100,70,230,100);
contentPane.add(displayJTextArea);
for(i=0;i<26;i++)
{
a[i]=new JCheckBox();
a[i].setBounds(kh,kl,50,20);
a[i].setText(all.substring(i,i+1));
contentPane.add(a[i]);
if(kl==340){kl=190;kh=kh+50;}
else kl=kl+30;
}
CheckBoxAction cba = new CheckBoxAction();
for(int i=0;i<26;i++)
a[i].addItemListener(cba);
display=new JTextField();
display.setBounds(280,265,80,30);
contentPane.add(display);
setTitle("Word");
setSize(430,400);
setVisible(true);
}private void enterJButtonActionPerformed(ActionEvent e)
{
b[j]=inputJTextField.getText();
displayJTextArea.append(b[j]+"\n");
j++;
inputJTextField.setText("");
}
class CheckBoxAction implements ItemListener{ /* (non-Javadoc)
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
JCheckBox i = (JCheckBox)e.getSource();
for(int n=0;n<j;n++)
if(i.getLabel().equals(b[n].substring(0,1)))
display.setText(b[n]);
}
}
public static void main(String[] args)
{
Word application=new Word();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}程序我改了一下
运行了一下,基本符合你的要求,呵~
中的i是类型