목록에서 사용자의 선택을 가져 와서 Jbox를 사용하여 문자열로 변환하고 싶습니다. 어떻게 사용할 수 있도록 내용을 문자열로 변환 할 수 있습니까?개체를 문자열로 변환하는 방법은 무엇입니까?
public class Graph extends JFrame
{
private String temp;
public Graph()
{ }
public void CreateBox(String[] a)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jButton1 = new JButton("ok");
final JList jList1 = new JList(a);
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Object contents = jList1.getSelectedValue();
//System.out.println(contents);
setChoise((String)contents);//how can i convert it to string ?
}
});
JButton jButton2 = new JButton("close");
jButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
frame.add(jList1, "Center");
frame.add(jButton1,"South");
frame.add(jButton2,"North");
frame.setSize(300, 200);
frame.setVisible(true);
}
public void setChoise(String temp)
{
this.temp=temp;
}
public String getChoise()
{
return this.temp;
}
}
에 대한 목록을 사용하는 경우 너무에만이 작업을 수행 작동하지 않습니다? 'String contents = (String) jList1.getSelectedValue(); ' – Ascalonian
@Ascalonian 그건 작동하지 않습니다. Java는 합법적 인 Java가 아닙니다. – ajb
[자바에서 개체를 문자열로 변환하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/6055476/how-to-convert-object-to-string-in-java) –