Combobox 在void方法中修改JLabel
请我需要你的帮助,我正在努力做到这一点,我现在想要3-4天,但我做不到。 我有两个班,一个班和一个班。 我在Class1的一个方法中有一个jlabel,我想通过按下Class2中的一个按钮来修改它Combobox 在void方法中修改JLabel,combobox,jlabel,Combobox,Jlabel,请我需要你的帮助,我正在努力做到这一点,我现在想要3-4天,但我做不到。 我有两个班,一个班和一个班。 我在Class1的一个方法中有一个jlabel,我想通过按下Class2中的一个按钮来修改它 public class MainForm { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) {
public class MainForm {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainForm window = new MainForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(400, 200, 488, 322);
frame.setTitle("ShutDown");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
/**
* Time BOX
*/
JComboBox<String> timeBox = new JComboBox<String>();
timeBox.setBounds(73, 142, 90, 20);
timeBox.addItem("Days");
timeBox.addItem("Hours");
timeBox.addItem("Minutes");
timeBox.addItem("Seconds");
timeBox.addItem("Right now");
timeBox.setSelectedItem("Minutes");
frame.getContentPane().add(timeBox);
String getTimeBox = timeBox.getSelectedItem().toString();
/**
* The label info.
*/
JLabel labelInfo = new JLabel("");
labelInfo.setBounds(73, 209, 310, 14);
frame.getContentPane().add(labelInfo);
labelInfo.setText(getTimeBox);
}
我尝试了很多,但都不起作用,我需要在那个按钮中编写什么代码才能从时间框(组合框)中获取selectedItem
然后把它放在那个标签上?首先,你需要两者的参考
JComboBox
和JLable
类2中的对象。实现这一点的简单方法是在
MainForm
中声明各自的私有成员,而不是initialize
方法的本地成员,并将它们传递给构造函数或使用setter方法。让我们考虑引用的名称分别是<代码> JCOMBOBOX 和<代码> JLabele/Buff>。 现在,您可以使用以下语法从匿名类的
actionPerformed
方法引用它们,使用此语法Class2.this.jComboBox
&Class2.this.jLable
(jComboBox&jLable是对象引用)
Class2
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}