如何限制 JButton 在重新验证时更改其尺寸

发布于 2024-12-27 10:25:16 字数 3687 浏览 1 评论 0原文

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

根据我的要求:-

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

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

  1. JButton 的尺寸在重新验证时发生了变化。
  2. JButton 的初始尺寸非常大(如何限制 JButton 的尺寸)

作为参考,请查看此 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;
    }
}

谢谢, 里茨:)

I have a panel (with GridLayout(0,1)) embedded on JFrame.

As per my requirement :-

  • I am adding (JButton,JTextArea) in pairs on above declared panel.
  • Now, on click of JButton of any pair, it's JTextArea should be removed, and on reclicking JButton, it's JTextArea should be added again.

Everything is working fine, except below two listed problems :

  1. Dimensions of JButton are changed on revalidation.
  2. Initial dimensions of JButton are very large(How to restrict dimension of JButton)

For reference, have a look at this screenshot.

And Please find below modified Code listings :-

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;
    }
}

Thanks,
Rits :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小红帽 2025-01-03 10:25:16
  1. 返回嵌套的 JPanel (BorderLayout)
    • JPanel(默认情况下有 FlowLayout)对于 JButton,此 JPanel 放入 BorderLayout .北
    • JTextArea 放入 BorderLayout.CENTER
  2. 那里我看不到 revalidate & 的原因。重新绘制,仅在您在JComponents之间切换或删除然后添加新的JComponent的情况下,
  3. 对于隐藏/可见 > 特定的 JPanelJComponent 使用方法 setVisible()
  1. Return JPanel (BorderLayout) nested
    • JPanel (has by default FlowLayout) for JButton, this JPanel put to the BorderLayout.NORTH
    • JTextArea put to then BorderLayout.CENTER
  2. There I can't see reason for revalidate & repaint, only in the case that you switch between JComponents or remove and then add new JComponent(s),
  3. For hide/visible particular JPanel or JComponent use method setVisible()
甜嗑 2025-01-03 10:25:16

使用例如垂直 BoxLayout 而不是 GridLayout

Use e.g. vertical BoxLayout instead of GridLayout

倾城°AllureLove 2025-01-03 10:25:16

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

You should use another layout. GridLayout draws the components to maximum size available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文