我正在尝试为标签添加点按手势。但它不起作用:
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.System.*;
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;
private ListSelectionModel ListSelectionModel;
public ListModel() {
setLayout(new FlowLayout());
DefaultListModel 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);
text.setEditable(false);
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.setModel(model);
list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
text.setText(list.getSelectedValue().toString());
}
});
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();
}
}
我为图像视图尝试了相同的代码,它完美地运行。为什么这不适用于标签?