【问题标题】:Display the selection of the combo Box in the Selection panel's text field在“选择”面板的文本字段中显示组合框的选择
【发布时间】:2017-11-02 15:55:46
【问题描述】:

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

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();

//使用网格布局 生日面板.setLayout(新的 GridLayout(1,1)); birthdayPanel.add(new 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().add(面板);

}

}

【问题讨论】:

  • 问题是什么?发布正确的minimal reproducible example 来说明问题。
  • (1-) 您还没有说明问题所在。 2)您发布的代码未格式化。了解如何在发布代码时正确使用论坛是您​​希望人们阅读它。 3) 那不是MCVE。您的问题是关于组合框和在面板中设置文本。所以创建一个简单的MCVE。您所需要的只是一个带有组合框和文本字段的框架。然后,您将 ActionLIsener 添加到组合框中,以在选择项目时更新文本字段。整个程序大约有 20-25 行代码,便于调试。一旦你理解了基本概念,你就可以修复你的真实代码。

标签: java swing actionlistener


【解决方案1】:

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

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

【讨论】:

  • 我编辑了代码,你能帮帮我吗?感谢您的帮助
  • JTextField textField = null; 为什么你的 textField 为空?
  • 另外,按照惯例,你的常量名应该像 ART 一样大写,你的方法名应该以小写字母开头,后面每个单词的第一个字母应该是大写,比如 private void TermPanel() 应该是 @987654326 @。您的方法名称应该描述该方法的作用。所以initialize()termPanel() 的更好名称。希望我的评论有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多