如何限制JButton因重新验证而改变它的尺寸

时间:2012-01-18 12:30:24

标签: java swing resize runtime jbutton

我在JFrame上嵌入了一个面板(GridLayout( 0,1 ))。

根据我的要求: -

  • 我在上面声明的面板上成对添加(JButton,JTextArea)。
  • 现在,点击任何一对的JButton,应该删除它的JTextArea,并且在重新点击JButton时,应该再次添加它的JTextArea。

除了下面列出的两个问题之外,一切都运转良好:

  1. 重新验证后,JButton的尺寸会发生变化。
  2. JButton的初始尺寸非常大(如何限制JButton的尺寸)
  3. 供参考,请查看此screenshot

    请查看以下修改后的代码清单: -

    TestFrame.java

    package com.test;
    
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class TestFrame extends JFrame implements ActionListener{
    
        final JPanel panel ;
    
        private TestFrame() {
            setExtendedState(JFrame.MAXIMIZED_BOTH) ;
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
            panel = new JPanel() ;
            panel.setLayout( new GridLayout(0, 1) ) ;
    
            for(int i = 1 ; i <= 3 ; ++i){
                TButton btn = new TButton("User " + i + " [Toggle Button for TextArea " + i + "] (Click)")  ;
                panel.add(btn) ;        btn.addActionListener(this) ;   panel.add(new JScrollPane(btn.getLoggingArea())) ;
            }
    
            add(panel, BorderLayout.CENTER) ;
            setVisible(true) ;
            setExtendedState(JFrame.MAXIMIZED_BOTH) ;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            TButton btn = (TButton) e.getSource() ;
            JPanel parent = (JPanel) btn.getParent() ;
    
            int index = getIndex(parent, btn) ;
    
            if(btn.isLoggingAreaVisible()){
                parent.remove( parent.getComponent(index + 1) ) ;
                btn.setLoggingAreaVisible(false) ;
            }else{
                parent.add(new JScrollPane(btn.getLoggingArea()), index + 1) ;
                btn.setLoggingAreaVisible(true) ;
            }
    
            parent.revalidate() ;
            parent.repaint() ;
        }
    
        private int getIndex(JComponent parent, Component btn) {
    
            Component []comps = parent.getComponents() ;
    
            for(int i = 0 ; i < comps.length ; ++i){
                if( comps[i].equals(btn) ){
                    return i ;
                }
            }
    
            return -1 ;
        }
    
        public static void main(String[] args) {
            new TestFrame() ;
        }
    }
    

    TButton.java

    package com.test;
    
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    
    public class TButton extends JButton{
    
        private JTextArea loggingArea ;
        private boolean loggingAreaVisible = true ;
    
        public TButton(String threadName) {
            super(threadName) ;
            initComponents(threadName) ;
        }
    
        public void initComponents(String threadName) {
    
            String str = "1. By default large buttons are displayed." + "\n" +
                         "    But, I want buttons with a little small dimensions." + "\n\n" +
                         "2. On click of above button, above button expands to cover space used by this textArea." + "\n" +
                         "    But, what I want, that button size does not changes onClick, only textArea length increases/decreases." ;
            loggingArea = new JTextArea(getText() + " textArea." + "\n\n" + str + "\n\n" + "Type Here...") ;
        }
    
    
        public boolean isLoggingAreaVisible() {
            return loggingAreaVisible;
        }
    
        public void setLoggingAreaVisible(boolean loggingAreaVisible) {
            this.loggingAreaVisible = loggingAreaVisible;
        }
    
        public JTextArea getLoggingArea() {
            return loggingArea;
        }
    }
    

    谢谢, Rits :))

3 个答案:

答案 0 :(得分:3)

  1. 返回JPanelBorderLayout)嵌套
      {li> JPanel(默认情况下为FlowLayoutJButton,此JPanel投放到BorderLayout.NORTH
    • JTextArea放到BorderLayout.CENTER
  2. 我无法看到revalidate & repaint的原因,只有在JComponents之间切换或删除然后添加新的JComponent(s)
  3. hide/visible特定JPanelJComponent使用方法setVisible()

答案 1 :(得分:2)

使用例如垂直BoxLayout而不是GridLayout

答案 2 :(得分:1)

您应该使用其他布局。 GridLayout将组件绘制为可用的最大大小。