【问题标题】:How to set the Title of a Frame in java via Combobox?如何通过 Combobox 在 java 中设置框架的标题?
【发布时间】:2019-06-07 11:46:54
【问题描述】:

我想创建如下图所示的内容,当用户从 Combobox 选项中选择年、月和日时,这些操作将更改标题,并且必须根据所选数据进行更改,这很简单,我还是新手

到目前为止,我已经做到了,问题是它不起作用,我该怎么做?请你帮帮我吗?

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class DateForm_Complete extends JFrame {

    private JLabel          year, month, day;
    private JComboBox       cmonth, cday, cyear;

    public DateForm_Complete() {

        setTitle("Date Selection");
        setSize(400,100);
        setupWidgets();
        setVisible(true);
    }

    private void setupWidgets() {
        year=   new JLabel("Year");
        month=  new JLabel("Month");
        day=    new JLabel("Day");
        cyear=  new JComboBox();
        cmonth= new JComboBox();
        cday=   new JComboBox();

        setLayout(new GridLayout (2,3));

        add(year);   add(month);    add(day);
        add(cyear);  add(cmonth);   add(cday);

        for (int i=1900; i<2019; i++)   
        {
            cyear.addItem(i);
        }

        String months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};

        for (int i=0; i<12; i++)
        {
            cmonth.addItem(months[i]);
        }

        for (int i=1; i<32; i++)    
        {
            cday.addItem(i);
        }
        setupEvents();
    }

    private void setupEvents() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        cyear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ev) {
                JComboBox combo = (JComboBox)ev.getSource();
                String texty = (String)combo.getSelectedItem(); 
            }
        });

        cmonth.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ev) {
                JComboBox combo = (JComboBox)ev.getSource();
                String textm = (String)combo.getSelectedItem();
            }
        });

        cday.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ev) {
                JComboBox combo = (JComboBox)ev.getSource();
                String textd = (String)combo.getSelectedItem();     
            }
        });
        setTitle("Today is "+ texd+ "of "+ textm + "of " +texty);               
    }   
    public static void main(String[] args) {

        new DateForm_Complete();        
    }
}

【问题讨论】:

  • 给定一个对JFrame的引用,只需调用它的setTitle方法

标签: java swing arraylist actionlistener jcombobox


【解决方案1】:

Whenever an item in a combo box is selected you need to reset the entire string you want to display as the title.

所以,你需要一个方法在你的类中,比如:

public void changeTitle()
{
    String year = cyear.getSeletedItem().toString();
    String month = cmonth.getSelectedItem().toString();
    String day = cday.getSelectedItem().toString();

    setTitle("Today is "+ day + "of "+ month + "of " + year);      
}

然后从 3 个 ActionListener 中调用 changTitle() 方法。`

【讨论】:

    【解决方案2】:

    我在您的代码中修复了一些问题,现在它可以工作了。试试看。主要变化是:

    在代码中的setTitle("Today is "+ texd+ "of "+ textm + "of " +texty); 中,变量textdtextmtexty 超出范围(意味着它们在每个actionPerformed() 方法中声明。因此它们在@987654326 之外不可用/可见@ 方法。)。所以我将它们设为DateForm_Complete 类的实例变量。

    然后我从每个actionPerformed() 方法调用setTitle("Today is "+ textd+ " of "+ textm + " of " +texty);。因为我猜您的要求是在每个组合框值更改后立即更新标题。

    texd 变量名也有错别字。

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class DateForm_Complete extends JFrame {
    
      private JLabel          year, month, day;
      private JComboBox       cmonth, cday, cyear;
    
      private String texty = "1900";
      private String textm = "January";
      private String textd = "1";
    
      public DateForm_Complete() {
    
        setTitle("Date Selection");
        setSize(400,100);
        setupWidgets();
        setVisible(true);
      }
    
      private void setupWidgets() {
        year=   new JLabel("Year");
        month=  new JLabel("Month");
        day=    new JLabel("Day");
        cyear=  new JComboBox();
        cmonth= new JComboBox();
        cday=   new JComboBox();
    
        setLayout(new GridLayout (2,3));
    
        add(year);   add(month);    add(day);
        add(cyear);  add(cmonth);   add(cday);
    
        for (int i=1900; i<2019; i++)
        {
          cyear.addItem(i);
        }
    
        String months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    
        for (int i=0; i<12; i++)
        {
          cmonth.addItem(months[i]);
        }
    
        for (int i=1; i<32; i++)
        {
          cday.addItem(i);
        }
        setupEvents();
      }
    
      private void setupEvents() {
    
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        cyear.addActionListener(new ActionListener() {
    
          @Override
          public void actionPerformed(ActionEvent ev) {
            JComboBox combo = (JComboBox)ev.getSource();
            texty = combo.getSelectedItem().toString();
            setTitle("Today is "+ textd+ " of "+ textm + " of " +texty);
          }
        });
    
        cmonth.addActionListener(new ActionListener() {
    
          @Override
          public void actionPerformed(ActionEvent ev) {
            JComboBox combo = (JComboBox)ev.getSource();
            textm = (String)combo.getSelectedItem();
            setTitle("Today is "+ textd+ " of "+ textm + " of " +texty);
          }
        });
    
        cday.addActionListener(new ActionListener() {
    
          @Override
          public void actionPerformed(ActionEvent ev) {
            JComboBox combo = (JComboBox)ev.getSource();
            textd = combo.getSelectedItem().toString();
            setTitle("Today is "+ textd+ " of "+ textm + " of " +texty);
          }
        });
    
      }
      public static void main(String[] args) {
    
        new DateForm_Complete();
      }
    }
    

    【讨论】:

    • (1-) 是的,这可行,但为什么会创建不必要的实例变量并重复代码以重置标题?如果您想将标题的格式更改为年、月和日,该怎么办?现在您需要在 3 个地方更改代码。使用方法简化代码,减少代码重复。
    • I've fixed few things in your code and now it works - 另外,只是发布没有解释的代码并不能帮助 OP 了解您更改了什么或更改它的原因。
    • @camickr,是的,从设计的角度来看,我可以在这段代码中改进很多东西。但我只是想提供一个更改最少的可行解决方案(无需重写)。
    • @camickr,有时,尤其是对于新开发人员,它有助于显示工作代码而不是解释。而且由于我的代码只有很少的更改,OP 将能够比较并查看他/她做错了什么。
    • 所有答案都应该对解决方案有某种解释,而不仅仅是代码转储。特别是对于新开发人员,这样他们就可以了解更改的上下文/原因。只是看看他们可能不明白它为什么起作用的差异。
    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 2023-03-23
    • 2013-03-17
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    相关资源
    最近更新 更多