java问题怎样实现jtextarea的监听

beizhu.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){... beizhu.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
remarks=beizhu.getText().trim(); }
});beizhu为jtextarea,remarks为String类型要怎样修改代码,出错在beizhu.addActionListener。没有这个监听
展开
 我来答
悦文斓月
推荐于2016-10-23 · 历史爱好者 法律顾问
悦文斓月
采纳数:505 获赞数:11895

向TA提问 私信TA
展开全部
Java 在JList上的监听事件
import javax.swing.event.*;
jmport javax.swing.*;
public interface ListSelectionListener extends EventListener {
public void valueChanged(ListSelectionEvent e);
}

实现 valueChanged

实例

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SelectingJListSample {
public static void main(String args[]) {
String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
JFrame frame = new JFrame("Selecting JList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist = new JList(labels);
JScrollPane scrollPane1 = new JScrollPane(jlist);
frame.add(scrollPane1, BorderLayout.CENTER);

ListSelectionListener listSelectionListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent listSelectionEvent) {
System.out.println("First index: " + listSelectionEvent.getFirstIndex());
System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
boolean adjust = listSelectionEvent.getValueIsAdjusting();
System.out.println(", Adjusting? " + adjust);
if (!adjust) {
JList list = (JList) listSelectionEvent.getSource();
int selections[] = list.getSelectedIndices();
Object selectionValues[] = list.getSelectedValues();
for (int i = 0, n = selections.length; i < n; i++) {
if (i == 0) {
System.out.println(" Selections: ");
}
System.out.println(selections[i] + "/" + selectionValues[i] + " ");
}
}
}
};
jlist.addListSelectionListener(listSelectionListener);

frame.setSize(350, 200);
frame.setVisible(true);
}

实例二
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;//由于ListSelectionEvent是swing的事件,不是awt的事件,因此我们必须import
//javax.swing.event.*。

public class JList7 extends JFrame implements ListSelectionListener
{
JList list = null;
JLabel label = null;
String[] s = {"美国","日本","大陆","英国","法国","意大利","澳洲","韩国"};

public JList7()
{
JFrame f = new JFrame("JList");
Container contentPane = f.getContentPane();
contentPane.setLayout(new BorderLayout());
label = new JLabel();

list = new JList(s);
list.setVisibleRowCount(5);
list.setBorder(BorderFactory.createTitledBorder("您最喜欢到哪个国家玩呢?"));
list.addListSelectionListener(this);

contentPane.add(label,BorderLayout.NORTH);
contentPane.add(new JScrollPane(list),BorderLayout.CENTER);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String args[])
{
new JList7();
}

public void valueChanged(ListSelectionEvent e)
{
System.out.println("eeee");
int tmp = 0;
String stmp = "您目前选取:";
int[] index = list.getSelectedIndices();//利用JList类所提供的getSelectedIndices()方法可得到用户所选取的所有
for(int i=0; i < index.length ; i++)//index值,这些index值由一个int array返回.
{
tmp = index[i];
stmp = stmp+s[tmp]+" ";
}
label.setText(stmp);
}
}

java中如何获取JList的值

有一个JList列表框,当双击Jlist中的某一项的时候,就会把此项的值Print出来。我已经实现了双击,怎么来获取值?

public Object getSelectedValue()用这个方法可以获得选则的值

Java JTextArea 的使用
package JTextArea;

import java.awt.*;

import javax.swing.*;

public class TextAreaDemo extends JFrame {
private DescriptionPanel descriptionPanel=new DescriptionPanel();

public static void main(String[] args) {
TextAreaDemo frame=new TextAreaDemo();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("林泉居士");
frame.setVisible(true);
}

public TextAreaDemo(){
descriptionPanel.setTitle("Canada");
String description="The Maple Leaf flag\n\n"+
"The Canadian National Flag was adopted by the Canadian "+
"Parliament on October 22,1964 and was proclaimed into law "+
"by Her Majesty Queen Elizabeth II (the Queen of Canada) on "+
"february 15,1965.The Canadian Flag(Colloquially known "+
"as The Maple Leaf Flag) is a red flag of the proportions "+
"two by length and one by width,containing in tis center a "+
"white square,with a single red stylized eleven-point "+
"mapleleaf centered in the white square.";

descriptionPanel.setDescription(description);
descriptionPanel.setImageIcon(new ImageIcon("image/a.gif"));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(descriptionPanel,BorderLayout.CENTER);
}

}

class DescriptionPanel extends JPanel{
private JLabel jlblTitle=new JLabel();
private JLabel jlblImage=new JLabel();
private JTextArea jtaDescription;

public DescriptionPanel(){
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(jlblImage,BorderLayout.CENTER);
panel.add(jlblTitle,BorderLayout.SOUTH);

JScrollPane scrollPane=new JScrollPane(jtaDescription=new JTextArea());

jlblTitle.setHorizontalAlignment(JLabel.CENTER);

jlblTitle.setFont(new Font("SansSerif",Font.BOLD,16));
jtaDescription.setFont(new Font("serif",Font.PLAIN,14));

jtaDescription.setLineWrap(true);
jtaDescription.setWrapStyleWord(true);
jtaDescription.setEditable(false);

scrollPane.setPreferredSize(new Dimension(200,100));

setLayout(new BorderLayout(5,5));
add(scrollPane,BorderLayout.CENTER);
add(panel,BorderLayout.WEST);
}

public void setTitle(String title){
jlblTitle.setText(title);
}

public void setImageIcon(ImageIcon icon){
jlblImage.setIcon(icon);
Dimension dimension=new Dimension(icon.getIconWidth(),icon.getIconHeight());
jlblImage.setPreferredSize(dimension);
}

public void setDescription(String text){
jtaDescription.setText(text);
}

}
匿名用户
2013-07-31
展开全部
监听不是随便加的,ActionListener只是监听点击事件,最典型就是点击按钮。你觉得对TextArea做什么,会触发这个事件?最好想清楚要做什么功能,然后在确定为哪个控件加哪种监听。顺便说一下,你对匿名类的使用应该也有错。actionPerformed方法里用到了remarks、beizhu这两个变量,那他们就必须是final的,否则语法错误,估计你也没注意这点。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式