package demo20110610;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/*
* this class is panel "configuration";
* the purpose of this class is channel configuration,
* it will provide some channels for importing data from excel files or SQL Server DataBase Tables.
*/
//config
public class configPanelCopy extends JPanel {
int chanNumber = 32;
public JTextField[] jtx1 = new JTextField[chanNumber];
public JTextField[] jtx2 = new JTextField[chanNumber];
public JTextField[] jtx3 = new JTextField[chanNumber];
public JButton[] jb1 = new JButton[chanNumber];
public JButton[] jb2 = new JButton[chanNumber];
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setName("test");
configPanelCopy config = new configPanelCopy();
jf.add(config);
jf.setVisible(true);
jf.setBounds(10, 10, 600, 500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public configPanelCopy() {
for (int i = 0; i < chanNumber; i++) {
jtx1[i] = new JTextField("name", 10);
jtx2[i] = new JTextField("function", 10);
jtx3[i] = new JTextField("max value", 8);
jb1[i] = new JButton("Insert");// insert button
jb2[i] = new JButton("Zero");// dispose
System.out.println(i);
jb1[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*???如何实现按钮监听,让每行的文本框从外部导入数据到所在行文本框????*/
例如:String[] abs= {bufferredRead(c//d.txt)
jtx1[i].setText( abs [0]);
jtx2[i].setText(abs [1]);
jtx3[i].setText(abs [2]);
//当然此actionPerformed方法是错误的,不能实现其中功能
}
});
jb2[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*????如何实现按钮监听,使得每行文本框清零???*/
}
});
}
JPanel editPanel = new JPanel();
// JCheckBox group
JCheckBox[] chan = new JCheckBox[chanNumber];
for (int i = 0; i < chanNumber; i++) {
int j = i + 1;
String chanName = "CHANNEL" + j;
chan[i] = new JCheckBox(chanName);
}
editPanel.setLayout(new GridLayout(chanNumber, 1));
JPanel[] subeditPanel = new JPanel[chanNumber];
for (int i = 0; i < chanNumber; i++) {
subeditPanel[i] = new JPanel();
subeditPanel[i].add(chan[i]);
subeditPanel[i].add(jtx1[i]);
subeditPanel[i].add(jtx2[i]);
subeditPanel[i].add(jtx3[i]);
subeditPanel[i].add(jb1[i]);
subeditPanel[i].add(jb2[i]);
editPanel.add(subeditPanel[i]);
// ScrollPanel.setViewportView(editPanel);
}
JScrollPane ScrollPanel = new JScrollPane(editPanel);// ???
ScrollPanel.setViewportView(editPanel);
ScrollPanel.setWheelScrollingEnabled(true);
ScrollPanel.setPreferredSize(new Dimension(20, 20));
ScrollPanel.revalidate();
ScrollPanel.repaint();
JPanel jrp = new JPanel();
JButton saveAll = new JButton("Save Channel Configuration");
JButton disposeAll = new JButton("Depose Channel Configuration");
jrp.add(saveAll);
jrp.add(disposeAll);
this.setLayout(new GridLayout(1,1));
this.add(ScrollPanel);
this.setSize(300,500);
}
}
另外:如果以上代码功能实现后,如何实现保存,然后下次打开面板时各文本框是配置之后的内容?
谢谢各位前辈指点!
感谢CSDN周老师解决问题:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
/*
* this class is the editor of the optional panel "configuration";
* the purpose of this class is channel configuration,
* it will provide some channels for importing data from different sensors.
*/
//config
public class configPanelCopy extends JPanel
{
int chanNumber = 32;
public JTextField[] jtx1 = new JTextField[chanNumber];
public JTextField[] jtx2 = new JTextField[chanNumber];
public JTextField[] jtx3 = new JTextField[chanNumber];
public JButton[] jb1 = new JButton[chanNumber];
public JButton[] jb2 = new JButton[chanNumber];
public configPanelCopy()
{
for (int i = 0; i < chanNumber; i++)
{
jtx1[i] = new JTextField("name", 10);
jtx2[i] = new JTextField("function", 10);
jtx3[i] = new JTextField("max value", 8);
jb1[i] = new JButton("Insert");// insert button
jb2[i] = new JButton("Zero");// dispose
System.out.println(i);
final int t = i;
jb1[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d://1.txt")));
jtx1[t].setText(in.readLine());
jtx2[t].setText(in.readLine());
jtx3[t].setText(in.readLine());
in.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
});
jb2[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtx1[t].setText("");
jtx2[t].setText("");
jtx3[t].setText("");
}
});
}
JPanel editPanel = new JPanel();
// JCheckBox group
JCheckBox[] chan = new JCheckBox[chanNumber];
for (int i = 0; i < chanNumber; i++)
{
int j = i + 1;
String chanName = "CHANNEL" + j;
chan[i] = new JCheckBox(chanName);
}
editPanel.setLayout(new GridLayout(chanNumber, 1));
JPanel[] subeditPanel = new JPanel[chanNumber];
for (int i = 0; i < chanNumber; i++)
{
subeditPanel[i] = new JPanel();
subeditPanel[i].add(chan[i]);
subeditPanel[i].add(jtx1[i]);
subeditPanel[i].add(jtx2[i]);
subeditPanel[i].add(jtx3[i]);
subeditPanel[i].add(jb1[i]);
subeditPanel[i].add(jb2[i]);
editPanel.add(subeditPanel[i]);
// ScrollPanel.setViewportView(editPanel);
}
JScrollPane ScrollPanel = new JScrollPane(editPanel);// ???
ScrollPanel.setViewportView(editPanel);
ScrollPanel.setWheelScrollingEnabled(true);
ScrollPanel.setPreferredSize(new Dimension(20, 20));
ScrollPanel.revalidate();
ScrollPanel.repaint();
JPanel jrp = new JPanel();
JButton saveAll = new JButton("Save Channel Configuration");
JButton disposeAll = new JButton("Depose Channel Configuration");
jrp.add(saveAll);
jrp.add(disposeAll);
this.setLayout(new GridLayout(1,1));
this.add(ScrollPanel);
this.setSize(300,500);
}
public static void main(String[] args)
{
JFrame jf = new JFrame();
jf.setName("test");
configPanelCopy config = new configPanelCopy();
jf.add(config);
jf.setVisible(true);
jf.setBounds(10, 10, 600, 500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}