首页 \ 问答 \ 以编程方式从组件创建和选择值 - Swing(Programatically Create and Select Values From Components - Swing)

以编程方式从组件创建和选择值 - Swing(Programatically Create and Select Values From Components - Swing)

我有这个方法在点击时向JPanel添加了一些指令,一个按钮和JLabel,但我需要一些方法来选择这三个元素,这样我就可以设置它们的样式,我正在使用迭代所有组件并找到我想要设计风格,但它不会让我设置JPanels的边框,当你查看可用的方法时,它不是一个选项。 无论如何在底部的循环中设置JLabel的边界? 或者一种单独选择每个元素的方法。

当用户单击其他JPanel上的按钮时,将运行GenerateImageArea方法。

public void GenerateImageArea(int id)
{
    areaHeight += 200; // Extends JPanel

    i++;

    gbc.gridx = 0;
    gbc.gridy = i;
    gbc.gridwidth = 4;
    gbc.anchor = GridBagConstraints.LINE_START;
    // Add the instructions JLabel
    add(new JLabel("["+ (id+5) + "]: Select an image of maximum dimensions 720 * 350 pixels."), gbc);

    i++;
    gbc.gridx = 0;
    gbc.gridy = i;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    // Add a button to load an image
    add(new JButton("Load Image"));

    gbc.gridx = 1;
    gbc.gridwidth = 3;
    // Add the JLabel which acts as a space to display the image
    add(new JLabel(""));

    // Set colour + font of the instructions JLabel
    for (int i = 0; i < this.getComponentCount(); i++) {
        Component comp = this.getComponent(i);
        if (comp.toString().contains("]:")) {
            comp.setForeground(Settings.SITE_GREEN);
            comp.setFont(Settings.SUBTITLEFONT);
        }
        else if (comp.toString().contains("")) {
            // I need to change the border of the second JLabel
        }
    }
}

与此类似,我需要以编程方式添加JTextAreas然后样式,并在用户单击提交后从中检索数据。 我如何以编程方式添加组件,但之后能够提取输入?


I've got this method which adds some instructions, a button and JLabel to a JPanel on click but I need some way of selecting these three elements so I can style them, I'm using a solution of iterating through all components and finding the ones I want to style but It won't let me set the border of the JPanels, it's not an option when you view available methods. Is there anyway to set the border of the JLabel in the loop at the bottom? OR a way to individually select each element.

When the user clicks a button on a different JPanel the GenerateImageArea method runs.

public void GenerateImageArea(int id)
{
    areaHeight += 200; // Extends JPanel

    i++;

    gbc.gridx = 0;
    gbc.gridy = i;
    gbc.gridwidth = 4;
    gbc.anchor = GridBagConstraints.LINE_START;
    // Add the instructions JLabel
    add(new JLabel("["+ (id+5) + "]: Select an image of maximum dimensions 720 * 350 pixels."), gbc);

    i++;
    gbc.gridx = 0;
    gbc.gridy = i;
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    // Add a button to load an image
    add(new JButton("Load Image"));

    gbc.gridx = 1;
    gbc.gridwidth = 3;
    // Add the JLabel which acts as a space to display the image
    add(new JLabel(""));

    // Set colour + font of the instructions JLabel
    for (int i = 0; i < this.getComponentCount(); i++) {
        Component comp = this.getComponent(i);
        if (comp.toString().contains("]:")) {
            comp.setForeground(Settings.SITE_GREEN);
            comp.setFont(Settings.SUBTITLEFONT);
        }
        else if (comp.toString().contains("")) {
            // I need to change the border of the second JLabel
        }
    }
}

Similar to this I need to programatically add JTextAreas then style and retrieve the data from them after the user clicks submit. How can i programatically add components but be able to extract the input afterwards?


原文:
更新时间:2022-06-28 07:06

最满意答案

如果你想成为jQuery从你的javascript调用异步调用,那么一个好去处就是jQuery网站

http://api.jquery.com/jQuery.ajax/


A good place to go if you are going to be jQuery to make asynch calls from your javascript is the jQuery website

http://api.jquery.com/jQuery.ajax/

相关问答

更多