谁能解释一下Java中GridBagLayout的使用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谁能解释一下Java中GridBagLayout的使用?相关的知识,希望对你有一定的参考价值。

希望有一些例子,并对关键语句解释清楚。

参考技术A GridLayout的使用:
GridLayout的类层次结构图:
java.lang.Object
--java.awt.GridLayout

GridLayout比FlowLayout多了行和列的设置,也就是说你要先设置GridLayout共有几行
几列,就如同二维平面一般,然后你加
进去的组件会先填第一行的格子,然后再从第二行开始填,依此类扒,就像是一个个的
格子一般。而且GridLayout会将所填进去组
件的大小设为一样。
构造函数:GridLayout()建立一个新的GridLayout,默认值是1行1列。
GridLayout(int rows,int
cols)建立一个几行几列的GridLayout.
GridLayout(int rows,int cols, int hgap,int
vgap)建立一个几行几列的GridLayout,并设置组件的间距。
例子:GridLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo implements ActionListener
JPanel p1,p2,p3,p4;
int i=1;
JFrame f;
public CardLayoutDemo()
f=new JFrame();//当做top-level组件
Container contentPane=f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));

p1=new JPanel();
Button b=new Button("Change Card");
b.addActionListener(this);//当按下"Change
Card"时,进行事件监听,将会有系统操作产生。
p1.add(b); file://处理操作在52-64行.
contentPane.add(p1);

p2=new JPanel();
p2.setLayout(new FlowLayout());
p2.add(new JButton("first"));
p2.add(new JButton("second"));
p2.add(new JButton("third"));

p3=new JPanel();
p3.setLayout(new GridLayout(3,1));
p3.add(new JButton("fourth"));
p3.add(new JButton("fifth"));
p3.add(new JButton("This is the last button"));

p4=new JPanel();
p4.setLayout(new CardLayout());
p4.add("one",p2);
p4.add("two",p3);
/*要显示CardLayout的卡片,除了用show(Container parent,String
name)这个方法外

*,也可试试first(Container),next(Container),previous(Container),last
(Container)这
*四个方法,一样可以达到显示效果。
*/
((CardLayout)p4.getLayout()).show(p4,"one");

contentPane.add(p4);

f.setTitle("CardLayout");
f.pack();
f.setVisible(true);

f.addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);


);


public void actionPerformed(ActionEvent event)
switch(i)
case 1:
((CardLayout)p4.getLayout()).show(p4,"two");
break;
case 2:
((CardLayout)p4.getLayout()).show(p4,"one");
break;

i++;
if (i==3) i=1;
f.validate();

public static void main(String[] args)
new CardLayoutDemo();


1-3-5:GridBagLayout的使用:是java中最有弹性但也是最复杂的一种版面管理器。它
只有一种构造函数,但必须配合
GridBagConstraints才能达到设置的效果。
GridBagLayout的类层次结构图:
java.lang.Object
--java.awt.GridBagLayout
构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器。
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,int
gridwidth,int gridheight,double weightx,double weighty,
int anchor,int fill, Insets
insets,int ipadx,int ipady)建立一个新的GridBagConstraints对象
,并指定其参数的值。
参数说明:
gridx,gridy:设置组件的位置,gridx设置为GridBagConstraints.RELATIVE代表此组件
位于之前所加入组件的右边。

若将gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下
面。建议定义出

gridx,gridy的位置,以便以后维护程序。表示放在几行几列,gridx=0,gridy=0时放在
0行0列。
gridwidth,gridheight:用来设置组件所占的单位长度与高度,默认值皆为1。你可以使
用GridBagConstraints.REMAINDER常
量,代表此组件为此行或此列的最后一个组件,而且会占据所
有剩余的空间。
weightx,weighty:用来设置窗口变大时,各组件跟着变大的比例,当数字越大,表示组
件能得到更多的空间,默认值皆为0。
anchor:
当组件空间大于组件本身时,要将组件置于何处,有CENTER(默认值)、NORTH、
NORTHEAST、EAST、SOUTHEAST、
WEST、NORTHWEST可供选择。
insets:设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,默认为
(0,0,0,0).
ipadx,ipady:设置组件内的间距,默认值为0。

我们以前提过,GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我
们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。
例子:
GridBagLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDemo
public GridBagLayoutDemo()
JButton b;
GridBagConstraints c;
int
gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;

JFrame f=new JFrame();
GridBagLayout gridbag=new GridBagLayout();
Container contentPane=f.getContentPane();
contentPane.setLayout(gridbag);

b=new JButton("first");
gridx=0;
gridy=0;
gridwidth=1;
gridheight=1;
weightx=10;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("second");
gridx=1;
gridy=0;
gridwidth=2;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=50;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("third");
gridx=0;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("fourth");
gridx=1;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
b=new JButton("This is the last button");
gridx=2;
gridy=1;
gridwidth=1;
gridheight=2;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new
GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);
f.setTitle("GridBagLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);


);

public static void main(String[] args)
new GridBagLayoutDemo();

参考技术B GridBagLayout的使用:是java中最有弹性但也是最复杂的一种版面管理器。它只有一种构造函数,但必须配合
GridBagConstraints才能达到设置的效果。
GridBagLayout的类层次结构图:
java.lang.Object
--java.awt.GridBagLayout
构造函数:
GirdBagLayout()建立一个新的GridBagLayout管理器。
GridBagConstraints()建立一个新的GridBagConstraints对象。
GridBagConstraints(int gridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,
int anchor,int fill, Insets insets,int ipadx,int ipady)建立一个新的GridBagConstraints对象
,并指定其参数的值。
参数说明:
gridx,gridy:设置组件的位置,gridx设置为GridBagConstraints.RELATIVE代表此组件位于之前所加入组件的右边。
若将gridy设置为GridBagConstraints.RELATIVE代表此组件位于以前所加入组件的下面。建议定义出
gridx,gridy的位置,以便以后维护程序。表示放在几行几列,gridx=0,gridy=0时放在0行0列。

gridwidth,gridheight:用来设置组件所占的单位长度与高度,默认值皆为1。你可以使用GridBagConstraints.REMAINDER常
量,代表此组件为此行或此列的最后一个组件,而且会占据所有剩余的空间。

weightx,weighty:用来设置窗口变大时,各组件跟着变大的比例,当数字越大,表示组件能得到更多的空间,默认值皆为0。
anchor: 当组件空间大于组件本身时,要将组件置于何处,有CENTER(默认值)、NORTH、NORTHEAST、EAST、SOUTHEAST、
WEST、NORTHWEST可供选择。
insets:设置组件之间彼此的间距,它有四个参数,分别是上,左,下,右,默认为(0,0,0,0).
ipadx,ipady:设置组件内的间距,默认值为0。
我们以前提过,GridBagLayout里的各种设置都必须通过GridBagConstraints,因此当我们将GridBagConstraints的参数都设置
好了之后,必须new一个GridBagConstraints的对象出来,以便GridBagLayout使用。
例子:
GridBagLayoutDemo.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDemo
public GridBagLayoutDemo()
JButton b;
GridBagConstraints c;
int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;

JFrame f=new JFrame();

GridBagLayout gridbag=new GridBagLayout();
Container contentPane=f.getContentPane();
contentPane.setLayout(gridbag);

b=new JButton("first");
gridx=0;
gridy=0;
gridwidth=1;
gridheight=1;
weightx=10;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("second");
gridx=1;
gridy=0;
gridwidth=2;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=50;
ipady=0;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("third");
gridx=0;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("fourth");
gridx=1;
gridy=1;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=0;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

b=new JButton("This is the last button");
gridx=2;
gridy=1;
gridwidth=1;
gridheight=2;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(0,0,0,0);
ipadx=0;
ipady=50;
c=new GridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,
fill,inset,ipadx,ipady);
gridbag.setConstraints(b,c);
contentPane.add(b);

f.setTitle("GridBagLayout");
f.pack();
f.setVisible(true);
f.addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);


);

public static void main(String[] args)
new GridBagLayoutDemo();

参考技术C package layout;

import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JPanel;
public class GridBagConstraints1 extends JFrame
MenuBar mb;
PopupMenu popm;
Menu m1, m2, m3, m4, m5, m6;
MenuItem mi0, mi1, mi2, mi3, mi4;
MenuItem mcl0, mcl1, mcl2, mcl3;
MenuItem mcl4, mcl5, mcl6, mcl7, mcl8, mcl9;
TextArea ta;
TextArea tf;
JRadioButton r1, r2, r3, r4;
ButtonGroup bw, cw;
GridBagLayout gbl, gb2;
GridBagConstraints gc, gw;
TextField tf1, tfname, tfzcn;
JPasswordField tfpw, tfzcpw;
Button b1, b2;
JButton bdl, bzc, bzctj, bzcqx;
JPanel p, pdl, pzc, pn, pc;
JFrame fdl, fzc, fLoad;
JLabel lname, lpw, lzcn, lzcpw, laddress, labelsex, labelsex1, ln0, ln1;
List luser;
int k;
String swho = "所有人", s;
PrintWriter out;
BufferedReader in;
Socket s1, sdl;
String ip;
// ToolTip ttfs;
// Toolkit tkdl;
//Image pic;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

String date = new SimpleDateFormat(" HH:mm:ss ").format(
Calendar.
getInstance().getTime());

public GridBagConstraints1()
//super();
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
String strpw = new String(tfpw.getPassword());
out.println("Login" + "#" + "xx" + "#"
+ tfname.getText() + "#" + strpw);
dispose();
System.exit(0);

);

ImageIcon ww = new ImageIcon("room.gif");
ImageIcon jj = new ImageIcon("room1.gif");
ImageIcon aa = new ImageIcon("anduo.gif");
ImageIcon iin0 = new ImageIcon("g0.gif");
mi1 = new MenuItem("好友资料");
mi2 = new MenuItem("聊天记录");
mi3 = new MenuItem("查看IP");
mi4 = new MenuItem("传送文件");
popm = new PopupMenu();
popm.add(mi1);
popm.add(mi2);
popm.add(mi3);
mb = new MenuBar();
m1 = new Menu("设置");
m2 = new Menu("功能");
//m3=new Menu("界面设置");
m4 = new Menu("界面颜色");
m5 = new Menu("字体设置");
m6 = new Menu("字体大小");
mcl1 = new MenuItem("红色");
mcl2 = new MenuItem("绿色");
mcl3 = new MenuItem("青色");
mcl4 = new MenuItem("粗体");
mcl5 = new MenuItem("斜体");
mcl6 = new MenuItem("正常体");
mcl7 = new MenuItem("小号");
mcl8 = new MenuItem("中号");
mcl9 = new MenuItem("大号");

m4.add(mcl1);
m4.add(mcl2);
m4.add(mcl3);
m5.add(mcl4);
m5.add(mcl5);
m5.add(mcl6);

m6.add(mcl7);
m6.add(mcl8);
m6.add(mcl9);
mb.add(m1);
mb.add(m2);
m2.add(mi4);
//m1.add(m3);
m1.add(m4);
m1.add(m5);
m1.add(m6);
luser = new List();
luser.setForeground(Color.red);
luser.add(popm);
luser.setBackground(Color.pink);
ta = new TextArea(15, 40);
ta.setEditable(false);
ta.setForeground(Color.blue);
ta.setBackground(Color.pink);
tf = new TextArea();
tf.setBackground(Color.pink);
tf.setForeground(Color.blue);
b1 = new Button("发送");
b2 = new Button("下线");
p = new JPanel();
p.setBackground(Color.blue);
pn = new JPanel();
ln0 = new JLabel(iin0);
ln1 = new JLabel(iin0);
pc = new JPanel();
pc.setBackground(Color.blue);
pn.add(ta);
pn.add(ln0);
pn.add(ln1);
pn.setBackground(Color.blue);
pc.add(tf);

// pLoad=new JPanel();
ImageIcon iif3 = new ImageIcon("f3.gif");
JLabel lLoad = new JLabel(iif3);
//fLoad=new JFrame("登录等待中...");
//pLoad.add(lLoad);
//fLoad.getContentPane().add(pLoad);
//fLoad.setSize(400,300);
// p.add(tf);
p.add(b1);
p.add(b2);
setMenuBar(mb);
getContentPane().setLayout(new BorderLayout(10, 10));
fdl = new JFrame("登录界面");
pdl = new JPanel();
pdl.setBackground(Color.CYAN);
lname = new JLabel(" 用户名:");
lpw = new JLabel(" 密码:");
tfname = new TextField(16);
tfpw = new JPasswordField(12);
labelsex = new JLabel("性别:");
r1 = new JRadioButton("男");
r1.setBackground(Color.cyan);
//r1.setBackground(new Color(47,177,210));//设置颜色
r2 = new JRadioButton("女");
r2.setBackground(Color.cyan);
//r2.setBackground(new Color(47,177,210));//设置颜色
bw = new ButtonGroup();
bw.add(r1);
bw.add(r2);
//tkdl=new Toolkit();
//pic=tkdl.getImage("f0.gif");
bdl = new JButton("登录", ww);
bzc = new JButton("注册", ww);
gbl = new GridBagLayout();
gc = new GridBagConstraints();
pdl.setLayout(gbl);
fdl.getContentPane().add(pdl);
gc.anchor = GridBagConstraints.NORTHWEST;

Label lnull5 = new Label(" ");
gc.gridx = 0;
gc.gridy = 0;
gbl.setConstraints(lnull5, gc);
pdl.add(lnull5);

gc.gridx = 0;
gc.gridy = 1;
gbl.setConstraints(lname, gc);
pdl.add(lname);

gc.gridx = 1;
gc.gridy = 1;
gbl.setConstraints(tfname, gc);
pdl.add(tfname);

Label lnull6 = new Label(" ");
gc.gridx = 0;
gc.gridy = 2;
gbl.setConstraints(lnull6, gc);
pdl.add(lnull6);

gc.gridx = 0;
gc.gridy = 3;
gbl.setConstraints(lpw, gc);
pdl.add(lpw);

gc.gridx = 1;
gc.gridy = 3;
gbl.setConstraints(tfpw, gc);
pdl.add(tfpw);

Label lnull7 = new Label(" ");
gc.gridx = 0;
gc.gridy = 4;
gbl.setConstraints(lnull7, gc);
pdl.add(lnull7);

gc.gridx = 0;
gc.gridy = 5;
gbl.setConstraints(bdl, gc);
pdl.add(bdl);

gc.gridx = 1;
gc.gridy = 5;
gbl.setConstraints(bzc, gc);
pdl.add(bzc);

fzc = new JFrame("用户注册");
pzc = new JPanel();
pzc.setBackground(Color.green);
lzcn = new JLabel(" 注册名:");
tfzcn = new TextField(12);
lzcpw = new JLabel(" 注册密码:");
tfzcpw = new JPasswordField(10);
bzctj = new JButton("提交", aa);
bzcqx = new JButton("取消", aa);
gb2 = new GridBagLayout();
gw = new GridBagConstraints();
labelsex1 = new JLabel(" 性别:");
r3 = new JRadioButton("男");
r3.setBackground(Color.green);
//r1.setBackground(new Color(47,177,210));//设置颜色
r4 = new JRadioButton("女");
r4.setBackground(Color.green);
//r2.setBackground(new Color(47,177,210));//设置颜色
cw = new ButtonGroup();
cw.add(r3);
cw.add(r4);
laddress = new JLabel(" 邮箱地址:");
tf1 = new TextField(16);
//tkdl=new Toolkit();
pzc.setLayout(gb2);
fzc.getContentPane().add(pzc);
gw.anchor = GridBagConstraints.NORTHWEST;

Label lnull1 = new Label(" ");
gw.gridx = 0;
gw.gridy = 1;
gb2.setConstraints(lnull1, gw);
pzc.add(lnull1);

gw.gridx = 1;
gw.gridy = 2;
gb2.setConstraints(lzcn, gw);
pzc.add(lzcn);

gw.gridx = 2;
gw.gridy = 2;
gb2.setConstraints(tfzcn, gw);
pzc.add(tfzcn);

Label lnull0 = new Label(" ");
gw.gridx = 1;
gw.gridy = 3;
gb2.setConstraints(lnull0, gw);
pzc.add(lnull0);

gw.gridx = 1;
gw.gridy = 4;
gb2.setConstraints(lzcpw, gw);
pzc.add(lzcpw);

gw.gridx = 2;
gw.gridy = 4;
gb2.setConstraints(tfzcpw, gw);
pzc.add(tfzcpw);

Label lnull4 = new Label(" ");
gw.gridx = 0;
gw.gridy = 5;
gb2.setConstraints(lnull4, gw);
pzc.add(lnull4);

gw.gridx = 1;
gw.gridy = 6;
gb2.setConstraints(laddress, gw);
pzc.add(laddress);

gw.gridx = 2;
gw.gridy = 6;
gb2.setConstraints(tf1, gw);
pzc.add(tf1);

Label lnull2 = new Label(" ");
gw.gridx = 0;
gw.gridy = 7;
gb2.setConstraints(lnull2, gw);
pzc.add(lnull2);

gw.gridx = 1;
gw.gridy = 8;
gb2.setConstraints(labelsex1, gw);
pzc.add(labelsex1);

gw.gridx = 2;
gw.gridy = 8;
gb2.setConstraints(r3, gw);
pzc.add(r3);

gw.gridx = 3;
gw.gridy = 8;
gb2.setConstraints(r4, gw);
pzc.add(r4);

Label lnull3 = new Label(" ");
gw.gridx = 0;
gw.gridy = 9;
gb2.setConstraints(lnull3, gw);
pzc.add(lnull3);

gw.gridx = 1;
gw.gridy = 10;
gb2.setConstraints(bzctj, gw);
pzc.add(bzctj);

gw.gridx = 2;
gw.gridy = 10;
gb2.setConstraints(bzcqx, gw);
pzc.add(bzcqx);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
fzc.setSize(400, 300);
fzc.pack();
fzc.setLocation(250, 250);
fdl.setVisible(true);
fdl.setSize(300, 200);
fdl.pack();
fdl.setLocation(250, 250);
getContentPane().add(pc, BorderLayout.CENTER);
getContentPane().add(pn, BorderLayout.NORTH);
getContentPane().add(p, BorderLayout.SOUTH);
getContentPane().add(luser, BorderLayout.EAST);
setSize(400, 300);
pack();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);

public static void main(String[] args)
GridBagConstraints1 gbc=new GridBagConstraints1();

谁能解释一下这个python代码中提到的一行的工作原理?

【中文标题】谁能解释一下这个python代码中提到的一行的工作原理?【英文标题】:Can anyone explain me the working of a line mentioned in this python code? 【发布时间】:2021-04-29 04:19:17 【问题描述】:

请从下面的代码中解释这一行。 num_to_match = sorted(set(grades.values()))[1]

N = int(raw_input())

students = []
for i in range(2*N):
    students.append(raw_input().split())
grades = 
for j in range(0, len(students), 2):
    grades[students[j][0]] = float(students[j + 1][0])
result = []
num_to_match = sorted(set(grades.values()))[1]
for pupil in grades.keys():
    if grades[pupil] == num_to_match:
        result.append(pupil)
for k in sorted(result):
    print k

【问题讨论】:

你有什么不明白的地方? “请向我解释 [工作,由其他人编写的] 代码”问题对 Stack Overflow 不利,因为我们无法读懂您的想法以知道要解释什么。 【参考方案1】:

你可以如下换行来充分理解:num_to_match = sorted(set(grades.values()))[1]

grades.values() - 从字典 grades 返回值列表

set(grades.values()) - 通过将列表转换为集合来删除重复条目

sorted(set(grades.values())) - 返回上面得到的排序集

sorted(set(grades.values()))[1] - 从排序的项目列表中选择第二个元素。

【讨论】:

以上是关于谁能解释一下Java中GridBagLayout的使用?的主要内容,如果未能解决你的问题,请参考以下文章

java中int.Class和String.Class指啥,麻烦谁能详细解释一下。

谁能帮我解释一下java线程中的wait()方法的作用与执行原理非常感谢!

谁能解释一下Applets和AWT?

java中Swing的GridBagLayout使用简介

在Java里面谁能解释一下 pass by reference 还有pass by value?

在 java 中使用 GridBagLayout 时设置最大组件大小