码迷,mamicode.com
首页 > 其他好文 > 详细

2017-07-11(Jtree和table的应用)

时间:2017-07-11 21:08:39      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:evel   gui   ide   ddp   invoke   creates   except   商品   hang   

package gui;

import java.awt.BorderLayout;

public class Spml extends JFrame {

private JPanel contentPane;
private JTable table;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JButton btnNewButton_2;
private JButton btnNewButton_3;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Spml frame = new Spml();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Spml() {
final Vector vc=new Vector();
vc.add("烟");
vc.add("芙蓉王");
vc.add(20);
vc.add(1);

final Vector vc1=new Vector();
vc1.add("烟");
vc1.add("中华");
vc1.add(60);
vc1.add(1);

final Vector vc2=new Vector();
vc2.add("烟");
vc2.add("猴王");
vc2.add(10);
vc2.add(1);

final Vector vc3=new Vector();
vc3.add("酒");
vc3.add("青岛");
vc3.add(8);
vc3.add(1);

final Vector vc4=new Vector();
vc4.add("酒");
vc4.add("崂山");
vc4.add(6);
vc4.add(1);

final Vector vc5=new Vector();
vc5.add("酒");
vc5.add("伏特加");
vc5.add(200);
vc5.add(1);

final Vector vc6=new Vector();
vc6.add("日用品");
vc6.add("毛巾");
vc6.add(25);
vc6.add(1);

final Vector vc7=new Vector();
vc7.add("日用品");
vc7.add("肥皂");
vc7.add(20);
vc7.add(1);

final Vector vc8=new Vector();
vc8.add("日用品");
vc8.add("餐巾纸");
vc8.add(2);
vc8.add(1);
//vc.add(arg0)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 832, 517);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);


table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{"\u79CD\u7C7B", "\u7C7B\u540D", "\u4EF7\u683C", "\u6570\u91CF "},
},
new String[] {
"New column", "New column", "New column", "New column"
}
));
final DefaultTableModel dtm = (DefaultTableModel)table.getModel();
String[] str = {"种类","类名","价格","数量"};
dtm.setColumnIdentifiers(str);
table.setBackground(Color.GREEN);
table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));

JTree tree = new JTree();
tree.setBackground(Color.CYAN);
tree.setBorder(new CompoundBorder());
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent arg0) {
Object st = arg0.getPath().getLastPathComponent();


if(st.toString().equals("芙蓉王")){
dtm.addRow(vc);;
}else if(st.toString().equals("中华")){
dtm.addRow(vc1);
}else if(st.toString().equals("猴王")){
dtm.addRow(vc2);
}else if(st.toString().equals("青岛")){
dtm.addRow(vc3);
}else if(st.toString().equals("崂山")){
dtm.addRow(vc4);
}else if(st.toString().equals("伏特加")){
dtm.addRow(vc5);
}else if(st.toString().equals("毛巾")){
dtm.addRow(vc6);
}else if(st.toString().equals("肥皂")){
dtm.addRow(vc7);
}else if(st.toString().equals("餐巾纸")){
dtm.addRow(vc8);
}}
});
tree.setModel(new DefaultTreeModel(
new DefaultMutableTreeNode("商品目录") {
{
DefaultMutableTreeNode node_1;
node_1 = new DefaultMutableTreeNode("烟");
node_1.add(new DefaultMutableTreeNode("芙蓉王"));
node_1.add(new DefaultMutableTreeNode("中华"));
node_1.add(new DefaultMutableTreeNode("猴王"));
add(node_1);
node_1 = new DefaultMutableTreeNode("酒");
node_1.add(new DefaultMutableTreeNode("青岛"));
node_1.add(new DefaultMutableTreeNode("崂山"));
node_1.add(new DefaultMutableTreeNode("伏特加"));
add(node_1);
node_1 = new DefaultMutableTreeNode("日用品");
node_1.add(new DefaultMutableTreeNode("毛巾"));
node_1.add(new DefaultMutableTreeNode("肥皂"));
node_1.add(new DefaultMutableTreeNode("餐巾纸"));
add(node_1);
}
}
));

JButton btnNewButton = new JButton("\u5E94\u6536");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int L=table.getRowCount()-1;
int total=0;
for(int i=1;i<=L;i++){
total +=(int)table.getValueAt(i, 2);
}textField.setText(String.valueOf(total));
}
});

JLabel label = new JLabel("");

textField = new JTextField();
textField.setColumns(10);

JLabel label_1 = new JLabel("\u5B9E\u6536\uFF1A");

textField_1 = new JTextField();
textField_1.setColumns(10);

JButton btnNewButton_1 = new JButton("\u627E\u96F6");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a=Integer.parseInt(textField_1.getText())-Integer.parseInt(textField.getText());
textField_2.setText(String.valueOf(a));
}
});

textField_2 = new JTextField();
textField_2.setColumns(10);

btnNewButton_2 = new JButton("\u589E\u52A0");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dtm.addRow(vc);
dtm.addRow(vc1);
dtm.addRow(vc2);
dtm.addRow(vc3);
dtm.addRow(vc4);
dtm.addRow(vc5);
dtm.addRow(vc6);
dtm.addRow(vc7);
dtm.addRow(vc8);;
}
});

btnNewButton_3 = new JButton("\u5220\u9664");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap(25, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton_2)
.addGap(65)
.addComponent(btnNewButton))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton_3)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label_1)
.addGap(9)))
.addGap(32))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(73)
.addComponent(tree, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(141)
.addComponent(btnNewButton_1)))
.addGap(42)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(table, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label))
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(26, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(16)
.addComponent(label))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(table, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(tree, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(ComponentPlacement.RELATED, 332, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_2)
.addComponent(btnNewButton)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(30)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(btnNewButton_3)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(34))
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(386, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_1)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(60))
);
contentPane.setLayout(gl_contentPane);

}
}

2017-07-11(Jtree和table的应用)

标签:evel   gui   ide   ddp   invoke   creates   except   商品   hang   

原文地址:http://www.cnblogs.com/wgb190/p/7152343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!