如何使用 JDialogs 在 JOptionPane 上垂直堆叠按钮? [英] How do you stack buttons vertically on a JOptionPane with JDialogs?

查看:25
本文介绍了如何使用 JDialogs 在 JOptionPane 上垂直堆叠按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 createDialog 将三个按钮垂直堆叠到 JOptionPane 上,但它不太适合使用 GridLayout.另外,我也不知道如何去掉确定"按钮.你可能想知道为什么我要这样做,但这是我被告知这样做的方式.我想我可以使用 JFrame,但我不认为这与 JOptionPane 搭配得很好,因为那是我想要堆叠按钮的地方.

I'm trying to stack three buttons vertically onto a JOptionPane using createDialog, but it's not quite working with a GridLayout. Also, I'm not sure how to get rid of the 'OK' button as well. You're probably wondering why I am doing it this way, but this is the way I was told to do it. I think I can use a JFrame, but I don't think that goes well with a JOptionPane because that's where I want the buttons stacked.

应该是这样的:
|需要帮助 |
|帮帮我|
|计数|

It should be like this:
| Need Help |
| Help Me |
| Counting |

我需要可访问性来在某个时候添加动作侦听器,但这似乎在我达到那个点之前就变得复杂了.

I need accessibility to add action listeners at some point, but this seems to be getting to convoluted before I can even get to that point.

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.*;
public class ThreeButtons {

    static JDialog dialog;
    public static void main(String[] args) {

        JOptionPane optionPane = new JOptionPane();
        optionPane.setMessage("Set Message");
        optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        optionPane.setLayout(new GridLayout(3,1));
        String[] buttonTxt = {"Need Help","Help Me","Counting"};
        JButton[] buttons = new JButton[buttonTxt.length];
        for (int i = 0; i < buttonTxt.length; i++)
        {
            buttons[i] = new JButton(buttonTxt[i]); 
            optionPane.add(buttons[i]);
        }
        dialog = optionPane.createDialog(null, "Icon/Text Button");
        dialog.setVisible(true);

    }

}

推荐答案

如果你想堆叠按钮,你需要将它们添加到面板并将面板添加到选项面板,如下所示:

If you want to stack the buttons you need to add them to a panel and add the panel to the option pane like this:

    JDialog dialog = null;
    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("Set Message");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3,1));
    String[] buttonTxt = {"Need Help","Help Me","Counting"};
    JButton[] buttons = new JButton[buttonTxt.length];
    for (int i = 0; i < buttonTxt.length; i++)
    {
        buttons[i] = new JButton(buttonTxt[i]);
        panel.add(buttons[i]);
    }
    optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
    optionPane.add(panel);
    dialog = optionPane.createDialog(null, "Icon/Text Button");
    dialog.setVisible(true);

除了手动浏览 JOptionPane 的内容并将其删除之外,我不确定您如何摆脱 OK 按钮.您始终可以创建自己的 JDialog,然后您就拥有完全的控制权,但是要获得漂亮的 joption 窗格图标需要做更多的工作:)

I'm not sure how you could get rid of the OK button though apart from manually going through the contents of the JOptionPane and removing it. You could always create your own JDialog then you have full control, but there will be slightly more work getting the nice joption pane icons :)

这篇关于如何使用 JDialogs 在 JOptionPane 上垂直堆叠按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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