Java 如何限制JButton来更改它';s重新验证后的尺寸

Java 如何限制JButton来更改它';s重新验证后的尺寸,java,swing,resize,runtime,jbutton,Java,Swing,Resize,Runtime,Jbutton,我在JFrame上嵌入了一个面板(带有GridLayout(0,1) 根据我的要求:- 我在上面声明的面板上成对添加(JButton,JTextArea) 现在,点击任意一对的JButton,它的JTextArea应该被删除,而在重新点击JButton时,它的JTextArea应该被再次添加 除了下面列出的两个问题外,一切正常: JButton的尺寸在重新验证时更改 JButton的初始尺寸非常大(如何限制JButton的尺寸) 作为参考,看看这个 请查看以下修改后的代码列表:- TestF

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

根据我的要求:-

  • 我在上面声明的面板上成对添加(JButton,JTextArea)
  • 现在,点击任意一对的JButton,它的JTextArea应该被删除,而在重新点击JButton时,它的JTextArea应该被再次添加
除了下面列出的两个问题外,一切正常:

  • JButton的尺寸在重新验证时更改
  • JButton的初始尺寸非常大(如何限制JButton的尺寸)
  • 作为参考,看看这个

    请查看以下修改后的代码列表:-

    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() ;
        }
    }
    
    谢谢,
    Rits:)

    您应该使用其他布局。将组件绘制到可用的最大尺寸。

    例如,使用垂直BoxLayout而不是GridLayout

  • 返回嵌套的
    JPanel
    BorderLayout
    • JPanel
      (默认情况下具有
      FlowLayout
      )对于
      JButton
      ,此
      JPanel
      放在
      BorderLayout.NORTH
    • JTextArea
      put-then
      BorderLayout.CENTER
  • 在这里,我看不出重新验证和重新绘制的原因,只有在您在
    JComponents
    之间切换或删除然后添加新的
    JComponent
    的情况下
  • 对于
    hide/visible
    特定
    JPanel
    JComponent
    使用方法
    setVisible()

  • @rits从这里开始:+1我曾经用你的头像作为我的桌面背景DThnks用于您的输入。我尝试了这种方法,但是setVisible(false)留下了一个空白,这不是我的要求。根据要求,这个空的空间应该被其他的文本空间使用。+ 1还考虑一个<代码> JTooBar < /C>(而不是<代码> jPosie<代码> > <代码> FlowLayout < /代码>中的<代码> JButton < /Cort>约束。@安得烈:JTooBar。Addit(BTN,JToSorBar。North)抛出非法组件位置。jtoolbar.add(btn)工作正常,但在左右位置显示[JButton,JTextArea]。在网上搜索“java垂直BoxLayout示例”和voilla…,效果很好。接受你的答案……:)
    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;
        }
    }