如何通过 Combobox 在 java 中设置框架的标题?
How to set the Title of a Frame in java via Combobox?
我想创建如下图所示的内容,当用户 select 从组合框选项中输入年、月和日时,这些操作将更改标题,并且必须根据select数据,很简单,我还是菜鸟
到目前为止我已经这样做了,问题是它不起作用,我该怎么做?请你帮我吗?
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();
}
}
无论何时选择组合框中的项目,您都需要重置要显示为标题的整个字符串。
所以,您的 class 中需要一个方法,例如:
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()
方法中声明. 所以它们不在 available/visible 那些 actionPerformed()
方法之外。)。所以我将它们设为 DateForm_Complete
class.
的实例变量
然后我从每个 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();
}
}
我想创建如下图所示的内容,当用户 select 从组合框选项中输入年、月和日时,这些操作将更改标题,并且必须根据select数据,很简单,我还是菜鸟
到目前为止我已经这样做了,问题是它不起作用,我该怎么做?请你帮我吗?
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();
}
}
无论何时选择组合框中的项目,您都需要重置要显示为标题的整个字符串。
所以,您的 class 中需要一个方法,例如:
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()
方法中声明. 所以它们不在 available/visible 那些 actionPerformed()
方法之外。)。所以我将它们设为 DateForm_Complete
class.
然后我从每个 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();
}
}