📄 validationdemo2.java
字号:
// ValidationDemo2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ValidationDemo2 extends JFrame implements ActionListener
{
final static int NOTHING = 0;
final static int SIZE_ONLY = 1;
final static int SIZE_AND_VALIDATE = 2;
JLabel l;
static int option = NOTHING;
ValidationDemo2 (String title)
{
super (title);
addWindowListener (new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
});
JPanel p = new JPanel ();
JButton b = new JButton ("Change");
b.addActionListener (this);
p.add (b);
l = new JLabel ("Hello");
p.add (l);
b = new JButton ("Restore");
b.addActionListener (this);
p.add (b);
getContentPane ().add (p);
pack ();
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
System.out.println ("Label validity: " + l.isValid ());
System.out.println ("Label's container validity: " + isValid ());
System.out.println ("Label's preferred size: " +
l.getPreferredSize ());
System.out.println ("Label's minimum size: " +
l.getMinimumSize ());
System.out.println ("Label's maximum size: " +
l.getMaximumSize ());
if (e.getActionCommand ().equals ("Change"))
l.setText ("Hello World");
else
l.setText ("Hello");
if (option != NOTHING)
{
setSize (getPreferredSize ());
if (option == SIZE_AND_VALIDATE)
validate ();
}
System.out.println ("Label validity: " + l.isValid ());
System.out.println ("Label's container validity: " + isValid ());
System.out.println ("Label's preferred size: " +
l.getPreferredSize ());
System.out.println ("Label's minimum size: " +
l.getMinimumSize ());
System.out.println ("Label's maximum size: " +
l.getMaximumSize ());
}
public static void main (String [] args)
{
if (args.length == 1)
{
if (args [0].toUpperCase ().equals ("S"))
option = SIZE_ONLY;
else
if (args [0].toUpperCase ().equals ("V"))
option = SIZE_AND_VALIDATE;
}
new ValidationDemo2 ("Validation Demo2");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -