How to set the Title of a Frame in java via Combobox?

问题: I want to create something like the image below, when the user select the year, the month and the day from the Combobox options, those actions will change the Title and it...

问题:

I want to create something like the image below, when the user select the year, the month and the day from the Combobox options, those actions will change the Title and it has to change according to the selected data, it's something simple, I am still newbie

enter image description here

So far I have done this, the problem is that it does't work, how can I make it?, could you help me please?

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

回答1:

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

So, you need a method in your class like:

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

Then from the 3 ActionListeners you just invoke the changTitle() method.`


回答2:

I've fixed few things in your code and now it works. Try and see. Main changes are:

In setTitle("Today is "+ texd+ "of "+ textm + "of " +texty); in your code, variables textd, textm and texty are out of scope (meaning they are declared inside each actionPerformed() method. So they are not available/visible outside those actionPerformed() methods.). So I made them instance variables of DateForm_Complete class.

Then I called setTitle("Today is "+ textd+ " of "+ textm + " of " +texty); from each actionPerformed() method. Because I guess your requirement is to update the title as soon as each combo box value is changed.

There was a typo in texd variable name as well.

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();
  }
}
  • 发表于 2019-01-14 23:19
  • 阅读 ( 319 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除