phoenix980613 2021-02-02 15:42 采纳率: 0%
浏览 26

Java中如何把JTextField输入的值传入JComboBox窗口中

  • 写回答

1条回答 默认 最新

  • allway2 2021-02-02 16:45
    关注


    // Java  Program to create a checkbox  
    // and add or remove items from it 
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;

    class JComboBoxJTextField extends JFrame implements ItemListener, ActionListener {

        // frame
        static JFrame f;

        // label
        static JLabel l, l1;

        // combobox
        static JComboBox c1;

        // textfield to add and delete items
        static JTextField tf;

        // main class
        public static void main(String[] args) {
            // create a new frame
            f = new JFrame("frame");

            // create a object
            JComboBoxJTextField s = new JComboBoxJTextField();

            // set layout of frame
            f.setLayout(new FlowLayout());

            // array of string contating cities
            String s1[] = {};

            // create checkbox
            c1 = new JComboBox(s1);

            // create textfield
            tf = new JTextField(16);

            // create add and remove buttons
            JButton b = new JButton("添加");

            // add action listener
            b.addActionListener(s);

            // add ItemListener
            c1.addItemListener(s);

            // create a new panel
            JPanel p = new JPanel();

            // add combobox to panel
            p.add(c1);

            p.add(tf);
            p.add(b);

            f.setLayout(new FlowLayout());

            // add panel to frame
            f.add(p);

            // set the size of frame
            f.setSize(700, 200);

            f.show();
        }

        // if button is pressed
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();

            c1.addItem(tf.getText());

        }

        @Override
        public void itemStateChanged(ItemEvent e) {
            System.out.println(c1.getSelectedItem());

        }

    }

    评论

报告相同问题?

悬赏问题

  • ¥50 CCD工业视觉相机检测出现光边
  • ¥20 C++初高中竞赛题,devc++可以通过的
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现
  • ¥50 python自动地图截图脚本
  • ¥20 瑞萨RA4M1芯片刷写为arduino r4 minima
  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目