首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java Swing GridBagLayout JTextField添加错误消息后更改大小

Java Swing GridBagLayout JTextField添加错误消息后更改大小
EN

Stack Overflow用户
提问于 2018-09-25 22:48:33
回答 1查看 677关注 0票数 2

我已经追踪这个bug有一段时间了,似乎找不到答案。我有一个带有标签和文本字段列表的小GUI。在具有标签+文本字段的行之间,有一个空行,其中包含一个JLabel,以便根据需要填充错误消息。问题是,当这个错误消息出现时,它们上面的文本字段由于某种原因被压缩了,在尝试了几个小时不同的东西之后,我似乎找不到原因。看起来所有的标签/文本区域都稍微压缩了一些。有谁能指出这个问题吗?

我也试着在面板gridbaglayout中给每个元素更多的空间。这似乎不会改变最终的结果。

如果看一下代码,元素在“continuousTransferPanel”上,"singleTransferPanel“也有同样的问题,只有2行。

代码语言:javascript
复制
 public void modifyJobWindow()
{
    setTitle("Job Editor");

    this.setMinimumSize(new Dimension(350, 400));
    this.setSize(350, 300);
    this.setResizable(false);

    JPanel basePanel = new JPanel();
    getContentPane().add(basePanel, BorderLayout.CENTER);
    basePanel.setLayout(new BorderLayout(0, 0));

    JPanel mainPanel = new JPanel();
    basePanel.add(mainPanel, BorderLayout.CENTER);
    mainPanel.setBackground(new Color(49, 49, 47));
    mainPanel.setLayout(new BorderLayout(0, 0));

    JPanel optionPanel = new JPanel();
    optionPanel.setBackground(new Color(49, 49, 47));
    mainPanel.add(optionPanel, BorderLayout.NORTH);

    JLabel transferLabel = new JLabel("Transfer Type: ");
    transferLabel.setHorizontalAlignment(SwingConstants.LEFT);
    transferLabel.setFont(new Font("Arial", Font.BOLD, 16));
    transferLabel.setForeground(Color.WHITE);
    optionPanel.add(transferLabel);

    comboBox = new JComboBox();
    comboBox.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            if (comboBox.getSelectedItem().equals("Single File Transfer"))
            {
                acceptButton.setEnabled(true);
                continuousTransferPanel.setVisible(false);
                deleteSourceCheckBox.setSelected(false);
                deleteSourceCheckBox.setEnabled(true);
                deleteSourceCheckBox.setForeground(Color.WHITE);
                autostartCheckBox.setSelected(false);
                autostartCheckBox.setEnabled(false);
                autostartCheckBox.setForeground(Color.GRAY);
                singleTransferPanel.setVisible(true);
                clearErrors();
            }
            else if (comboBox.getSelectedItem().equals("Continuous File Transfer"))
            {
                acceptButton.setEnabled(true);
                deleteSourceCheckBox.setSelected(false);
                deleteSourceCheckBox.setEnabled(true);
                deleteSourceCheckBox.setForeground(Color.WHITE);
                singleTransferPanel.setVisible(false);
                autostartCheckBox.setEnabled(true);
                autostartCheckBox.setForeground(Color.WHITE);
                continuousTransferPanel.setVisible(true);
                clearErrors();
            }
            else
            {
                acceptButton.setEnabled(false);
                deleteSourceCheckBox.setSelected(false);
                deleteSourceCheckBox.setEnabled(false);
                deleteSourceCheckBox.setForeground(Color.GRAY);
                autostartCheckBox.setSelected(false);
                autostartCheckBox.setEnabled(false);
                autostartCheckBox.setForeground(Color.GRAY);
                singleTransferPanel.setVisible(false);
                continuousTransferPanel.setVisible(false);
                clearErrors();
            }
        }
    });
    comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "Single File Transfer", "Continuous File Transfer"}));
    comboBox.setMaximumRowCount(3);
    optionPanel.add(comboBox);

    JPanel subMainPanel = new JPanel();
    subMainPanel.setBackground(new Color(49, 49, 47));
    mainPanel.add(subMainPanel, BorderLayout.CENTER);
    GridBagLayout gbl_subMainpanel = new GridBagLayout();
    gbl_subMainpanel.columnWidths = new int[] {400};
    gbl_subMainpanel.rowHeights = new int[] {175};
    gbl_subMainpanel.columnWeights = new double[] {1.0};
    gbl_subMainpanel.rowWeights = new double[] {0.0, Double.MIN_VALUE};
    subMainPanel.setLayout(gbl_subMainpanel);

    continuousTransferPanel = new JPanel();
    GridBagConstraints gbc_continuousTransferPanel = new GridBagConstraints();
    gbc_continuousTransferPanel.anchor = GridBagConstraints.NORTH;
    gbc_continuousTransferPanel.fill = GridBagConstraints.HORIZONTAL;
    gbc_continuousTransferPanel.gridx = 0;
    gbc_continuousTransferPanel.gridy = 0;
    subMainPanel.add(continuousTransferPanel, gbc_continuousTransferPanel);
    continuousTransferPanel.setBackground(new Color(49, 49, 47));
    GridBagLayout gbl_continuousTransferPanel = new GridBagLayout();
    gbl_continuousTransferPanel.columnWidths = new int[] {100, 300};
    gbl_continuousTransferPanel.rowHeights = new int[] {25, 15, 25, 15, 25, 15, 25, 15};
    gbl_continuousTransferPanel.columnWeights = new double[] {0.0, 1.0};
    gbl_continuousTransferPanel.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    continuousTransferPanel.setLayout(gbl_continuousTransferPanel);
    continuousTransferPanel.setVisible(false);

    JLabel jobCNameLabel = new JLabel("Job Name:");
    jobCNameLabel.setFont(new Font("Arial", Font.BOLD, 12));
    jobCNameLabel.setForeground(Color.WHITE);
    GridBagConstraints gbc_jobCNameLabel = new GridBagConstraints();
    gbc_jobCNameLabel.anchor = GridBagConstraints.WEST;
    gbc_jobCNameLabel.insets = new Insets(5, 5, 0, 5);
    gbc_jobCNameLabel.gridx = 0;
    gbc_jobCNameLabel.gridy = 0;
    continuousTransferPanel.add(jobCNameLabel, gbc_jobCNameLabel);

    JLabel sourceCLabel = new JLabel("Source Folder:");
    sourceCLabel.setFont(new Font("Arial", Font.BOLD, 12));
    sourceCLabel.setForeground(Color.WHITE);
    GridBagConstraints gbc_sourceCLabel = new GridBagConstraints();
    gbc_sourceCLabel.anchor = GridBagConstraints.WEST;
    gbc_sourceCLabel.insets = new Insets(5, 5, 0, 5);
    gbc_sourceCLabel.gridx = 0;
    gbc_sourceCLabel.gridy = 2;
    continuousTransferPanel.add(sourceCLabel, gbc_sourceCLabel);

    JLabel destCLabel = new JLabel("Destination:");
    destCLabel.setForeground(Color.WHITE);
    destCLabel.setFont(new Font("Arial", Font.BOLD, 12));
    GridBagConstraints gbc_destCLabel = new GridBagConstraints();
    gbc_destCLabel.insets = new Insets(5, 5, 0, 5);
    gbc_destCLabel.anchor = GridBagConstraints.WEST;
    gbc_destCLabel.gridx = 0;
    gbc_destCLabel.gridy = 4;
    continuousTransferPanel.add(destCLabel, gbc_destCLabel);

    JLabel fileFilterLabel = new JLabel("File Regex:");
    fileFilterLabel.setForeground(Color.WHITE);
    fileFilterLabel.setFont(new Font("Arial", Font.BOLD, 12));
    GridBagConstraints gbc_fileFilterLabel = new GridBagConstraints();
    gbc_fileFilterLabel.insets = new Insets(5, 5, 0, 5);
    gbc_fileFilterLabel.anchor = GridBagConstraints.WEST;
    gbc_fileFilterLabel.gridx = 0;
    gbc_fileFilterLabel.gridy = 6;
    continuousTransferPanel.add(fileFilterLabel, gbc_fileFilterLabel);

    jobCNameField = new JTextField();
    jobCNameField.setBorder(defaultBorder);
    GridBagConstraints gbc_jobCNameField = new GridBagConstraints();
    gbc_jobCNameField.insets = new Insets(5, 0, 0, 5);
    gbc_jobCNameField.fill = GridBagConstraints.HORIZONTAL;
    gbc_jobCNameField.gridx = 1;
    gbc_jobCNameField.gridy = 0;
    continuousTransferPanel.add(jobCNameField, gbc_jobCNameField);

    errorFieldCJobName = new JLabel("");
    errorFieldCJobName.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldCJobName.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldCJobName = new GridBagConstraints();
    gbc_errorFieldCJobName.anchor = GridBagConstraints.WEST;
    gbc_errorFieldCJobName.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldCJobName.gridx = 1;
    gbc_errorFieldCJobName.gridy = 1;
    continuousTransferPanel.add(errorFieldCJobName, gbc_errorFieldCJobName);

    sourceCField = new JTextField();
    sourceCField.setBorder(defaultBorder);
    GridBagConstraints gbc_sourceCField = new GridBagConstraints();
    gbc_sourceCField.insets = new Insets(5, 0, 0, 5);
    gbc_sourceCField.fill = GridBagConstraints.HORIZONTAL;
    gbc_sourceCField.gridx = 1;
    gbc_sourceCField.gridy = 2;
    continuousTransferPanel.add(sourceCField, gbc_sourceCField);

    errorFieldCSourceField = new JLabel("");
    errorFieldCSourceField.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldCSourceField.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldCSourceField = new GridBagConstraints();
    gbc_errorFieldCSourceField.anchor = GridBagConstraints.WEST;
    gbc_errorFieldCSourceField.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldCSourceField.gridx = 1;
    gbc_errorFieldCSourceField.gridy = 3;
    continuousTransferPanel.add(errorFieldCSourceField, gbc_errorFieldCSourceField);

    destCField = new JTextField();
    destCField.setBorder(defaultBorder);
    GridBagConstraints gbc_destCField = new GridBagConstraints();
    gbc_destCField.insets = new Insets(5, 0, 0, 5);
    gbc_destCField.fill = GridBagConstraints.HORIZONTAL;
    gbc_destCField.gridx = 1;
    gbc_destCField.gridy = 4;
    continuousTransferPanel.add(destCField, gbc_destCField);

    errorFieldCDest = new JLabel("");
    errorFieldCDest.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldCDest.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldCDest = new GridBagConstraints();
    gbc_errorFieldCDest.anchor = GridBagConstraints.WEST;
    gbc_errorFieldCDest.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldCDest.gridx = 1;
    gbc_errorFieldCDest.gridy = 5;
    continuousTransferPanel.add(errorFieldCDest, gbc_errorFieldCDest);

    fileFilterField = new JTextField();
    fileFilterField.setBorder(defaultBorder);
    GridBagConstraints gbc_fileFilterField = new GridBagConstraints();
    gbc_fileFilterField.insets = new Insets(5, 0, 0, 5);
    gbc_fileFilterField.fill = GridBagConstraints.HORIZONTAL;
    gbc_fileFilterField.gridx = 1;
    gbc_fileFilterField.gridy = 6;
    continuousTransferPanel.add(fileFilterField, gbc_fileFilterField);

    errorFieldFileRegex = new JLabel("");
    errorFieldFileRegex.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldFileRegex.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldFileRegex = new GridBagConstraints();
    gbc_errorFieldFileRegex.anchor = GridBagConstraints.WEST;
    gbc_errorFieldFileRegex.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldFileRegex.gridx = 1;
    gbc_errorFieldFileRegex.gridy = 7;
    continuousTransferPanel.add(errorFieldFileRegex, gbc_errorFieldFileRegex);

    singleTransferPanel = new JPanel();
    GridBagConstraints gbc_singleTransferPanel = new GridBagConstraints();
    gbc_singleTransferPanel.anchor = GridBagConstraints.NORTH;
    gbc_singleTransferPanel.fill = GridBagConstraints.HORIZONTAL;
    gbc_singleTransferPanel.gridx = 0;
    gbc_singleTransferPanel.gridy = 0;
    subMainPanel.add(singleTransferPanel, gbc_singleTransferPanel);
    singleTransferPanel.setBackground(new Color(49, 49, 47));
    GridBagLayout gbl_singleTransferPanel = new GridBagLayout();
    gbl_singleTransferPanel.columnWidths = new int[] {100, 200};
    gbl_singleTransferPanel.rowHeights = new int[] {30, 15, 30, 15};
    gbl_singleTransferPanel.columnWeights = new double[] {0.0, 1.0};
    gbl_singleTransferPanel.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0};
    singleTransferPanel.setLayout(gbl_singleTransferPanel);
    singleTransferPanel.setVisible(false);

    JLabel sourceLabel = new JLabel("Source File:");
    sourceLabel.setFont(new Font("Arial", Font.BOLD, 12));
    sourceLabel.setForeground(Color.WHITE);
    GridBagConstraints gbc_sourceLabel = new GridBagConstraints();
    gbc_sourceLabel.anchor = GridBagConstraints.WEST;
    gbc_sourceLabel.insets = new Insets(5, 5, 0, 5);
    gbc_sourceLabel.gridx = 0;
    gbc_sourceLabel.gridy = 0;
    singleTransferPanel.add(sourceLabel, gbc_sourceLabel);

    JLabel destLabel = new JLabel("Destination:");
    destLabel.setForeground(Color.WHITE);
    destLabel.setFont(new Font("Arial", Font.BOLD, 12));
    GridBagConstraints gbc_destLabel = new GridBagConstraints();
    gbc_destLabel.insets = new Insets(5, 5, 0, 5);
    gbc_destLabel.anchor = GridBagConstraints.WEST;
    gbc_destLabel.gridx = 0;
    gbc_destLabel.gridy = 2;
    singleTransferPanel.add(destLabel, gbc_destLabel);

    sourceField = new JTextField();
    sourceField.setBorder(defaultBorder);
    GridBagConstraints gbc_sourceField = new GridBagConstraints();
    gbc_sourceField.insets = new Insets(5, 0, 0, 5);
    gbc_sourceField.fill = GridBagConstraints.HORIZONTAL;
    gbc_sourceField.gridx = 1;
    gbc_sourceField.gridy = 0;
    singleTransferPanel.add(sourceField, gbc_sourceField);

    errorFieldSourceField = new JLabel("");
    errorFieldSourceField.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldSourceField.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldSourceField = new GridBagConstraints();
    gbc_errorFieldSourceField.anchor = GridBagConstraints.WEST;
    gbc_errorFieldSourceField.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldSourceField.gridx = 1;
    gbc_errorFieldSourceField.gridy = 1;
    singleTransferPanel.add(errorFieldSourceField, gbc_errorFieldSourceField);

    destField = new JTextField();
    destField.setBorder(defaultBorder);
    GridBagConstraints gbc_destField = new GridBagConstraints();
    gbc_destField.insets = new Insets(5, 0, 0, 5);
    gbc_destField.fill = GridBagConstraints.HORIZONTAL;
    gbc_destField.gridx = 1;
    gbc_destField.gridy = 2;
    singleTransferPanel.add(destField, gbc_destField);

    errorFieldDest = new JLabel("");
    errorFieldDest.setFont(new Font("Arial", Font.BOLD, 8));
    errorFieldDest.setForeground(Color.RED);
    GridBagConstraints gbc_errorFieldDest = new GridBagConstraints();
    gbc_errorFieldDest.anchor = GridBagConstraints.WEST;
    gbc_errorFieldDest.insets = new Insets(0, 5, 0, 5);
    gbc_errorFieldDest.gridx = 1;
    gbc_errorFieldDest.gridy = 3;
    singleTransferPanel.add(errorFieldDest, gbc_errorFieldDest);

    JPanel titlePanel = new JPanel();
    basePanel.add(titlePanel, BorderLayout.NORTH);
    titlePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    titlePanel.setBackground(new Color(49, 49, 47));
    titlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

    JLabel titleLabel = new JLabel("File Transfer Job Editor");
    titleLabel.setForeground(new Color(243, 112, 33));
    titleLabel.setFont(new Font("Arial", Font.BOLD, 28));
    titlePanel.add(titleLabel);

    JPanel buttonPanel = new JPanel();
    basePanel.add(buttonPanel, BorderLayout.SOUTH);
    buttonPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
    GridBagLayout gbl_buttonPanel = new GridBagLayout();
    gbl_buttonPanel.columnWidths = new int[] {175, 175, 0};
    gbl_buttonPanel.rowHeights = new int[] {30, 30, 0};
    gbl_buttonPanel.columnWeights = new double[] {1.0, 1.0, Double.MIN_VALUE};
    gbl_buttonPanel.rowWeights = new double[] {0.0, 0.0, Double.MIN_VALUE};
    buttonPanel.setBackground(new Color(49, 49, 47));
    buttonPanel.setLayout(gbl_buttonPanel);

    autostartCheckBox = new JCheckBox("Auto Start");
    autostartCheckBox.setForeground(Color.gray);
    autostartCheckBox.setEnabled(false);
    autostartCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
    autostartCheckBox.setFont(new Font("Calibri", Font.BOLD, 16));
    autostartCheckBox.setBackground(new Color(49, 49, 47));
    GridBagConstraints gbc_autostartCheckBox = new GridBagConstraints();
    gbc_autostartCheckBox.gridwidth = 1;
    gbc_autostartCheckBox.fill = GridBagConstraints.VERTICAL;
    gbc_autostartCheckBox.insets = new Insets(0, 0, 5, 5);
    gbc_autostartCheckBox.gridx = 0;
    gbc_autostartCheckBox.gridy = 0;
    buttonPanel.add(autostartCheckBox, gbc_autostartCheckBox);

    deleteSourceCheckBox = new JCheckBox("Delete Source File");
    deleteSourceCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
    deleteSourceCheckBox.setForeground(Color.WHITE);
    deleteSourceCheckBox.setEnabled(false);
    deleteSourceCheckBox.setForeground(Color.gray);
    deleteSourceCheckBox.setFont(new Font("Calibri", Font.BOLD, 16));
    deleteSourceCheckBox.setBackground(new Color(49, 49, 47));
    GridBagConstraints gbc_deleteSourceCheckBox = new GridBagConstraints();
    gbc_deleteSourceCheckBox.gridwidth = 1;
    gbc_deleteSourceCheckBox.fill = GridBagConstraints.VERTICAL;
    gbc_deleteSourceCheckBox.insets = new Insets(0, 0, 5, 5);
    gbc_deleteSourceCheckBox.gridx = 1;
    gbc_deleteSourceCheckBox.gridy = 0;
    buttonPanel.add(deleteSourceCheckBox, gbc_deleteSourceCheckBox);

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            close();
        }
    });
    cancelButton.setFont(new Font("Calibri", Font.BOLD, 14));
    cancelButton.setPreferredSize(new Dimension(100, 30));
    GridBagConstraints gbc_generateButton = new GridBagConstraints();
    gbc_generateButton.insets = new Insets(0, 0, 10, 0);
    gbc_generateButton.gridx = 0;
    gbc_generateButton.gridy = 1;
    buttonPanel.add(cancelButton, gbc_generateButton);

    acceptButton = new JButton("Accept");
    acceptButton.setEnabled(false);
    acceptButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            success = true;
            if (comboBox.getSelectedItem().equals("Single File Transfer"))
            {

                if (sourceField.getText().length() > 0)
                {
                    File sourceCheck = new File(sourceField.getText());
                    if (sourceCheck.exists() && sourceCheck.isFile())
                    {
                        // Success
                        sourceField.setBorder(defaultBorder);
                        errorFieldSourceField.setText("");
                    }
                    else
                    {
                        sourceField.setBorder(BorderFactory.createLineBorder(Color.red));
                        errorFieldSourceField.setText("File does not exist.");
                        success = false;
                    }
                }
                else
                {
                    sourceField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldSourceField.setText("Cannot be empty.");
                    success = false;
                }

                if (destField.getText().length() > 0)
                {
                    destField.setBorder(defaultBorder);
                    errorFieldDest.setText("");
                }
                else
                {
                    destField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldDest.setText("Cannot be empty.");
                    success = false;
                }

                if (success == true)
                {
                    Job newJob = new Job("SingleJob", sourceField.getText(), destField.getText(), null, deleteSourceCheckBox.isSelected(), true);
                    // TODO: Start Job
                }

            }
            else if (comboBox.getSelectedItem().equals("Continuous File Transfer"))
            {
                success = true;

                if (jobCNameField.getText().length() > 0)
                {
                    File jobNameCheck = new File("jobs/" + jobCNameField.getText() + ".job");
                    if (jobNameCheck.exists() && modify == false)
                    {
                        jobCNameField.setBorder(BorderFactory.createLineBorder(Color.red));
                        errorFieldCJobName.setText("Job name already exists.");
                        success = false;
                    }
                    else
                    {
                        // Success
                        jobCNameField.setBorder(defaultBorder);
                        errorFieldCJobName.setText("");
                    }
                }
                else
                {
                    jobCNameField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldCJobName.setText("Cannot be empty.");
                    success = false;
                }

                if (sourceCField.getText().length() > 0)
                {
                    File sourceCheck = new File(sourceCField.getText());
                    if (sourceCheck.exists() && sourceCheck.isDirectory())
                    {
                        // Success
                        sourceCField.setBorder(defaultBorder);
                        errorFieldCSourceField.setText("");
                    }
                    else
                    {
                        sourceCField.setBorder(BorderFactory.createLineBorder(Color.red));
                        errorFieldCSourceField.setText("Directory does not exist.");
                        success = false;
                    }
                }
                else
                {
                    sourceCField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldCSourceField.setText("Cannot be empty.");
                    success = false;
                }

                if (destCField.getText().length() > 0)
                {
                    destCField.setBorder(defaultBorder);
                    errorFieldCDest.setText("");
                }
                else
                {
                    destCField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldCDest.setText("Cannot be empty.");
                    success = false;
                }

                try
                {
                    Pattern.compile(fileFilterField.getText());
                    errorFieldFileRegex.setText("");
                    fileFilterField.setBorder(defaultBorder);
                }
                catch (PatternSyntaxException exception)
                {
                    fileFilterField.setBorder(BorderFactory.createLineBorder(Color.red));
                    errorFieldFileRegex.setText("Invalid syntax.");
                    success = false;
                }

                if (success == true)
                {
                    if (modify)
                    {
                        FileTransferUtility.jobList.remove(originalJob);
                        FileTransferGUI.clearJobFromTable(originalJob);
                        // TODO: Stop if running
                    }

                    if (comboBox.getSelectedItem().equals("Continuous File Transfer"))
                    {
                        Job newJob = new Job(jobCNameField.getText(), sourceCField.getText(), destCField.getText(), fileFilterField.getText(), deleteSourceCheckBox.isSelected(),
                                autostartCheckBox.isSelected());
                        JobHandler newJobHandler = new JobHandler(newJob);
                        FileTransferUtility.jobList.add(newJobHandler);
                        newJob.writeToFile("/jobs");
                        FileTransferGUI.addJobToTable(newJobHandler);
                        if (autostartCheckBox.isSelected())
                        {
                            // TODO: Start Job
                        }
                    }
                    close();
                }
            }
        }
    });
    acceptButton.setPreferredSize(new Dimension(100, 30));
    acceptButton.setFont(new Font("Calibri", Font.BOLD, 14));
    GridBagConstraints gbc_closeButton = new GridBagConstraints();
    gbc_closeButton.insets = new Insets(0, 0, 10, 0);
    gbc_closeButton.gridx = 1;
    gbc_closeButton.gridy = 1;
    buttonPanel.add(acceptButton, gbc_closeButton);

    JPanel entryPanel = new JPanel();
    entryPanel.setBorder(null);

    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.pack();
    this.setVisible(true);
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-25 23:29:20

不是真正的答案,但我不能用我的MCVE尝试重现您的问题。注意使用数组和集合(这里是map)来简化代码:

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

@SuppressWarnings("serial")
public class MyPanel extends JPanel {
    public static final String[] LABELS = {"Job Name:", "Source Folder:", "Destination:", "File Regex:"};
    private static final int TF_COLS = 20;
    private static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 12);
    private static final Font ERROR_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 8);
    private static final String ERROR_MESSAGE = "Cannot Be Empty";
    private static final Color BACKGROUND = new Color(49, 49, 47);
    private Map<String, JTextField> labelFieldMap = new HashMap<>();
    private Map<String, JLabel> errorLabelMap = new HashMap<>();

    public MyPanel() {
        setLayout(new BorderLayout());
        setBackground(BACKGROUND);

        JPanel labelFieldPanel = new JPanel(new GridBagLayout());
        labelFieldPanel.setOpaque(false);
        for (int i = 0; i < LABELS.length; i++) {
            String text = LABELS[i];
            JLabel label = new JLabel(text);
            JTextField textField = new JTextField(TF_COLS);
            JLabel errorLabel = new JLabel("    ");

            label.setFont(LABEL_FONT);
            label.setForeground(Color.WHITE);
            errorLabel.setFont(ERROR_FONT);
            errorLabel.setForeground(Color.RED);


            labelFieldMap.put(text, textField);
            errorLabelMap.put(text, errorLabel);

            GridBagConstraints gbc = createLabelConstraint(i);
            labelFieldPanel.add(label, gbc);

            gbc = createTextFieldConstraints(i);
            labelFieldPanel.add(textField, gbc);

            gbc = createErrorLabelConstraints(i);
            labelFieldPanel.add(errorLabel, gbc);

            // add blank JLabel at the 0 position
            gbc.gridx = 0;
            labelFieldPanel.add(new JLabel(), gbc); 
        }

        JButton acceptButton = new JButton("Accept");
        acceptButton.setMnemonic(KeyEvent.VK_A);
        acceptButton.addActionListener(e -> {
            for (int i = 0; i < LABELS.length - 1; i++) {
                String text = LABELS[i];
                JTextField textField = labelFieldMap.get(text);
                JLabel errorLabel = errorLabelMap.get(text);
                if (textField.getText().trim().isEmpty()) {
                    errorLabel.setText(ERROR_MESSAGE);
                } else {
                    errorLabel.setText(" ");
                    System.out.println(text + " " + textField.getText());
                }
            }
            System.out.println();
        });
        JPanel btnPanel = new JPanel();
        btnPanel.setOpaque(false);
        btnPanel.add(acceptButton);        

        add(labelFieldPanel, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    private GridBagConstraints createErrorLabelConstraints(int i) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2 * i + 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        return gbc;
    }

    private GridBagConstraints createTextFieldConstraints(int i) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2 * i;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 0, 0, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        return gbc;
    }

    private GridBagConstraints createLabelConstraint(int i) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2 * i;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 0, 5);
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        return gbc;
    }

    private static void createAndShowGui() {
        MyPanel mainPanel = new MyPanel();

        JFrame frame = new JFrame("MyPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

生成的GUI:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52501040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档