Vag*_*ism 1 java swing jcombobox
我有一个组合框,你可以在代码中看到从表中获取值.所以在我点击确定后,表格的值会发生变化.如何在不关闭和打开jframe的情况下看到compobox的这些新值?我今天做了很多关于java.awt.EventQueue.invokeLater和其他工作人员的研究,但我不能使它工作,我是java新手和编程通用.所以这是代码:
public class Compo extends JFrame implements ActionListener
{//start of class Compo
//start of variables
private JComboBox<String> CompoBox;
private String array[];
private JButton okButton;
private JPanel panel;
//end of variables
public Compo ()
{//start of Compo method
super("Example");
panel=new JPanel(null);
//table = new String[3];
array= new String[3];
array[0]="alpha";
array[1]="beta";
array[2]="charlie";
CompoBox= new JComboBox<>(array);
CompoBox.setBounds(50, 70, 100, 20);
panel.add(CompoBox);
okButton=new JButton("ok");
okButton.setBounds(50, 120, 70, 30);
okButton.setActionCommand("ok");
okButton.addActionListener(this);
panel.add(okButton);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setVisible(true);
}//end of compo method
@Override
public void actionPerformed(ActionEvent event)
{//start of actionperformed
String testString=event.getActionCommand();
if (testString.equals("ok"))
{//start of if
for (int i = 0; i < array.length; i++)
{
String sample= array[i];
array[i]=sample+"aa";
}
}//end of if
}//end of aciton performed
}//end of class Compo
Run Code Online (Sandbox Code Playgroud)
这应该是你正在寻找的答案,希望你会接受它:
if (testString.equals("ok")) {
CompoBox.removeAllItems();
for (int i = 0; i < array.length; i++) {
String sample = array[i];
CompoBox.addItem(sample + "aa");
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句.通用comqo-pox是这样完成的:
CompoBox = new JComboBox<String>(array);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8314 次 |
| 最近记录: |