| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1355 人关注过本帖
标题:[求助]BoxLayout布局问题
只看楼主 加入收藏
QsLinuxS
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2017-9-28
收藏
 问题点数:0 回复次数:0 
[求助]BoxLayout布局问题
程序代码:
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();
    }
}


我想问下
        verticalBoxLeft = Box.createVerticalBox();
        verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20)));

这里怎么解释呢?
运行图片(注释与不注释行: verticalBoxLeft.add(Box.createRigidArea( new Dimension(70, 20))); )

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: import swing Box new Add 
2017-10-25 22:36
快速回复:[求助]BoxLayout布局问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035990 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved