在“选择”面板的文本字段

时间:2017-11-02 15:55:47

标签: java swing actionlistener

我正在尝试在“选择”面板的文本字段中显示组合框的选择

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import javax.swing.AbstractButton;
  import javax.swing.BorderFactory;
 import javax.swing.JButton;
  import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JTextField;

 public class Department extends JFrame {

 private static final long serialVersionUID = 1L;

 public static final String Art = "Art";

 public static final String Biology = "Biology";

 public static final String Chemistry = "Chemistry";

 public static final String Computer_Science = "Computer_Science";
    public static final String Economics = "Economics";

     public static final String History = "History";

     public static final String Music = "Music";

    public static final String Philosophy = "Philosophy";
    public static final String Physics = "Physics";

     public static final String Psycholgy = "Psychology";
       public static final String Psychology = "Psychology";

  //constructor
  public Department() {
   setSize(700, 150);

  setLocationRelativeTo(null);

  setVisible(true);// making the frame visible

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("Banner Self Service For Brahmbhatt");

TermPanel();

}

//400 width and 500 height
    //private method with components
        private void TermPanel() {

//base panel
 JPanel panel = new JPanel();

 panel.setLayout(new FlowLayout());

 panel.setSize(700, 190);
 panel.setBackground(Color.darkGray);

 JPanel selectionPanel = new JPanel();
 selectionPanel.setSize(10, 10);

 selectionPanel.setLayout(new GridLayout(1, 1));
 JTextField zodiacSign = new JTextField(10);

//adding the instructions for creating the birthday panel
JPanel birthdayPanel = new JPanel();

//使用网格布局      birthdayPanel.setLayout(new GridLayout(1,1));      birthdayPanel.add(新JLabel("选择一个部门:"));

 birthdayPanel.setBackground(new Color(250, 230, 230));
 String[] choices = { "","Art","Biology", 
"Chemistry","Computer_Science","Economics","English", "History", "Music", 
"Mathematics", "Philosophy", "Physics", "Psychology"};

//Creating a comboBox
 final JComboBox<String> cb = new JComboBox<String>(choices);
 cb.setVisible(true);

 birthdayPanel.add(cb);
 panel.add(birthdayPanel);

 getContentPane().add(panel);
 JTextField textField = null;


  //Panels for the selection term field
 zodiacSign.setEnabled(true);//so that no one can use it as input field 
 zodiacSign.setEditable(true);//so that no one can edit the zodiac sign

 selectionPanel.add(zodiacSign); // adding the zodiacsign textfield to panel
 zodiacSign.setMaximumSize(new Dimension(1000, 500));   //setting minimu 
 dimensions for the zodiac panel
 zodiacSign.setMinimumSize(new Dimension(500, 150));     //setting minimum 
  dimensions for the zodia

 selectionPanel.setBorder(BorderFactory.createTitledBorder("Your Selection 
    is"));
 selectionPanel.setBackground(new Color(250, 230, 230));
 panel.add(selectionPanel);

//添加动作侦听器 cb.addActionListener(new ActionListener(){

public void actionPerformed (ActionEvent event)
{
    JComboBox cb = (JComboBox) event.getSource();
    Object selected = cb.getSelectedItem();
    textField.setText((String) cb.getSelectedItem());
}

});

的getContentPane()添加(面板);

}

}

1 个答案:

答案 0 :(得分:0)

你没有发布你的其他代码,所以我猜你忘了将监听器添加到JComboBox。 如果在ActionListener对象中声明此方法,则可以执行此操作以将此侦听器添加到JComboBox

JComboBox cb = new JComboBox(); //sample declaration
cb.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        //do your stuff here
    }
});