課本的實例程序,我照著抄的,還是很多錯誤啊,第一次抄完有三十多個錯誤,然后開始改正,改了一個多小時吧,才弄好,剛剛運行成功呵呵。等會再寫個小程序,是課后題,下午我上自習的時候在本上寫下了那個程序,等會調試一下。呵呵,希望大體是正確的。
/*
示例程序: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{
public SwingDemo (int width,int height){
//設置頂層容器
super(width,height);
setTitle("演示GUI容器,組件,事件及布局");
//JPanel容器采用默認的FlowLayout布局
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采用默認的布局管理器BoxLayout布局
box.add(new JLabel("性別:"));
box.add(rb1);
box.add(rb2);
box.add(rb3);
box.add(checkBox);
//JFrame容器采用默認的BorderLayout布局
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add(topPanel,"North");
c.add(button,"South");
c.add(box,"West");
c.add(list,"East");
c.add(new JScrollPane(textArea),"Center");
//演示Jlist組件選擇列表項事件
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()) return;
textArea.append("\n"+(String)(list.getSelectedValue()));
}
});
//演示JCheckBox組件單擊事件
checkBox.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String c = " 未選";
if(checkBox.isSelected())
c = " 已選";
textArea.append("\n"+(String)(checkBox.getText())+c);
}
});
//演示JComboBOx組件選擇列表事件
comboBox.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
textArea.append("\n"+(String)(comboBox.getSelectedItem()));
}
});
//演示JButton組件單擊事件
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//演示消息框JOptionPane組件
JOptionPane.showMessageDialog(null,"消息提示框。",button.getText(),JOptionPane.INFORMATION_MESSAGE);
}
});
//演示JTextField組件文本改變事件
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){}
});
//演示JRadioButton組件單擊事件
rb1.addActionListener(rbListener);
rb2.addActionListener(rbListener);
rb3.addActionListener(rbListener);
}
//演示3個JRadioButton組件注冊地一個共用監聽器
private ActionListener rbListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.append("\n"+((JRadioButton)e.getSource()).getText());
}
};
public static void main(String args[]){
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("男");
private JRadioButton rb2 = new JRadioButton("女");
private 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= {"小學","初中","高中","大學大專","大學本科","碩士生研究生","博士研究生"};
}
Tags - java , gui , 課本習題
文章來源:http://www.tt-shopping.com/kevinlau/read.php/119.htm
/*
示例程序: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{
public SwingDemo (int width,int height){
//設置頂層容器
super(width,height);
setTitle("演示GUI容器,組件,事件及布局");
//JPanel容器采用默認的FlowLayout布局
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采用默認的布局管理器BoxLayout布局
box.add(new JLabel("性別:"));
box.add(rb1);
box.add(rb2);
box.add(rb3);
box.add(checkBox);
//JFrame容器采用默認的BorderLayout布局
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
c.add(topPanel,"North");
c.add(button,"South");
c.add(box,"West");
c.add(list,"East");
c.add(new JScrollPane(textArea),"Center");
//演示Jlist組件選擇列表項事件
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()) return;
textArea.append("\n"+(String)(list.getSelectedValue()));
}
});
//演示JCheckBox組件單擊事件
checkBox.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String c = " 未選";
if(checkBox.isSelected())
c = " 已選";
textArea.append("\n"+(String)(checkBox.getText())+c);
}
});
//演示JComboBOx組件選擇列表事件
comboBox.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
textArea.append("\n"+(String)(comboBox.getSelectedItem()));
}
});
//演示JButton組件單擊事件
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//演示消息框JOptionPane組件
JOptionPane.showMessageDialog(null,"消息提示框。",button.getText(),JOptionPane.INFORMATION_MESSAGE);
}
});
//演示JTextField組件文本改變事件
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){}
});
//演示JRadioButton組件單擊事件
rb1.addActionListener(rbListener);
rb2.addActionListener(rbListener);
rb3.addActionListener(rbListener);
}
//演示3個JRadioButton組件注冊地一個共用監聽器
private ActionListener rbListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
textArea.append("\n"+((JRadioButton)e.getSource()).getText());
}
};
public static void main(String args[]){
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("男");
private JRadioButton rb2 = new JRadioButton("女");
private 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= {"小學","初中","高中","大學大專","大學本科","碩士生研究生","博士研究生"};
}
Tags - java , gui , 課本習題
文章來源:http://www.tt-shopping.com/kevinlau/read.php/119.htm