0

我有一个 Swing 应用程序,在框架底部有一个 JTextArea,它跨越整个宽度,当用户出错时,JTextArea 中会显示一条错误消息。尽管发生这种情况时,右侧的两列会变宽,而左侧的两列会变窄。我到处搜索,找不到阻止这种情况发生的方法。请有人能解释一下我解决这个问题的方法吗?我的 GUI 的代码如下所示。

public GUI(Event event) {
        super("Checkpoint Manager");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocation(165,25);
        listener = new Listener(event, this);

        // Initializes labels and text fields.
        arrivalLabel = new JLabel("Arrival Time:  ");
        depLabel = new JLabel("Departure Time:  ");
        checkLabel = new JLabel("Tick For Exclusion:  ");
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        arrival = new JFormattedTextField(format);
        dep = new JFormattedTextField(format);
        check = new JCheckBox();

        // Initializes panel and buttons, also adds ActionListeners to buttons.
        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        setLayout(new FlowLayout());
        submit = new JButton("Submit");
        cancel = new JButton("Cancel");
        enterMed = new JButton("Enter Medical Checkpoint");
        enterTime = new JButton("Enter Time Checkpoint");
        submit.addActionListener(listener);
        cancel.addActionListener(listener);
        enterMed.addActionListener(listener);
        enterTime.addActionListener(listener);

        // Initializes the messageArea and sets the border for it.
        messageArea = new JTextArea();
        messageArea.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder
                (BorderFactory.createTitledBorder("Messages"),BorderFactory.createEmptyBorder(5,5,5,5)),messageArea.getBorder()));


        // Initializes the ScrollPane for the timeList and adds the timeList to it. 
        entrantModel = new DefaultListModel();
        entrantList = new JList(entrantModel);
        entrantList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        entrantPane = new JScrollPane(entrantList);
        entrantPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder
        (BorderFactory.createTitledBorder("Entrants"),BorderFactory.createEmptyBorder(5,5,5,5)),entrantPane.getBorder()));
        entrantPane.setMaximumSize(new Dimension(60,300));
        // Initializes the ScrollPane for the timeList and adds the timeList to it. 
        nodeModel = new DefaultListModel();
        nodeList = new JList(nodeModel);
        nodeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        nodePane = new JScrollPane(nodeList);
        nodePane.setMaximumSize(new Dimension(60, 300));
        nodePane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder
        (BorderFactory.createTitledBorder("Nodes"),BorderFactory.createEmptyBorder(5,5,5,5)),nodePane.getBorder()));


        // Structures the components in the frame using GridBagLayout.
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.ipadx = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(enterTime, c);

        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 2;
        c.ipadx = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(enterMed, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        c.ipady = 100;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(entrantPane, c);

        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 2;
        c.ipady = 100;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(nodePane, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(arrivalLabel, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(arrival, c);

        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(checkLabel, c);

        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(check, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(depLabel, c);

        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(dep, c);

        c.gridx = 2;
        c.gridy = 3;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(submit, c);

        c.gridx = 3;
        c.gridy = 3;
        c.gridwidth = 1;
        c.ipadx = 5;
        c.ipady = 5;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(cancel, c);

        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        c.ipadx = 20;
        c.ipady = 30;
        c.insets = new Insets(10,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(messageArea, c);

        //Adds the panel to the frame and sets the components to fit according to the size of the frame. 
        add(panel, c);

        pack(); 
        setVisible(true);
    }
4

1 回答 1

1

JScrollPane如果您不希望JTextArea每次添加文本时都扩大大小,则可以使用A :

JScrollPane scrollPane = new JScrollPane(messageArea);
scrollPane.setBorder(...);
panel.add(scrollPane, c);
于 2013-03-14T18:46:46.020 回答