我的问题在于,我调用JComboBox,我的要求是想在按钮监听事件中得到JComboBox的值,我对swing也是现学现用,所以不知道为什么得不到值
为什么我把String s1 = combo1.getItemAt(combo1.getSelectedIndex()).toString();//方法1
拿到button1中就报错了,请教谢谢。package tf.dt.credit.view;/**
* @author dingting
* @date 2008-10-20
* @ 需要调用类DateChooserJButton.java
*/import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComboBox;public class DemoForDateChooser
extends JFrame { private JFormattedTextField formattedTextField; //文本框 /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true); //显示面板
}
catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("报文生成器"); //整个输入框的title
getContentPane().setLayout(null);
setBounds(500, 300, 483, 337); //第一二个参数控制面板输入框在哪个位置显示,第三四个参数控制面板输入框的宽和高
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击对话框叉时直接关闭窗体 final JPanel panel = new JPanel();
panel.setLayout(null); //设置布局格式为空
panel.setBounds(0, 0, 375, 107); //控制面板内容在面板中的显示位置和宽高
getContentPane().add(panel); formattedTextField = new JFormattedTextField(); //面板中的文本框
formattedTextField.setBounds(68, 48, 175, 23); //设置文本框的显示位置和大小 final JButton button = new JButton();
button.addActionListener(new ActionListener() { //启用按钮监听
public void actionPerformed(ActionEvent e) { System.out.println("dingting-->formattedTextField-->1:" +
formattedTextField.getValue());
DateChooser mDateChooser = new DateChooser(formattedTextField);
System.out.println("dingting-->formattedTextField-->2:" +
formattedTextField.getValue());
Point p = button.getLocationOnScreen(); //得到按钮的坐标值
// System.out.println("dingting-->p:"+p);
p.y = p.y + 30;
// System.out.println("dingting--p.y:"+p.y);
mDateChooser.showDateChooser(p); //点击按钮显示整个日期控件的处理
formattedTextField.requestFocusInWindow(); //表示在文本框中显示
System.out.println("dingting-->formattedTextField:" +
formattedTextField.getValue()); }
}); button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button); final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label); panel.add(formattedTextField); //把文本框添加进面板显示 final JLabel label2 = new JLabel();
label2.setText("类型:");
label2.setBounds(10, 87, 81, 23);
panel.add(label2); String[] s = {
"报文1", "报文2", "报文3"}; JComboBox combo1 = new JComboBox(s);
combo1.setBounds(70, 85, 81, 23);
combo1.setEditable(true); //下拉列是可编辑状态
panel.add(combo1); String s1 = combo1.getItemAt(combo1.getSelectedIndex()).toString();//方法1
//String s2 = combo1.getSelectedItem().toString();//方法2
//int i2 = Integer.parseInt( s2 );
System.out.println("dingting-->s1"+s1); final JButton button1 = new JButton();
button1.addActionListener(new ActionListener() { //传值用
public void actionPerformed(ActionEvent e) {
System.out.println("dingting-->s1"+s1);
}
});
button1.setBounds(250, 85, 99, 23); //第四个参数控制按钮长度
button1.setText("确定");
panel.add(button1); }
}
为什么我把String s1 = combo1.getItemAt(combo1.getSelectedIndex()).toString();//方法1
拿到button1中就报错了,请教谢谢。package tf.dt.credit.view;/**
* @author dingting
* @date 2008-10-20
* @ 需要调用类DateChooserJButton.java
*/import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComboBox;public class DemoForDateChooser
extends JFrame { private JFormattedTextField formattedTextField; //文本框 /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true); //显示面板
}
catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("报文生成器"); //整个输入框的title
getContentPane().setLayout(null);
setBounds(500, 300, 483, 337); //第一二个参数控制面板输入框在哪个位置显示,第三四个参数控制面板输入框的宽和高
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击对话框叉时直接关闭窗体 final JPanel panel = new JPanel();
panel.setLayout(null); //设置布局格式为空
panel.setBounds(0, 0, 375, 107); //控制面板内容在面板中的显示位置和宽高
getContentPane().add(panel); formattedTextField = new JFormattedTextField(); //面板中的文本框
formattedTextField.setBounds(68, 48, 175, 23); //设置文本框的显示位置和大小 final JButton button = new JButton();
button.addActionListener(new ActionListener() { //启用按钮监听
public void actionPerformed(ActionEvent e) { System.out.println("dingting-->formattedTextField-->1:" +
formattedTextField.getValue());
DateChooser mDateChooser = new DateChooser(formattedTextField);
System.out.println("dingting-->formattedTextField-->2:" +
formattedTextField.getValue());
Point p = button.getLocationOnScreen(); //得到按钮的坐标值
// System.out.println("dingting-->p:"+p);
p.y = p.y + 30;
// System.out.println("dingting--p.y:"+p.y);
mDateChooser.showDateChooser(p); //点击按钮显示整个日期控件的处理
formattedTextField.requestFocusInWindow(); //表示在文本框中显示
System.out.println("dingting-->formattedTextField:" +
formattedTextField.getValue()); }
}); button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button); final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label); panel.add(formattedTextField); //把文本框添加进面板显示 final JLabel label2 = new JLabel();
label2.setText("类型:");
label2.setBounds(10, 87, 81, 23);
panel.add(label2); String[] s = {
"报文1", "报文2", "报文3"}; JComboBox combo1 = new JComboBox(s);
combo1.setBounds(70, 85, 81, 23);
combo1.setEditable(true); //下拉列是可编辑状态
panel.add(combo1); String s1 = combo1.getItemAt(combo1.getSelectedIndex()).toString();//方法1
//String s2 = combo1.getSelectedItem().toString();//方法2
//int i2 = Integer.parseInt( s2 );
System.out.println("dingting-->s1"+s1); final JButton button1 = new JButton();
button1.addActionListener(new ActionListener() { //传值用
public void actionPerformed(ActionEvent e) {
System.out.println("dingting-->s1"+s1);
}
});
button1.setBounds(250, 85, 99, 23); //第四个参数控制按钮长度
button1.setText("确定");
panel.add(button1); }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货