我在下面的代码中创建了如下UI。我有一个串行选项卡和继电器选项卡,还在其中添加了按钮。我还有6个编号为1-6的按钮。 我想在jtable中添加值。当单击串口选项卡添加时,如果单击中继选项卡添加的selected.how中的任何一个按钮,则应该添加相同的内容。
我怀疑是:
如果按下按钮1并再次添加值按下按钮2时,表格应为空,应允许新值添加到表格中,如果单击后退按钮1,则应再次显示值之前已添加到按钮1中,并且还允许添加新值,这应该与所有按钮相同。
我尝试使用哈希映射列表,但我无法获得最佳方法。
我的代码是:
buttonone=new JButton("1");
buttonone.setBounds(80, 40, 50, 35);
buttonone.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttonone);
buttontwo=new JButton("2");
buttontwo.setBounds(180, 40, 50, 35);
buttontwo.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttontwo);
buttonthree=new JButton("3");
buttonthree.setBounds(80, 100, 50, 35);
buttonthree.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttonthree);
buttonfour=new JButton("4");
buttonfour.setBounds(180, 100, 50, 35);
buttonfour.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttonfour);
buttonfive=new JButton("5");
buttonfive.setBounds(80, 160, 50, 35);
buttonfive.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttonfive);
buttonsix=new JButton("6");
buttonsix.setBounds(180, 160, 50, 35);
buttonsix.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
buttonid=Integer.parseInt(s);
}
});
buttonpanel.add(buttonsix);
digitalpanel=new JPanel();
digitalpanel.setLayout(null);
digitalpanel.setBounds(430, 30, 380, 70);
digitalpanel.setBorder(BorderFactory.createTitledBorder("Digital Input"));
contentPane.add(digitalpanel);
digitalbtn1=new JButton("Input 1");
digitalbtn1.setBounds(80, 30, 80, 25);
digitalbtn1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
if(s.equals("Input 1"))
{
buttonid=7;
}
}
});
digitalpanel.add(digitalbtn1);
digitalbtn2=new JButton("Input 2");
digitalbtn2.setBounds(220, 30, 80, 25);
digitalbtn2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=e.getActionCommand();
if(s.equals("Input 2"))
{
buttonid=8;
}
}
});
digitalpanel.add(digitalbtn2);
relaytab = new JPanel();
relaytab.setLayout(null);
String onoff[]={"ON","OFF"};
String time[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24","25","26","27","28","29","30","31",
"32","33","34","35","36","37","38","39","40","41","42","43","44","45","46",
"47","48","49","50","51","52","53","54","55","56","58","59","60","61","62",
"63","64","65","66","67","68","69","70","71","72","73","74","75","76","77",
"78","79","80","81","82","83","84","85","86","87","88","89","90","91","92",
"93","94","95","96","97","98","99","100","101","102","103","104","105","106",
"107","108","109","110","111","112","113","114","115","116","117","118","119",
"120","121","122","123","124","125","126","127","128","129","130","131","132",
"133","134","135","136","137","138","139","140","141","142","143","144","145",
"146","147","148","149","150","151","152","153","154","155","156","157","158",
"159","160","161","162","163","164","165","166","167","168","169","170","171",
"172","173","174","175","176","177","178","179","180","181","182","183","184",
"185","186","187","188","189","190","191","192","193","194","195","196","197",
"198","199","200","201","202","203","204","205","206","207","208","209","210",
"211","212","213","214","215","216","217","218","219","220","221","222","223",
"224","225","226","227","228","229","230","231","232","233","234","235","236",
"237","238","239","240","241","242","243","244","245","246","247","248","249",
"250","251","252","253","254","255",};
relaylabel1=new JLabel("1");
relaylabel1.setBounds(35, 47, 30, 30);
relaylabel1.setFont(new Font("serif",Font.BOLD,20));
relaytab.add(relaylabel1);
relay1combo=new JComboBox<String>(onoff);
relay1combo.setBounds(60,50,100,25);
relay1combo.setSelectedIndex(-1);
relaytab.add(relay1combo);
relay1field=new JComboBox<String>(time);
relay1field.setBounds(170,50,80,26);
relay1field.setSelectedIndex(0);
relaytab.add(relay1field);
relayadd1=new JButton("Add");
relayadd1.setBounds(260, 50, 70, 25);
relayadd1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(buttonid == 1)
{
model.addRow(new Object[]{relay1combo.getSelectedItem().toString(),relay1field.getSelectedItem().toString()});
}
}
});
relaytab.add(relayadd1);
relaylabel2=new JLabel("2");
relaylabel2.setBounds(35, 96, 30, 30);
relaylabel2.setFont(new Font("serif",Font.BOLD,20));
relaytab.add(relaylabel2);
relay2combo=new JComboBox<String>(onoff);
relay2combo.setBounds(60,100,100,25);
relay2combo.setSelectedIndex(-1);
relaytab.add(relay2combo);
relay2field=new JComboBox<String>(time);
relay2field.setBounds(170,100,80,26);
relay2field.setSelectedIndex(0);;
relaytab.add(relay2field);
relayadd2=new JButton("Add");
relayadd2.setBounds(260, 100, 70, 25);
relaytab.add(relayadd2);
relaylabel3=new JLabel("3");
relaylabel3.setBounds(35, 146, 30, 30);
relaylabel3.setFont(new Font("serif",Font.BOLD,20));
relaytab.add(relaylabel3);
relay3combo=new JComboBox<String>(onoff);
relay3combo.setBounds(60,150,100,25);
relay3combo.setSelectedIndex(-1);
relaytab.add(relay3combo);
relay3field=new JComboBox<String>(time);
relay3field.setBounds(170,150,80,26);
relay3field.setSelectedIndex(0);
relaytab.add(relay3field);
relayadd3=new JButton("Add");
relayadd3.setBounds(260, 150, 70, 25);
relaytab.add(relayadd3);
relaylabel4=new JLabel("4");
relaylabel4.setBounds(35, 196, 30, 30);
relaylabel4.setFont(new Font("serif",Font.BOLD,20));
relaytab.add(relaylabel4);
relay4combo=new JComboBox<String>(onoff);
relay4combo.setBounds(60,200,100,25);
relay4combo.setSelectedIndex(-1);
relaytab.add(relay4combo);
relay4field=new JComboBox<String>(time);
relay4field.setBounds(170,200,80,26);
relay4field.setSelectedIndex(0);
relaytab.add(relay4field);
relayadd4=new JButton("Add");
relayadd4.setBounds(260, 200, 70, 25);
relaytab.add(relayadd4);
serialporttab = new JPanel();
serialporttab.setLayout(null);
serialportlabel=new JLabel("Select Serial Port");
serialportlabel.setBounds(18, 77, 100, 30);
serialporttab.add(serialportlabel);
String ports[]={"Port 1","Port 2","Port 3","Port 4"};
serialportcombobox=new JComboBox<String>(ports);
serialportcombobox.setBounds(125, 80, 220, 25);
serialportcombobox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String s=serialportcombobox.getSelectedItem().toString();
HashMap<String, String> mainhash=seriallist.get(s);
if(serialportcommandcombo.getItemCount() == 0)
{
List<String> list=new ArrayList<String>();
for(String s1:mainhash.values())
{
list.add(s1);
}
for(int i=0;i<list.size();i++)
{
serialportcommandcombo.addItem(list.get(i));
}
}
else
{
serialportcommandcombo.removeAllItems();
List<String> list=new ArrayList<String>();
for(String s1:mainhash.values())
{
list.add(s1);
}
for(int i=0;i<list.size();i++)
{
serialportcommandcombo.addItem(list.get(i));
}
}
}
});
serialporttab.add(serialportcombobox);
commandselection=new JLabel("Select command");
commandselection.setBounds(18, 137, 100, 30);
serialporttab.add(commandselection);
serialportcommandcombo=new JComboBox<String>();
serialportcommandcombo.setBounds(125, 140, 220, 25);
serialportcommandcombo.setSelectedIndex(-1);
serialporttab.add(serialportcommandcombo);
serialadd=new JButton("Add");
serialadd.setBounds(230, 260, 100, 25);
serialadd.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
if(serialportcommandcombo.getSelectedItem()==null)
{
JOptionPane.showMessageDialog(contentPane,"Select command is empty,Please select the command", "Error", JOptionPane.ERROR_MESSAGE);
}
else if(serialportcommandcombo.getSelectedItem()!=null)
{
if(buttonid == 1)
{
model.addRow(new Object[]{serialportcombobox.getSelectedItem().toString(),serialportcommandcombo.getSelectedItem().toString()});
for(int i=0;i<model.getRowCount();i++)
{
list1.add((String) model.getValueAt(i, 0));
}
System.out.println(list1);
}
}
}
});
serialporttab.add(serialadd);
tabbedpane=new JTabbedPane(JTabbedPane.TOP);
tabbedpane.setBounds(30, 300, 370, 340);
tabbedpane.addTab("Serial Port",serialporttab);
tabbedpane.addTab("Relay",relaytab);
tabbedpane.setBorder(BorderFactory.createTitledBorder(""));
contentPane.add(tabbedpane);
eventlist=new JPanel();
eventlist.setBounds(430, 110, 380, 490);
eventlist.setLayout(null);
eventlist.setBorder(BorderFactory.createTitledBorder("Event List"));
contentPane.add(eventlist);
String[] columnnames={"Type","Data"};
model = new DefaultTableModel(columnnames, 0);
eventtabel=new JTable(model);
eventtabel.setRowHeight(20);
JScrollPane scroll=new JScrollPane(eventtabel);
scroll.setBounds(10, 20, 360, 460);
eventlist.add(scroll);
哪种方法是好的方法?