在 `JOptionPane.showOptionDialog()` 中更改按钮布局。

huangapple go评论71阅读模式
英文:

Change button layout in JOptionPane.showOptionDialog()

问题

我正在处理一个程序,让用户从命令列表中进行选择。我的程序有多个命令,而JOptionPane.showOptionDialog()会水平显示它们。

在 `JOptionPane.showOptionDialog()` 中更改按钮布局。

正如你所见,窗口比我的屏幕更宽。我希望能够显示两行按钮,而不是一行,这样用户就可以看到所有的选项。

如何实现这个目标呢?

这是我的代码:

public int getCommand (String[] commands) {
    return JOptionPane.showOptionDialog
            (null,
                    "在下面选择一个选项", // 提示信息
                    windowTitle, // 窗口标题
                    JOptionPane.YES_NO_CANCEL_OPTION, // 选项类型
                    JOptionPane.QUESTION_MESSAGE, // 消息类型
                    null, // 图标
                    commands, // 命令列表
                    commands[commands.length - 1]);
}
英文:

I am working on a program that has the user choose from a list of commands. My program has several commands, and JOptionPane.showOptionDialog() displays them horizontally.

在 `JOptionPane.showOptionDialog()` 中更改按钮布局。

As you can see, the window is wider than my screen. I'd like to make it to where there are two rows of buttons instead of one, that way the user can see all of the options.

How exactly would one do this?

Here is my code:

public int getCommand (String[] commands) {
	
	return JOptionPane.showOptionDialog
			(null,
					"Choose an option below", // Prompt message
					windowTitle, // Window title
					JOptionPane.YES_NO_CANCEL_OPTION, // Option type
					JOptionPane.QUESTION_MESSAGE, // Message type
					null, // Icon
					commands, // List of commands
					commands[commands.length - 1]);
}

答案1

得分: 3

选项面板的布局在内部受控制,并且没有直接控制按钮布局的方法。

因此,正确的解决方案是创建一个自定义的模态 JDialog,根据您的要求显示组件。

然而,如果您真的想要使用 JOPtionPane 功能,则需要:

  1. 将 JOptionPane 创建为 Swing 组件,然后更改包含按钮的面板的布局管理器。
  2. 将 JOptionPane 添加到 JDialog,并手动实现标准的选项面板功能。

以下是第一步的示例代码:

import java.awt.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        String[] commands = {"1", "2", "3", "4","5", "6", "7", "8"};

        JOptionPane op = new JOptionPane
        (
            "选择下面的一个选项", // 提示消息
            JOptionPane.QUESTION_MESSAGE, // 消息类型
            JOptionPane.YES_NO_CANCEL_OPTION, // 选项类型
            null, // 图标
            commands, // 命令列表
            commands[commands.length - 1]
        );

        java.util.List<JButton> buttons = SwingUtils.getDescendantsOfType(JButton.class, op, true);
        Container parent = buttons.get(0).getParent();
        parent.setLayout( new GridLayout(2, 0, 5, 5) );

        add(op);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE(), BorderLayout.LINE_START);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

上述代码搜索已添加到选项面板的按钮,然后找到父容器并更改容器的布局管理器为 GridLayout。您还需要 SwingUtils 类。

要实现第二步,您需要阅读 JOptionPane 的 API。其中包含了将代码添加到 JDialog 并实现选项面板功能所需的代码。

英文:

The layout of the option pane is controlled internally, and there are no methods to directly control the layout of the buttons.

So, the proper solution is to just create a custom modal JDialog that displays the components based on your requirements.

However, if you really want to use the JOPtionPane functionality then you would need to:

  1. create the JOptionPane as a Swing component and then change the layout manager of the panel containing the buttons.
  2. add the JOptionPane to a JDialog and implement the standard option pane functionality manually.

The first step is demonstrated below:

import java.awt.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
String[] commands = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;,&quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;};
JOptionPane op = new JOptionPane
(
&quot;Choose an option below&quot;, // Prompt message
JOptionPane.QUESTION_MESSAGE, // Message type
JOptionPane.YES_NO_CANCEL_OPTION, // Option type
null, // Icon
commands, // List of commands
commands[commands.length - 1]
);
java.util.List&lt;JButton&gt; buttons = SwingUtils.getDescendantsOfType(JButton.class, op, true);
Container parent = buttons.get(0).getParent();
parent.setLayout( new GridLayout(2, 0, 5, 5) );
add(op);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame(&quot;SSCCE&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE(), BorderLayout.LINE_START);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater( () -&gt; createAndShowGUI() );
}
}

The above code searches for the buttons that have been added to the option pane and then finds the parent container and changes the layout manager of the container to be a GridLayout. You will also need the SwingUtils class.

To implement the second step you will need to Read the API for the JOptionPane. It contains the code necessary add the code to a JDialog and implement the option pane functionality.

huangapple
  • 本文由 发表于 2020年9月21日 08:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定