
package ContactsDatabase;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ContactsDatabase extends JFrame
implements ActionListener{
private JPanel informationPanel;
private JPanel listPanel;
private JList contactList;
private final JTextField JTextFieldName = new JTextField(20);
private final JTextField JTextFieldMobile = new JTextField(20);
private JButton jButtonAdd = new JButton("Add");
private JButton JButtonClear = new JButton("Clear");
public ContactsDatabase() {
initGUI();
}
private void initGUI() {
Box verticalBoxRight, verticalBoxLeft;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
verticalBoxLeft = Box.createVerticalBox();
verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20)));
verticalBoxLeft.add(new JLabel("Name"));
verticalBoxLeft.add(JTextFieldName);
verticalBoxLeft.add(Box.createVerticalStrut(10));
verticalBoxLeft.add(new JLabel("Mobel #"));
verticalBoxLeft.add(JTextFieldMobile);
verticalBoxLeft.add(Box.createVerticalStrut(25));
verticalBoxLeft.add(jButtonAdd);
jButtonAdd.addActionListener(this);
informationPanel = new JPanel();
informationPanel.add(verticalBoxLeft);
informationPanel.setBorder(
BorderFactory.createTitledBorder("Information") );
contactList = new JList();
contactList.setModel(new DefaultListModel());
verticalBoxRight = Box.createVerticalBox();
verticalBoxRight.add(new JScrollPane(contactList));
verticalBoxRight.add(Box.createRigidArea(new Dimension(80, 10)));
verticalBoxRight.add(JButtonClear);
JButtonClear.addActionListener(this);
listPanel = new JPanel();
listPanel.setBorder(
BorderFactory.createTitledBorder("Contacts"));
listPanel.add(verticalBoxRight);
contentPane.add(informationPanel);
contentPane.add(listPanel);
setSize(600, 250);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jButtonAdd){
DefaultListModel contactsModel =
(DefaultListModel)contactList.getModel();
contactsModel.addElement(JTextFieldName.getText()
+ " " + JTextFieldMobile.getText());
JTextFieldName.setText("");
JTextFieldMobile.setText("");
} else{
contactList.setModel(new DefaultListModel());
}
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(
"javax.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(ClassNotFoundException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(InstantiationException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(IllegalAccessException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(UnsupportedLookAndFeelException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}
ContactsDatabase mDIFrame = new ContactsDatabase();
}
}
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ContactsDatabase extends JFrame
implements ActionListener{
private JPanel informationPanel;
private JPanel listPanel;
private JList contactList;
private final JTextField JTextFieldName = new JTextField(20);
private final JTextField JTextFieldMobile = new JTextField(20);
private JButton jButtonAdd = new JButton("Add");
private JButton JButtonClear = new JButton("Clear");
public ContactsDatabase() {
initGUI();
}
private void initGUI() {
Box verticalBoxRight, verticalBoxLeft;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
verticalBoxLeft = Box.createVerticalBox();
verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20)));
verticalBoxLeft.add(new JLabel("Name"));
verticalBoxLeft.add(JTextFieldName);
verticalBoxLeft.add(Box.createVerticalStrut(10));
verticalBoxLeft.add(new JLabel("Mobel #"));
verticalBoxLeft.add(JTextFieldMobile);
verticalBoxLeft.add(Box.createVerticalStrut(25));
verticalBoxLeft.add(jButtonAdd);
jButtonAdd.addActionListener(this);
informationPanel = new JPanel();
informationPanel.add(verticalBoxLeft);
informationPanel.setBorder(
BorderFactory.createTitledBorder("Information") );
contactList = new JList();
contactList.setModel(new DefaultListModel());
verticalBoxRight = Box.createVerticalBox();
verticalBoxRight.add(new JScrollPane(contactList));
verticalBoxRight.add(Box.createRigidArea(new Dimension(80, 10)));
verticalBoxRight.add(JButtonClear);
JButtonClear.addActionListener(this);
listPanel = new JPanel();
listPanel.setBorder(
BorderFactory.createTitledBorder("Contacts"));
listPanel.add(verticalBoxRight);
contentPane.add(informationPanel);
contentPane.add(listPanel);
setSize(600, 250);
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jButtonAdd){
DefaultListModel contactsModel =
(DefaultListModel)contactList.getModel();
contactsModel.addElement(JTextFieldName.getText()
+ " " + JTextFieldMobile.getText());
JTextFieldName.setText("");
JTextFieldMobile.setText("");
} else{
contactList.setModel(new DefaultListModel());
}
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(
"javax.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(ClassNotFoundException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(InstantiationException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(IllegalAccessException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}catch(UnsupportedLookAndFeelException ex){
Logger.getLogger(
ContactsDatabase.class.getName()).log(Level.SEVERE,
null, ex);
}
ContactsDatabase mDIFrame = new ContactsDatabase();
}
}
我想问下
verticalBoxLeft = Box.createVerticalBox();
verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20)));
verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20)));
这里怎么解释呢?
运行图片(注释与不注释行: verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20))); )
只有本站会员才能查看附件,请 登录