gpt4 book ai didi

java - Actionlistener 将 Jlist 信息输出到文本区域

转载 作者:行者123 更新时间:2023-12-01 22:18:48 25 4
gpt4 key购买 nike

有人可以帮我解决以下问题吗?我想实现一个 Action 监听器,将有关突出显示的 Jlist 对象的信息输出到已经存在的文本区域中。

这是我编写的代码:

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

public class ListModel extends JFrame {

private JList list;
private DefaultListModel model;
private JTextField inputf;
private JTextField inpute;
private JTextArea text;
private JPanel p1;
private JPanel p2;

public ListModel() {
setLayout(new FlowLayout());
model = new DefaultListModel();
list = new JList(model);
JButton addButton = new JButton( "Lägg till" );
model.addElement("Kajsa Åslund");
model.addElement("Erik Carlsson");
model.addElement("John Åkesson");
model.addElement("Per-arne Ingvarsson");
model.addElement("Rebecka Asp");
model.addElement("Linnéa Åslund");
model.addElement("Åsa-Nisse Strong");
model.addElement("Super Lasse");
model.addElement("Alexander Ahl");
model.addElement("Ann Ahl");
model.addElement("Bo Sten");
addButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
final String name=inputf.getText() + " " + inpute.getText();

model.addElement( name );
}
}
);
JButton removeButton =
new JButton( "Ta bort" );

removeButton.addActionListener(
new ActionListener() {

public void actionPerformed( ActionEvent event )
{
setTitle("Borttagning");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
model.removeElement(list.getSelectedValue());
setTitle("Personer");
}
}
);

text = new JTextArea(5, 20);

inputf = new JTextField();
inputf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}

});
inpute = new JTextField();
inpute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}

});
list.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION );

inpute.setBounds(5, 5, 100, 100);
inpute.setPreferredSize(new Dimension(120,20));
inputf.setBounds(10, 10, 150, 150);
inputf.setPreferredSize(new Dimension(120,20));
JScrollPane scroll = new JScrollPane(list);
scroll.setPreferredSize(new Dimension(200,200));

JPanel p2 = new JPanel();
p2.add(text);
p2.add(inputf);
p2.add(inpute);
p2.add(addButton);
p2.add(removeButton);
p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS));

JPanel p1 = new JPanel();
p1.add(scroll);


Container container = getContentPane();
container.add(p1);
container.add(p2);
container.setLayout(new FlowLayout());

setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( 500, 250 );
setVisible( true );
}

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

提前致谢:)

最佳答案

目前您只能从 list 中读取的数据是姓名,您可以通过使用 ListSelectionListener 来实现:

list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
text.setText(list.getSelectedValue().toString());
}
});

关于java - Actionlistener 将 Jlist 信息输出到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30394511/

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