?? swingdemo.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class SwingDemo extends SimpleFrame
{
//Creates a new instance of <code>SwingDemo</code>.
public SwingDemo(int width,int height)
{
super(width,height);
setTitle("演示GUI容器,組件,事件及布局");
topPanel.add(nameLabel);
topPanel.add(nameText);
topPanel.add(new JLabel("學歷"));
topPanel.add(comboBox);
buttonGroup.add(rb1);
buttonGroup.add(rb2);
buttonGroup.add(rb3);
list.setBorder(BorderFactory.createTitledBorder("文化程度"));
textArea.setBorder(BorderFactory.createTitledBorder("演示效果:"));
textArea.setBackground(new Color(200,220,180));
box.add(new JLabel("性別:"));
box.add(rb1);
box.add(rb2);
box.add(rb3);
box.add(checkBox);
Container c=this.getContentPane();
c.add(topPanel,"North");
c.add(list,"East");
c.add(box,"West");
c.add(button,"South");
c.add(new JScrollPane(textArea),"Center");
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting() ) return;
textArea.append("\n"+(String)(list.getSelectedValue() ) );
}
} );
checkBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String c=" 未選";
if(checkBox.isSelected() )
c=" 已選";
textArea.append("\n"+(String)(checkBox.getText() )+c);
}
} );
comboBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.append("\n"+
(String)(comboBox.getSelectedItem() ) );
}
} );
button.addActionListener ( new ActionListener() {
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,
"演示消息框。",button.getText(),
JOptionPane.INFORMATION_MESSAGE );
}
});
nameText.getDocument().addDocumentListener(
new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
textArea.append("\n"+nameText.getText() );
}
public void removeUpdate(DocumentEvent e) {
textArea.append("\n"+nameText.getText() );
}
public void changedUpdate(DocumentEvent e) {}
} );
rb1.addActionListener(rbListener);
rb2.addActionListener(rbListener);
rb3.addActionListener(rbListener);
}
private ActionListener rbListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.append("\n"+
( (JRadioButton)e.getSource() ).getText() );
}
};
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// TODO code application logic here
try{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
} catch (Exception e){
System.err .println("程序異常:"+e.getMessage() );
}
SwingDemo frame=new SwingDemo(400,300);
frame.setVisible(true);
}
private JPanel topPanel=new JPanel();
private JPanel listPanel=new JPanel();
private Box box=Box.createVerticalBox();
private JButton button=new JButton("OK");
private JCheckBox checkBox=new JCheckBox("少數民族");
private JRadioButton
rb1=new JRadioButton("男"),
rb2=new JRadioButton("女"),
rb3=new JRadioButton("不詳",true);
private ButtonGroup buttonGroup=new ButtonGroup();
private JComboBox comboBox=new JComboBox(degrees);
private JList list=new JList(educationalBackground);
private JTextArea textArea=new JTextArea();
private JLabel nameLabel=new JLabel("姓名:");
private JTextField nameText=new JTextField("張三");
private static String[] degrees=
{"無","學士","碩士","工程碩士","MBA","博士"};
private static String[] educationalBackground=
{"小學","初中","高中","大學大專","大學本科","碩士研究生","博士研究生"};
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -