private JComboBox jcbx1 ;
String[] kindname1={"饮料","干果","小吃"};
jcbx1= new JComboBox(kindname1);
有哪位高手给指点下,小弟想实现如果选择饮料,点击保存后饮料选项变为灰色或者不可用
private JComboBox jcbx1 ;
String[] kindname1={"饮料","干果","小吃"};
jcbx1= new JComboBox(kindname1);
有哪位高手给指点下,小弟想实现如果选择饮料,点击保存后饮料选项变为灰色或者不可用
实现代码,添加了一个listener类jComboBoxListener用来监听jcombobox的action。
combobox中的值用一个vector容器来管理。
[code="java"]Vector jcbx1Vec = new Vector();//Anddy[/code]
[code="java"]
package desktopapplication1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import org.eclipse.swt.widgets.Listener;
import java.sql.*;
import java.text.*;
import java.util.Vector;
public class Book implements ActionListener
{ Vector jcbx1Vec = new Vector();//Hyvi
private JButton btn1,btn2,btn3,btn4;
private JTextField jtfd1,jtfd2,jtfd3,jtfd5,jtfd6,
jtfd7,jtfd8,jtfd9,jtfd10;
private JComboBox jcbx1 ;
private JTextArea jta;
private Connection con;
private PreparedStatement pstmt1,pstmt2;
private JFrame frame;
public Book()
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame=new JFrame("宁夏同心县乡巴里KTV小票打印系统");
Container content=frame.getContentPane();
Toolkit tool=frame.getToolkit();
Dimension wndsize=tool.getScreenSize();
JLabel lb1=new JLabel("货号:");
lb1.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb1.setForeground(Color.BLUE);
lb1.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb2=new JLabel("货名:");
lb2.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb2.setForeground(Color.BLUE);
lb2.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb3=new JLabel("价格:");
lb3.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb3.setForeground(Color.BLUE);
lb3.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb4=new JLabel("包厢名:");
lb4.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb4.setForeground(Color.BLUE);
lb4.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb5=new JLabel("种类:");
lb5.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb5.setForeground(Color.BLUE);
lb5.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb6=new JLabel("免钱:");
lb6.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb6.setForeground(Color.BLUE);
lb6.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb7=new JLabel("小计:");
lb7.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb7.setForeground(Color.BLUE);
lb7.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb8=new JLabel("日期:");
lb8.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb8.setForeground(Color.BLUE);
lb8.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb9=new JLabel("总计:");
lb9.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb9.setForeground(Color.BLUE);
lb9.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb10=new JLabel("数量:");
lb10.setFont(new Font("方正行楷简体", Font.BOLD, 18));
lb10.setForeground(Color.BLUE);
lb10.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lb11=new JLabel("温馨提示:");
jtfd1=new JTextField();
jtfd2=new JTextField();
jtfd3=new JTextField("0");
String[] kindname1={"酒","饮料","干果","小吃","纸"};
String[] kindname2={"霸王别姬(豪华包)","浪人情歌(大包)","酒当歌(中包)","雪在飞(中包)","自由人(中包)","为你写诗(中包)",
"东风破(中包)","忘情水(小包)","七里香(小包) "};
//jcbx1=new JComboBox(kindname1);
//Anddy
jcbx1Vec.add("item1");
jcbx1Vec.add("item2");
jcbx1Vec.add("item3");
jcbx1Vec.add("item4");
jcbx1= new JComboBox();
jcbx1.setModel(new javax.swing.DefaultComboBoxModel(jcbx1Vec));
jcbx1.addActionListener(new jComboBoxListener());
// jcbx1.setEnabled(false);
jcbx1.setName("znm"); // NOI18N
jtfd5=new JTextField();
jtfd6=new JTextField("0");
jtfd7=new JTextField("0");
jtfd8=new JTextField();
jtfd9=new JTextField("0");
jtfd10=new JTextField("0");
jtfd8.addFocusListener(new FocusHandler());
jta=new JTextArea("请您妥善保管好自己的物品");
jta.setLineWrap(true);
btn1=new JButton("添加");
btn2=new JButton("打印");
btn3=new JButton("撤消");
btn4=new JButton("退出");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
JPanel pl1=new JPanel();
JPanel pl2=new JPanel();
JPanel pl3=new JPanel();
JPanel pl4=new JPanel();
pl1.setLayout(new GridLayout(5,4,6,6));
pl1.add(lb1);
pl1.add(jtfd1);
pl1.add(lb2);
pl1.add(jtfd2);
pl1.add(lb10);
pl1.add(jtfd10);
pl1.add(lb3);
pl1.add(jtfd3);
pl1.add(lb4);
pl1.add(jcbx1);
pl1.add(lb5);
pl1.add(jtfd5);
pl1.add(lb6);
pl1.add(jtfd6);
pl1.add(lb7);
pl1.add(jtfd7);
pl1.add(lb8);
pl1.add(jtfd8);
pl1.add(lb9);
pl1.add(jtfd9);
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
pl2.setLayout(gridbag);
constraints.weightx=constraints.weighty=10.0;
constraints.fill=constraints.BOTH;
gridbag.setConstraints(lb11,constraints);
constraints.weightx=1;
constraints.gridwidth=1;
pl2.add(lb11,constraints);
gridbag.setConstraints(jta,constraints);
constraints.weightx=9;
constraints.gridheight=3;
constraints.gridwidth=3;
constraints.insets=new Insets(10,15,10,20);
pl2.add(jta,constraints);
GridBagLayout gridbag1=new GridBagLayout();
GridBagConstraints constraints1=new GridBagConstraints();
constraints1.weightx=1.0;
constraints1.fill=constraints1.BOTH;
pl3.setLayout(gridbag1);
//gridbag1.setConstraints(pl1,constraints1);
constraints1.weighty=0.8;
constraints1.gridwidth=constraints1.REMAINDER;
pl3.add(pl1,constraints1);
//gridbag1.setConstraints(pl2,constraints1);
constraints1.weighty=0.2;
constraints1.gridheight=constraints.REMAINDER;
pl3.add(pl2,constraints1);
pl4.setLayout(new GridLayout(1,4,6,0));
pl4.add(btn1);
pl4.add(btn2);
pl4.add(btn3);
pl4.add(btn4);
content.setLayout(new BorderLayout());
content.add(pl3,BorderLayout.CENTER);
content.add(pl4,BorderLayout.SOUTH);
frame.setBounds(100,100,550,400);
frame.setResizable(false);
frame.setVisible(true);
try
{
String sqlStr1,sqlStr2;
sqlStr1="insert into book (bookname,bannercode,kindnumber,kindname,"+
"positionnumber,publishingcompany,publishtime,putintime,price,"+
"state,introduction,author) values(?,?,?,?,?,?,?,?,?,'在架',?,?)";
sqlStr2="delete from book where bannercode=?";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:library");
pstmt1=con.prepareStatement(sqlStr1);
pstmt2=con.prepareStatement(sqlStr2);
}
catch(ClassNotFoundException e)
{
}
catch(SQLException sqle)
{
}
}
class jComboBoxListener implements ActionListener{ //处理包厢的监听事件。
public void actionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// index=jComboBox2.getSelectedIndex();
String value= (String)jcbx1.getSelectedItem();//Anddy
if(value.equals("item1")){
//jcbx1.setEnabled(false);
//jcbx1.setEnabledAt(1,true);
jcbx1.removeItem("item1");
System.out.println(jcbx1Vec.toString());//用来调试,打印出来的是jcbx1Vec中的元素
}
if(value.equals("item2")){
jcbx1.removeItem("item2");
//jcbx1.setEnabled(false);
System.out.println(jcbx1Vec.toString());
}
if(value.equals("item3")){
jcbx1.removeItem("item3");
//jcbx1.setEnabled(false);
System.out.println(jcbx1Vec.toString());
}
if(value.equals("item4")){
jcbx1.removeItem("item4");
//jcbx1.setEnabled(false);
System.out.println(jcbx1Vec.toString());
}
}}
private JComboBox getComboBox(String[] strings, Color blue, Object object,
Object object2, Object object3, boolean b, boolean c) {
// TODO Auto-generated method stub
return null;
}
class FocusHandler implements FocusListener
{ java.util.Date today=new java.util.Date();
DateFormat format=DateFormat.getDateInstance();
String formatted=format.format(today);
public void focusGained(FocusEvent e)
{Object obj=(JTextField)e.getSource();
if(obj==jtfd8)
{jtfd8.setText(formatted);
}
}
public void focusLost(FocusEvent e)
{
}
}
public void actionPerformed(ActionEvent e)
{
final String str1;
String str2, str3, str4, str5, str6, str7, str8, str9, str10, str11,str13;
// str1=jtfd1.getText().trim();//货物编号
str2=jtfd2.getText().trim();//货名
str3=jtfd3.getText().trim();//价格
str4=(String)jcbx1.getSelectedItem();//包厢列表
str5=jtfd5.getText().trim();//种类
str6=jtfd6.getText().trim();//免钱
str7=jtfd7.getText().trim();//小计
str8=jtfd8.getText().trim();//日期
str9=jtfd9.getText().trim();//总计
str10=jtfd10.getText().trim();//数量
str11=jta.getText().trim();//温馨提示
// System.out.println("str10="+str10);
//新建
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
if (!jtfd1.getText().equals("")&& !jtfd10.getText().equals("0"))
{
nulword();
}
else {
JOptionPane.showMessageDialog((Component) null, " 货物编号错误", "error", JOptionPane.PLAIN_MESSAGE);
}
}
});
//打印按钮事件
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt)
{
if (!jtfd1.getText().equals("")&& !jtfd10.getText().equals("0"))
{
nulword();
reload();
}
else {
JOptionPane.showMessageDialog((Component) null, " 货物编号错误", "error", JOptionPane.PLAIN_MESSAGE);
}
}
});
}
//新建订单
void nulword(){
jtfd1.setText("");
jtfd2.setText("");
jtfd3.setText("");
jtfd5.setText("");
jtfd6.setText("");
jtfd7.setText("");
jtfd8.setText("");
jtfd9.setText("");
jtfd10.setText("");
//jta.setText("");
System.out.println("11");
}
void reload()
{//读取数据库货物基本信息操作
String str13;
String val1=(String)jtfd1.getText();
System.out.println("val1="+val1);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection("jdbc:odbc:xblDB");//需要修改
Statement sql = con.createStatement();
System.out.println("val2="+val1);
ResultSet rs = sql.executeQuery("select 编号,货名,种类 ,售价 from 货物基础信息表 where 编号='" + jtfd1.getText().trim()
+ "'");
while (rs.next()) {
String goodid = rs.getString(1);
System.out.println("goodid="+goodid);
String goodname = rs.getString(2);
String goodsort= rs.getString(3);
String goodprice= rs.getString(4);
//String s1=String.valueOf(goodprice);
//System.out.println("goodprice="+goodprice);
jtfd2.setText(goodname);
jtfd5.setText(goodsort);
jtfd3.setText(goodprice);
double p=Integer.parseInt(goodprice);
Integer free=Integer.parseInt(jtfd6.getText().trim());
Integer number=Integer.parseInt(jtfd10.getText().trim());//强制转换数量
Integer goodprice2=Integer.parseInt(goodprice);//强制类型转换 价格
Integer count1=number*goodprice2;//小计
Integer count2=count1-free;
String s1 = String.valueOf(count1);//转换小计为字符串类型
String s2=String.valueOf(count2);
jtfd7.setText(s1);
jtfd9.setText(s2);
str13=jtfd2.getText().trim();
System.out.println("str13="+str13);
}
} catch(SQLException ex)
{
System.out.println("Unable to access the database");
}
catch(Exception ex)
{
System.out.println("Exception raised is:"+ex);
}
finally {
}
}
public static void main(String[]args)
{new Book();
}
}
[/code]