【问题标题】:How to restrict JButton to change it's dimensions as a result of revalidation如何限制 JButton 由于重新验证而更改其尺寸
【发布时间】:2012-01-18 12:30:23
【问题描述】:

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

根据我的要求:-

  • 我在上面声明的面板上成对添加 (JButton,JTextArea)。
  • 现在,单击任意一对 JButton 时,应将其 JTextArea 删除,再次单击 JButton 时,应再次添加其 JTextArea。

一切正常,除了下面列出的两个问题:

  1. JButton 的尺寸在重新验证时发生更改。
  2. 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() ;
    }
}

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 :)

【问题讨论】:

  • +1 我曾经用你的头像作为我的桌面背景。 :D

标签: java swing resize runtime jbutton


【解决方案1】:
  1. 返回JPanel (BorderLayout) 嵌套
    • JPanel(默认有FlowLayout)对于JButton,这个JPanel放到BorderLayout.NORTH
    • JTextArea 然后BorderLayout.CENTER
  2. 那里我看不到revalidate &amp; repaint 的原因,只有在您在JComponents 之间切换或删除然后添加新的JComponent(s) 的情况下,
  3. 对于hide/visible特定JPanelJComponent使用方法setVisible()

【讨论】:

  • 感谢您的意见。我尝试了这种方法,但是 setVisible(false) 留下了一个空白空间,这不是我的要求。根据要求,此空白区域应由其他 textAreas 使用。
  • +1 还要考虑 NORTH 约束中的 JButtonJToolBar(而不是 JPanelFlowLayout)。
  • @Andrew : jtoolbar.add(btn, JToolBar.NORTH) 抛出非法组件位置。 jtoolbar.add(btn) 工作正常,但在左右位置显示 [JButton,JTextArea]。
【解决方案2】:

使用例如垂直 BoxLayout 而不是 GridLayout

【讨论】:

  • 在网上搜索了“java 垂直 BoxLayout 示例”,然后瞧……,它就像魅力一样。接受你的回答... :)
【解决方案3】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-03-15
  • 2021-04-24
  • 1970-01-01
  • 2010-11-29
  • 2014-08-27
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
相关资源
最近更新 更多