如何通过组合框在Java中设置框架的标题?

时间:2019-01-13 01:37:04

标签: java swing arraylist actionlistener jcombobox

我想创建类似下图的图像,当用户从组合框选项中选择年,月和日时,这些操作将更改标题,并且必须根据所选数据进行更改,这很简单,我还是新手

enter image description here

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

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

2 个答案:

答案 0 :(得分:0)

每当在组合框中选择一个项目时,都需要重设要显示为标题的整个字符串。

因此,您需要在类中使用以下方法:

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()方法。

答案 1 :(得分:0)

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

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

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

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