リスト内の getSelection 値のコードに疑問があります。マウスを使用すると取得した値が複製されますが、キーボードを使用すると次のようになりません。
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import br.com.operacao.Paga;
public class Tela extends JPanel implements ActionListener{
private static final long serialVersionUID = 1L;
JLabel image;
public Tela() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(5, 5, 5, 5);
String labels[] = { "Coca-Cola", "Fanta Laranja", "Fanta-Uva",
"Sprite"};
final JList<String> list = new JList<String>(labels);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
list.setSelectedIndex(0);
JScrollPane pane = new JScrollPane();
pane.getViewport().add(list);
JPanel firstpanel = new JPanel();
firstpanel.add(pane);
firstpanel.setBackground(Color.BLACK);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
this.add(firstpanel,c);
image = new JLabel();
image.setFont(image.getFont().deriveFont(Font.ITALIC));
image.setHorizontalAlignment(JLabel.CENTER);
updateLabel(labels[list.getSelectedIndex()]);
image.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = c.weighty = 0.0;
this.add(image, c);
JScrollPane js = new JScrollPane();
js.setPreferredSize(new Dimension(110,110));
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.gridheight = 1;
this.add(js, c);
final JButton comprar = new JButton("Comprar");
comprar.setEnabled(false);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int selections[] = list.getSelectedIndices();
//String selectedValue = list.getSelectedValue();
Object selectionValues[] = list.getSelectedValues();
for (int i = 0, n = selections.length; i < n; i++) {
if (i == 0) {
System.out.println("Value" + selectionValues[i] );
}}
comprar.setEnabled(true);
}
});
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
this.add(comprar, c);
comprar.addActionListener(new Paga());
final JButton confirma = new JButton("Confirmar");
confirma.setEnabled(false);
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
this.add(confirma,c);
/*
c.gridx = 2;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
this.add(new JButton("Button #6"), c);
*/
}
@Override
public void actionPerformed(ActionEvent e) {
//String selectedValue = list.getSelectedValue();
JList<String> jl = (JList<String>)e.getSource(); //some doubt here to display the correct images when selected item from list?
String refriName = jl.getSelectedValue();
updateLabel(refriName);
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("assets/" + name + ".jpg");
image.setIcon(icon);
//image.setToolTipText("A drawing of a " + name.toLowerCase());
if (icon != null) {
image.setText(null);
} else {
image.setText("Image not found");
}
}
/** Returns an ImageIcon, or null if the path was invalid.*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Tela.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
これは私のメインクラスです:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import br.com.operacao.Tela;
public class Executa {
public static void main(String[] args) {
Tela display = new Tela();
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(new Tela());
f.setTitle("Maquina de Refrigerante");
f.pack();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(280, 380);
}
}
それで、私のactionListenerの問題は何ですか?また、表示できない画像に問題がありますが、なぜですか?