【问题标题】:jCheckbox to be displayed in textareajCheckbox 要显示在 textarea 中
【发布时间】:2012-10-23 19:34:33
【问题描述】:

我正在试验 JCheckBox。我想要的是,当我选中其中任何一个(复选框)时,复选框中的文本应显示在 jtextarea 中。

我现在拥有的是这样的:

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

@SuppressWarnings("serial")
public class CheckboxSample extends JFrame implements ActionListener{

    //vars
    JCheckBox cb1, cb2, cb3, cb4, cb5, cb6;
    JTextArea ta;
    String text = "";
    JLabel lbl;

    CheckboxSample(){

        Container c = getContentPane();
        c.setBackground(Color.BLACK);
        c.setLayout( new FlowLayout() );

        ta = new JTextArea( 10, 20 );

        //create two check boxes
        cb1 = new JCheckBox( "Java" );
        cb2 = new JCheckBox( "C#" );
        cb3 = new JCheckBox( "VB.Net" );
        cb4 = new JCheckBox( "Python" );
        cb5 = new JCheckBox( "C++" );
        cb6 = new JCheckBox( "Objective-C" );

        lbl = new JLabel( "Choose your favorite programming language/s: " );

        //add the checkboxes,  textarea to the container
        lbl.setForeground(Color.white);
        c.add( lbl );
        c.add( cb1 );
        c.add( cb2 );
        c.add( cb3 );
        c.add( cb4 );
        c.add( cb5 );
        c.add( cb6 );
        c.add( ta );
        ta.enable(false);

        //add action listeners. We need not add listener to text area
        //since the user clicks on the checkboxes or radio buttons only
        cb1.addActionListener(this);
        cb2.addActionListener(this); 
        cb3.addActionListener(this);
        cb4.addActionListener(this);
        cb5.addActionListener(this);
        cb6.addActionListener(this);

        //close the frame upon clicking
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //know which components are selected by user
        if( cb1.getModel().isSelected() ) text+="\nJava";
        if( cb2.getModel().isSelected() ) text+="\nC#";     
        if( cb3.getModel().isSelected() ) text+="\nVB.Net";
        if( cb4.getModel().isSelected() ) text+="\nPython";
        if( cb5.getModel().isSelected() ) text+="\nC++";
        if( cb6.getModel().isSelected() ) text+="\nObjective-C";
        //else text+="\nFemale";
        //display the selected message in text area
        ta.setText(text);

        //reset the message to empty string
        text="";
    }


    public static void main(String args[])
    {
        //create frame
        CheckboxSample cr = new CheckboxSample();
        cr.setTitle("My Samples");
        cr.setSize(500,400);
        cr.setVisible(true);
    }

    }

如何在不连接复选框文本的情况下做到这一点?谢谢。

【问题讨论】:

    标签: java swing jcheckbox


    【解决方案1】:

    所以,如果我没看错的话:

    public void actionPerformed(ActionEvent e) {
    
        if (e.getSource() instanceof JCheckBox) {
    
            JCheckBox checkBox = (JCheckBox)e.getSource();
            String text = checkBox.getText();
    
            int pos = ta.getText().getLength()
            ta.insert(text, pos);
    
            // or, more simply
    
            ta.append(text);
    
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您应该为每个JCheckBox 添加一个ChangeListener。每次单击复选框时都会调用#stateChanged(ChangeEvent)

      final JCheckBox cb1 = new JCheckBox("cb Text");
      final JTextArea textArea = new JTextArea("initial text");
      cb1.addChangeListener(new ChangeListener() {        
              @Override
              public void stateChanged(ChangeEvent e) {
                  if(cb1.isSelected()){
                      //textArea.setText(b.getText()); //to get the text from CheckBox
                      textArea.setText("your Text here");
                  }
              }
      });
      

      【讨论】:

      • +1 表示stateChanged()。这可以通过使用e.getSource() 使其更通用。可以检查文本区域的Document 是否删除。
      猜你喜欢
      • 2014-02-02
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 2020-12-30
      相关资源
      最近更新 更多