根据框架大小更改按钮的文本可见性 [英] Change text visibility of buttons according to the frame size

查看:25
本文介绍了根据框架大小更改按钮的文本可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的按钮有文字和图标.如果用户调整框架大小并使按钮不可见,我想隐藏文本.我找到了滚动窗格的解决方法,但我想知道是否有更好的解决方案.提前致谢.

我的解决方案:

public class IconButtonTest extends JFrame {私人 JScrollPane buttonScrollPane;私人 JButton button1;私人 JButton button2;私人 JButton button3;公共图标按钮测试(){JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));ImageIcon icon = new ImageIcon();button1 = new JButton("Button1", icon);button2 = new JButton("Button2", icon);button3 = new JButton("Button2", icon);buttonPanel.add(button1);buttonPanel.add(button2);buttonPanel.add(button3);buttonScrollPane = new JScrollPane(buttonPanel);buttonScrollPane.setBorder(null);添加(buttonScrollPane,BorderLayout.NORTH);addComponentListener(new ResizeListener());setSize(300, 300);设置可见(真);}公共静态无效主(字符串 [] args){新的 IconButtonTest();}私有类 ResizeListener 扩展了 ComponentAdapter {@覆盖public void componentResized(ComponentEvent e) {button1.setText("Button1");button2.setText("Button2");button3.setText("Button2");SwingUtilities.invokeLater(new Runnable() {公共无效运行(){如果 (buttonScrollPane.getHorizo​​ntalScrollBar().isVisible()) {button1.setText("");button2.setText("");button3.setText("");}}});}}}

我向 JPanel 添加了按钮,然后将该面板添加到带有滚动窗格的框架中.在 ResizeListener 我正在检查水平滚动是否可见.如果可见,则表示 buttonPanel 溢出了可见区域.

解决方案

我找到了滚动窗格的解决方法,但我想知道是否有更好的解决方案.

不知道它是否更好,但不需要滚动窗格.只需将侦听器添加到面板,然后您就可以根据实际宽度检查首选宽度:

button1.setText("Button1");button2.setText("Button2");button3.setText("Button2");JPanel 面板 = (JPanel)e.getComponent();如果 (panel.getSize().width < panel.getPreferredSize().width){button1.setText("");button2.setText("");button3.setText("");}

<块引用>

如果用户调整框架大小并使按钮不可见,我想隐藏文本.

如果您只是想确保所有按钮都可以访问,那么您可以:

  1. 使用环绕布局.按钮将换行并增加面板的高度.

  2. 使用 ScrollContainer 的概念.按钮将根据需要添加到容器的开始/结束位置,以允许您滚动浏览当前不可见的按钮.请参阅:在 JPanel 中左右移动

I have both texts and icons for my buttons. I want to hide texts, if user resize the frame and make one the buttons invisible. I found a workaround with scroll pane, but I want to know if there is a better solution. Thanks in advance.

My solution:

public class IconButtonTest extends JFrame {

    private JScrollPane buttonScrollPane;

    private JButton button1;
    private JButton button2;
    private JButton button3;

    public IconButtonTest() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        ImageIcon icon = new ImageIcon();

        button1 = new JButton("Button1", icon);
        button2 = new JButton("Button2", icon);
        button3 = new JButton("Button2", icon);

        buttonPanel.add(button1);
        buttonPanel.add(button2);
        buttonPanel.add(button3);

        buttonScrollPane = new JScrollPane(buttonPanel);
        buttonScrollPane.setBorder(null);
        add(buttonScrollPane, BorderLayout.NORTH);

        addComponentListener(new ResizeListener());

        setSize(300, 300);
        setVisible(true);
    }

    public static void main(String[] args) {
        new IconButtonTest();
    }

    private class ResizeListener extends ComponentAdapter {
        @Override
        public void componentResized(ComponentEvent e) {
            button1.setText("Button1");
            button2.setText("Button2");
            button3.setText("Button2");

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (buttonScrollPane.getHorizontalScrollBar().isVisible()) {
                        button1.setText("");
                        button2.setText("");
                        button3.setText("");
                    }
                }
            });

        }
    }
}

I added buttons to JPanel and add that panel to the frame with a scroll pane. In ResizeListener I am checking if horizontal scroll visible or not. If it is visible it means buttonPanel is overflowed the visible area.

解决方案

I found a workaround with scroll pane, but I want to know if there is a better solution.

Don't know if it is better but there is no need for the scroll pane. Just add the listener to the panel, then you can check the preferred width against the actual width:

button1.setText("Button1");
button2.setText("Button2");
button3.setText("Button2");

JPanel panel = (JPanel)e.getComponent();

if (panel.getSize().width < panel.getPreferredSize().width)
{
    button1.setText("");
    button2.setText("");
    button3.setText("");
}

I want to hide texts, if user resize the frame and make one the buttons invisible.

If you are just trying to make sure all the buttons are accessible then you might be able to:

  1. Use the Wrap Layout. The button will wrap to a new line and the height of the panel will be increased.

  2. Use the concept of the ScrollContainer. Buttons will be added to the start/end of the container as required to allow you to scroll through buttons that are not currently visible. See: Moving left right in a JPanel

这篇关于根据框架大小更改按钮的文本可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆