我想创建如下图所示的内容,当用户从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();
}
}
每当选择组合框中的项目时,您需要重置要显示为标题的整个字符串。
所以,你需要一个类中的方法:
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个ActionListeners中调用changTitle()
方法
我已经在你的代码中修改了一些东西,现在它可以工作了。试试看。主要变化是:
在代码中的setTitle("Today is "+ texd+ "of "+ textm + "of " +texty);
中,变量textd
,textm
和texty
超出范围(意味着它们在每个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();
}
}