java swing 中如何获取下拉列表的值给文本框?
2个回答
展开全部
package demo;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Demo extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Demo frame = new Demo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Demo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 357, 91);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"选项一","选项二","选项三"}));
comboBox.setBounds(12, 11, 92, 22);
contentPane.add(comboBox);
textField = new JTextField();
textField.setBounds(253, 11, 76, 22);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("\u786E\u5B9A");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(comboBox.getSelectedItem().toString());
}
});
button.setBounds(137, 10, 84, 25);
contentPane.add(button);
}
}
更多追问追答
追问
我的咋要报错啊
和你一样的
追答
能不能给我看看错误呢,加我吧1182830044
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询