当前位置: 技术问答>java相关
学习Swing
来源: 互联网 发布时间:2015-08-01
本文导语: 我初学Swing,今天学习了一个小程序,自己加了注释,希望能和其他初学者一起探讨。另外,有两个问题希望高手解答一下(见末尾),谢谢啦。 /* 原始代码以及程序的详细描述可以察看 http://java.sun.com/docs/books/tutori...
我初学Swing,今天学习了一个小程序,自己加了注释,希望能和其他初学者一起探讨。另外,有两个问题希望高手解答一下(见末尾),谢谢啦。
/*
原始代码以及程序的详细描述可以察看
http://java.sun.com/docs/books/tutorial/uiswing/mini/secondexample.html
*/
import javax.swing.*; //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
//Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
public class SwingApplication {
private static String labelPrefix = "Number of button clicks: ";
private int numClicks = 0;
public Component createComponents() {
//为什么是final?不清楚
final JLabel label = new JLabel(labelPrefix + "0 ");
JLabel label2 = new JLabel("");
JButton button = new JButton("I'm a Swing button!");
/*setMnemonic
public void setMnemonic(int mnemonic)Sets the keyboard mnemonic on the current model.
Parameters:
mnemonic - the key code which represents the mnemonic
public class KeyEvent
extends InputEvent
An event which indicates that a keystroke occurred in a component.
这条语句用来设置快捷键,程序运行时,按Alt+I可以按下button
*/
button.setMnemonic(KeyEvent.VK_I);
/*这是只是一行代码:
public void addActionListener(ActionListener l)
在这里,添加一个ActionListener接口的同时,填充了该接口唯一的虚函数actionPerformed()
*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numClicks++;
label.setText(labelPrefix + numClicks);
}
});
/*
public void setLabelFor(Component c)
If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
See Also:
getDisplayedMnemonic(), setDisplayedMnemonic(int)
发现这条语句对当前程序没有什么作用。
*/
label.setLabelFor(button);
/*
* An easy way to put space between a top-level container
* and its contents is to put the contents in a JPanel
* that has an "empty" border.
*/
JPanel pane = new JPanel();
/*public void setBorder(Border border)
Sets the border of this component. The Border object is responsible for defining the insets for the component (overriding any insets set directly on the component) and for optionally rendering any border decorations within the bounds of those insets. Borders should be used (rather than insets) for creating both decorative and non-decorative (such as margins and padding) regions for a swing component. Compound borders can be used to nest multiple borders within a single component.
This is a bound property.
public class BorderFactory
extends Object
Factory class for vending standard Border objects. Wherever possible, this factory will hand out references to shared Border instances.
createEmptyBorder()
Creates an empty border that takes up no space.
*/
pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
10, //bottom
30) //right
);
/*public void setLayout(LayoutManager mgr)
LayoutManager是一个接口,GridLayout是类:
public class GridLayout
extends Object
implements LayoutManager, Serializable
因为实现了LayoutManager,也就具有了LayoutManager的属性。(?)
GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows and columns.
*/
pane.setLayout(new GridLayout(0, 1));
pane.add(button);
pane.add(label);
/*自己加的1条语句,用来显示CrossPlatformLookAndFeelClassName
1是index,后面的代码用这个index获得该Label
*/
pane.add(label2, 1);
return pane;
}
public static void main(String[] args) {
/*
具体用途还不知道,在本程序中屏蔽掉并不影响程序运行和效果。
另外,改为
UIManager.setLookAndFeel("javax.swing.plaf.basic.BasicLookAndFeel");
效果也一样。
改为"javax.swing.plaf.multi.MultiLookAndFeel"运行时出错。
下面是一个有趣的例子
http://java.sun.com/docs/books/tutorial/uiswing/mini/example-1dot3/SimpleExample.java
*/
String sLookFeel = "";
try {
sLookFeel = UIManager.getCrossPlatformLookAndFeelClassName();
//System.out.println(sLookFeel);
UIManager.setLookAndFeel(sLookFeel);
//UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("SwingApplication");
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
/*The "contentPane" is the primary container for application specific components. Applications should add children to the contentPane, set its layout manager, and so on.
The contentPane my not be null.
see also:getRootPane(),getLayoutPane(), getGlassPane()。
*/
frame.getContentPane().add(contents, BorderLayout.CENTER);
//这3行代码是自己加的,用来显示CrossPlatformLookAndFeelClassName
JPanel panel = (JPanel)contents;
JLabel label = (JLabel)panel.getComponent(1);
label.setText( sLookFeel);
//Finish setting up the frame, and show it.
//接口WindowAdapter有多个接口函数。
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
//后面不能有关于GUI的代码
}
}
1)final JLabel label = new JLabel(labelPrefix + "0 ");
为什么是final?
2)JLabel label = (JLabel)panel.getComponent(1);
有没有别的方法能获得Panel中的部件?
/*
原始代码以及程序的详细描述可以察看
http://java.sun.com/docs/books/tutorial/uiswing/mini/secondexample.html
*/
import javax.swing.*; //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
//Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
public class SwingApplication {
private static String labelPrefix = "Number of button clicks: ";
private int numClicks = 0;
public Component createComponents() {
//为什么是final?不清楚
final JLabel label = new JLabel(labelPrefix + "0 ");
JLabel label2 = new JLabel("");
JButton button = new JButton("I'm a Swing button!");
/*setMnemonic
public void setMnemonic(int mnemonic)Sets the keyboard mnemonic on the current model.
Parameters:
mnemonic - the key code which represents the mnemonic
public class KeyEvent
extends InputEvent
An event which indicates that a keystroke occurred in a component.
这条语句用来设置快捷键,程序运行时,按Alt+I可以按下button
*/
button.setMnemonic(KeyEvent.VK_I);
/*这是只是一行代码:
public void addActionListener(ActionListener l)
在这里,添加一个ActionListener接口的同时,填充了该接口唯一的虚函数actionPerformed()
*/
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numClicks++;
label.setText(labelPrefix + numClicks);
}
});
/*
public void setLabelFor(Component c)
If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
See Also:
getDisplayedMnemonic(), setDisplayedMnemonic(int)
发现这条语句对当前程序没有什么作用。
*/
label.setLabelFor(button);
/*
* An easy way to put space between a top-level container
* and its contents is to put the contents in a JPanel
* that has an "empty" border.
*/
JPanel pane = new JPanel();
/*public void setBorder(Border border)
Sets the border of this component. The Border object is responsible for defining the insets for the component (overriding any insets set directly on the component) and for optionally rendering any border decorations within the bounds of those insets. Borders should be used (rather than insets) for creating both decorative and non-decorative (such as margins and padding) regions for a swing component. Compound borders can be used to nest multiple borders within a single component.
This is a bound property.
public class BorderFactory
extends Object
Factory class for vending standard Border objects. Wherever possible, this factory will hand out references to shared Border instances.
createEmptyBorder()
Creates an empty border that takes up no space.
*/
pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
10, //bottom
30) //right
);
/*public void setLayout(LayoutManager mgr)
LayoutManager是一个接口,GridLayout是类:
public class GridLayout
extends Object
implements LayoutManager, Serializable
因为实现了LayoutManager,也就具有了LayoutManager的属性。(?)
GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows and columns.
*/
pane.setLayout(new GridLayout(0, 1));
pane.add(button);
pane.add(label);
/*自己加的1条语句,用来显示CrossPlatformLookAndFeelClassName
1是index,后面的代码用这个index获得该Label
*/
pane.add(label2, 1);
return pane;
}
public static void main(String[] args) {
/*
具体用途还不知道,在本程序中屏蔽掉并不影响程序运行和效果。
另外,改为
UIManager.setLookAndFeel("javax.swing.plaf.basic.BasicLookAndFeel");
效果也一样。
改为"javax.swing.plaf.multi.MultiLookAndFeel"运行时出错。
下面是一个有趣的例子
http://java.sun.com/docs/books/tutorial/uiswing/mini/example-1dot3/SimpleExample.java
*/
String sLookFeel = "";
try {
sLookFeel = UIManager.getCrossPlatformLookAndFeelClassName();
//System.out.println(sLookFeel);
UIManager.setLookAndFeel(sLookFeel);
//UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("SwingApplication");
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
/*The "contentPane" is the primary container for application specific components. Applications should add children to the contentPane, set its layout manager, and so on.
The contentPane my not be null.
see also:getRootPane(),getLayoutPane(), getGlassPane()。
*/
frame.getContentPane().add(contents, BorderLayout.CENTER);
//这3行代码是自己加的,用来显示CrossPlatformLookAndFeelClassName
JPanel panel = (JPanel)contents;
JLabel label = (JLabel)panel.getComponent(1);
label.setText( sLookFeel);
//Finish setting up the frame, and show it.
//接口WindowAdapter有多个接口函数。
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
//后面不能有关于GUI的代码
}
}
1)final JLabel label = new JLabel(labelPrefix + "0 ");
为什么是final?
2)JLabel label = (JLabel)panel.getComponent(1);
有没有别的方法能获得Panel中的部件?
|
1) 因为方法中的变量如果在方法定义的内部类中使用的化需要final.这牵扯到编译的问题.
2) 一般情况下都是保留组件的引用.
当使用panel.getCompnent(i)时,我以为会搭配reflect使用.
除此以外,应该还有别的方法,不过具体的API我不清楚.
2) 一般情况下都是保留组件的引用.
当使用panel.getCompnent(i)时,我以为会搭配reflect使用.
除此以外,应该还有别的方法,不过具体的API我不清楚.
|
在构造器外面声明你的各个组件,如
private JLabel label;
然后在构造器内初始化,
label=new JLabel("");
...
在这个类内部,你自己用label.方法 就是了
private JLabel label;
然后在构造器内初始化,
label=new JLabel("");
...
在这个类内部,你自己用label.方法 就是了