重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面纸:3元",Label.LEFT);label2=newLabel("钢笔:5元",Label.LEFT);label3=newLabel("书:10元",Label.LEFT);label4=newLabel("袜子:8元",Label.LEFT);button1=newButton("加入购物车");button2=newButton("加入购物车");button3=newButton("加入购物车");button4=newButton("加入购物车");button5=newButton("查看购物车");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只钢笔、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本书、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一双袜子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车");}}我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。
目前创新互联已为1000多家的企业提供了网站建设、域名、网站空间、网站改版维护、企业网站设计、洪江网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
这个很好写的,代码如下:
private ListDept recursionDept(ListDept ld){
for(int i=0; ild.size(); i++) {
Dept d = ld.get(i)
loop(d);
}
}
private void loop(Dept d) {
ListDept children=service.getChildDept(d.id);
if (children.size() 0) {
d.setChildren(children); // 这里假设子列表属性的名字就是children
for(int j=0; jchidren.size(); j++){
loop(children.get(j);
}
}
}
这个题目对初学者来说比较难的一点是,得想明白要自己建一个递归方法(loop)
import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{ Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面纸:3元",Label.LEFT);
label2=new Label("钢笔:5元",Label.LEFT);
label3=new Label("书:10元",Label.LEFT);
label4=new Label("袜子:8元",Label.LEFT);
button1=new Button("加入购物车");
button2=new Button("加入购物车");
button3=new Button("加入购物车");
button4=new Button("加入购物车");
button5=new Button("查看购物车");
text=new TextArea("商品有:"+"\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ text.append("一个面纸、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只钢笔、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本书、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一双袜子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"总价为:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("购物车");
}
}
我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。
代码如下:
import java.util.ArrayList;
import java.util.List;
class Org {
private String id;
private String name;
private String pid;
public Org(String id, String name, String pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
@Override
public String toString() {
return "Org [id=" + id + ", name=" + name + ", pid=" + pid + "]";
}
}
public class App {
static void find(ListOrg list, String pid) {
list.stream().filter(p - p.getPid().equals(pid))
.forEach(org - {
System.out.println(org);
find(list, org.getId());
});
}
public static void main(String[] args) {
ListOrg list = new ArrayList();
list.add(new Org("111", "公司", "0"));
list.add(new Org("222", "部门", "111"));
list.add(new Org("333", "小组", "222"));
list.add(new Org("444", "员工1", "333"));
list.add(new Org("555", "员工2", "333"));
find(list, "0");
System.out.println("------------------------------------");
find(list, "111");
}
}
运行结果: