package com.aiflow.SQL;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class zxb extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Container contentPane;// 整个容器
private JLabel label_VPNUname;// vpn名称
private JLabel label_VPNUname_x;// vpn名称
private JLabel label_VPNPhone;// vpn名称
private JTextField textField_VPNUname;
private JPanel jPanel_phone;
private JButton jbutton_1;
private JScrollPane scroll;
List<String> list = null;
FileOutputStream fos;
public zxb() {
super();
// try {
// UIManager
// .setLookAndFeel("org.fife.plaf.Office2003.Office2003LookAndFeel");
// } catch (Exception e) {
// e.printStackTrace();
// }
initSelf();
initComponets();
}
JFrame jf = new JFrame();
private void initSelf() {
jf.setVisible(true);
jf.setTitle("配置告警短信的号码");
jf.setSize(350, 560);
jf.setLocationRelativeTo(null);//居中
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//
jf.setResizable(true); // 去掉最大化
contentPane = jf.getContentPane();
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage("img/Flow Chart.gif");
jf.setIconImage(img);
}
public void initComponets() {
jPanel_phone = new JPanel();
jPanel_phone.setBorder(BorderFactory.createTitledBorder("短信号码配置"));
GridLayout gridLayout = new GridLayout(0,2);//网格布局,不限制行,2列
jPanel_phone.setLayout(gridLayout);
label_VPNUname = new JLabel("vpn名称", SwingConstants.CENTER);
label_VPNPhone = new JLabel("短信号码配置", SwingConstants.CENTER);
jPanel_phone.add(label_VPNUname);
jPanel_phone.add(label_VPNPhone);
list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for (int i = 0; i < list.size(); i++) {
label_VPNUname_x = new JLabel(list.get(i), SwingConstants.CENTER);
textField_VPNUname = new JTextField();
jPanel_phone.add(label_VPNUname_x);
jPanel_phone.add(textField_VPNUname);
}
scroll = new JScrollPane(jPanel_phone);//将需要滚动条的Panel直接当参数传入
JPanel bottom = new JPanel();
jbutton_1 = new JButton("保存退出");
jbutton_1.setEnabled(true);
jbutton_1.addActionListener(this);
bottom.add(jbutton_1);
jPanel_phone.add(bottom);
contentPane.add(scroll);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// new PhoneFrame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new zxb();
}
});
}
}
我想获取每个textField_VPNUname文本框的值,知道的告诉下,谢了
7 个解决方案
#1
可以用jpanel_phone.getComponent()来得到容器中的对象
scroll = new JScrollPane(jPanel_phone);//将需要滚动条的Panel直接当参数传入
JPanel bottom = new JPanel();
jbutton_1 = new JButton("显示在a里输入的值");
jbutton_1.setEnabled(true);
jbutton_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JTextField)jPanel_phone.getComponent(3)).getText());
}
});
bottom.add(jbutton_1);
jPanel_phone.add(bottom);
#2
大哥你这只能打印出来一个个值啊,你看清我的问了么,我是要获取所有的文本框的值
#3
兄弟,所有文本框的值,不也是一个一个获取的嘛
#4
jbutton_1的actionlistener增加
jbutton_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i=0;i<jPanel_phone.getComponentCount();i++ ){
Object obj=jPanel_phone.getComponent(i);
if(obj instanceof JTextField){
JOptionPane.showMessageDialog(null, ((JTextField)jPanel_phone.getComponent(i)).getText(),"",JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
#5
是啊,但是你这就只能获取第一个啊,你也没注释怎么获取后面的啊,帮忙弄一下跑出来我看看,谢了
#6
看上面的,循环一下就出来了
#7
恩,谢了哥们
#1
可以用jpanel_phone.getComponent()来得到容器中的对象
scroll = new JScrollPane(jPanel_phone);//将需要滚动条的Panel直接当参数传入
JPanel bottom = new JPanel();
jbutton_1 = new JButton("显示在a里输入的值");
jbutton_1.setEnabled(true);
jbutton_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JTextField)jPanel_phone.getComponent(3)).getText());
}
});
bottom.add(jbutton_1);
jPanel_phone.add(bottom);
#2
大哥你这只能打印出来一个个值啊,你看清我的问了么,我是要获取所有的文本框的值
#3
兄弟,所有文本框的值,不也是一个一个获取的嘛
#4
jbutton_1的actionlistener增加
jbutton_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i=0;i<jPanel_phone.getComponentCount();i++ ){
Object obj=jPanel_phone.getComponent(i);
if(obj instanceof JTextField){
JOptionPane.showMessageDialog(null, ((JTextField)jPanel_phone.getComponent(i)).getText(),"",JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
#5
是啊,但是你这就只能获取第一个啊,你也没注释怎么获取后面的啊,帮忙弄一下跑出来我看看,谢了
#6
看上面的,循环一下就出来了
#7
恩,谢了哥们