如何在确认的JOptionPane上的同一“行”上放置多个JComponent?

时间:2017-03-02 04:42:18

标签: java swing

这是我的代码:

public class PaymentDialog {

    public static int show(Double value) {
        JLabel total = new JLabel("Total: ");
        JLabel totalValue = new JLabel(value.toString());
        JLabel input = new JLabel("Entrada: ");
        JTextField inputField = new JTextField();
        JLabel change = new JLabel("Troco: ");
        JLabel changeValue = new JLabel("1234");
        JComponent[] components = new JComponent[] {
            total,
            totalValue,
            input,
            inputField,
            change,
            changeValue
        };
        int result = JOptionPane.showConfirmDialog(null, components, "Pagamento", JOptionPane.OK_CANCEL_OPTION);
        return result;
    }

}

以下是它的图像:

enter image description here

如您所见,每个组件占据整行,这是非常残酷的。标签应该在它的相应组件后面,而不是在它上面。

如何自定义此布局行为?如果不可能,我怎么能创建一个允许我这样做的自定义JDialog? (因为我不想创建一个JFrame来正确定制布局,因为它不是一回事。)

2 个答案:

答案 0 :(得分:2)

您可以将任何组件添加到选项窗格中。因此,您可以创建自己的面板并使用任何布局并添加所需的任何组件。

一个问题是文本字段没有焦点。焦点将位于选项窗格的按钮上。

如果您想要关注文字字段,请查看Dialog Focus中找到的RequestFocusListener。这将允许您在显示选项窗格时控制面板上的哪个组件获得焦点。

它还包含用于创建面板的示例代码,其中GridLayout显示在选项窗格上。

答案 1 :(得分:1)

enter image description here

我将发布未来人类的代码:

attribute vec3 a_position;
attribute vec4 a_color;
uniform mat4 u_projTrans;
uniform mat4 u_worldTrans;

varying vec4 vColor;

void main() {
    vColor = a_color; // hard-coding a color will show up on screen
    gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}