3
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class JDorm implements ItemListener{

public static void main(String[] args){

    JCheckBox privateroom = new JCheckBox("Private Room",false);
    JCheckBox interweb = new JCheckBox("Internet Connection",false);
    JCheckBox cable = new JCheckBox("Cable TV connection",false);
    JCheckBox fridg = new JCheckBox("Refridgerator",false);
    JCheckBox microwave = new JCheckBox("Microwave",false);
    JCheckBox soon = new JCheckBox(",and so on",false);
    JLabel greet = new JLabel("Please choose ammenities");
    String sel = "Your selected options";

    JTextArea textBox = new JTextArea(sel,0,1);
    cable.addItemListener();

    JFrame dormFrame = new JFrame("Dorm Options");// creates frame with title
    final int WIDTH = 250;
    final int HEIGHT = 500;
    dormFrame.setSize(WIDTH, HEIGHT);// sets the size of frame in pixels
    dormFrame.setVisible(true);// note: default visibility is false
    dormFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    dormFrame.setLayout(new FlowLayout());
    dormFrame.add(greet);
    dormFrame.add(microwave);
    dormFrame.add(fridg);
    dormFrame.add(cable);
    dormFrame.add(interweb);
    dormFrame.add(privateroom);
    dormFrame.add(soon);
    dormFrame.add(textBox);
}
public void itemStateChanged(ItemEvent event){
    Object source = event.getSource();
    int select = event.getStateChange();
}

}

这是我到目前为止所拥有的,我知道我需要监听器,并且在选中和取消选中选择时会在框中显示一条消息。我需要 if 语句来进行更改吗?

4

2 回答 2

2

创建一个可以添加到所有复选框的通用侦听器。就像是:

ItemListener listener = new ItemListener()
{
    public void itemStateChanged(ItemEvent event)
    {
        JCheckBox checkBox = (JCheckBox)event.getSource();
        textBox.setText( checkBox.getText() );
    }
};

然后将侦听器添加到每个复选框:

privateRoom.addItemListener( listener );
interweb.addItemListener( listener );
于 2013-04-14T18:12:38.517 回答
0

我试过一个复选框,你可以做其他类似的事情

 final JCheckBox privateroom = new JCheckBox("Private Room",false);

现在将项目侦听器添加到复选框私人房间以及您希望发生的操作,即

 privateroom.addItemListener(new ItemListener()
    {
        public void itemStateChanged(ItemEvent event)
        {
            if (event.getItemSelectable() == privateroom)
                textBox.setText("Private Room");
        }
    });

我将 Checkbox privaterroom 声明为 final 的原因是因为 privaterroom 是本地的并且是从内部类访问的。

还有一件事我不知道您编写程序的方式是好是坏,因为我也在学习挥杆并且是新手。但是我编写程序的方式具有以下结构

class MyClass extends JFrame implements LISTENER_NAME
{
    // Components declared here

    // constructor starts
    public MyClass()
    {
        //Componenets instantiated 
        // add listeners to appropriate components
        // setVisible(), setDefaultCloseOperation(),setSize() etc called

    }

    // listener interface methods here like

    public void itemStateChanged(ItemEvent ie)
    {
        ................
    }

    // main method
    public static void main(String[] args)
    {
        new MyClass();
    }
} // class over

这样我就从来没有遇到过 final 关键字等问题。希望有高手指导一下。

于 2013-04-14T14:56:18.980 回答