1

我试图在一个框架中显示大约 7 或 8 个多个文本字段和 JLabel。我很难让他们全部出现。我知道显示 GUI 各个部分的唯一方法是创建一个 JFrame,然后在该框架内创建一个容器,然后创建多个面板并将它们添加到容器中。有一个更好的方法吗?一个不那么限制并允许我显示超过 4 帧的?(我仅限于选择 NORTH、EAST、SOUTH、WEST,并且这些位置只会显示一项)




这是我的代码:

Container content = n.getContentPane();
         Container contentTwo = n.getContentPane();

            JTextField JField = new JTextField(agentID);
            JTextField stateField = new JTextField("Running");
            JTextField transField = new JTextField(5);
            JTextField opsField  = new JTextField("0");
            stateField.setEnabled(false);
            transField.setEnabled(false);
            JField.setEnabled(false);
            opsField.setEnabled(false);
            JLabel stateLabel = new JLabel("State:");
            JLabel transLabel = new JLabel("Amount transferred:");
            JLabel opsLabel = new JLabel("Operations Completed:");
            JPanel fundsPanel = new JPanel(new BorderLayout());
            JLabel agentLabel = new JLabel("Agent ID: ");
            agentLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
            stateLabel.setLabelFor(stateField);
            transLabel.setLabelFor(transField);
            agentLabel.setLabelFor(JField);
            opsLabel.setLabelFor(opsField);
            fundsPanel.add(agentLabel, BorderLayout.WEST);
            fundsPanel.add(JField, BorderLayout.CENTER);
            content.add(fundsPanel, BorderLayout.NORTH);

            JLabel accLabel = new JLabel("Amount in $: ");
            JPanel accPanel = new JPanel(new BorderLayout());
            JPanel accPanelTwo = new JPanel(new BorderLayout());
            JPanel accPanelThree = new JPanel(new BorderLayout());
            accLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
            JFormattedTextField accTextField = new JFormattedTextField(amount);
            accTextField.setEnabled(false);
            JLabel opLabel = new JLabel("Operations per second:");
            opLabel.setDisplayedMnemonic(KeyEvent.VK_ENTER);
            JTextField accTextFieldTwo = new JTextField(Double.toString(opsPerSec), 5);
            accTextFieldTwo.setEnabled(false);
            accLabel.setLabelFor(accTextField);
            opLabel.setLabelFor(accTextFieldTwo);
            accPanel.add(accLabel, BorderLayout.WEST);
            accPanel.add(accTextField, BorderLayout.CENTER);
            accPanelTwo.add(opLabel, BorderLayout.WEST);
            accPanelTwo.add(accTextFieldTwo, BorderLayout.CENTER);
            accPanelTwo.add(opsLabel, BorderLayout.EAST);
            accPanelTwo.add(opsField, BorderLayout.NORTH);
            accPanelThree.add(stateLabel, BorderLayout.WEST);
            accPanelThree.add(stateField, BorderLayout.CENTER);
            content.add(accPanel, BorderLayout.CENTER);
            content.add(accPanelTwo, BorderLayout.EAST);
            contentTwo.add(accPanelThree, BorderLayout.EAST);



        JButton jButton1 = new JButton("Stop agent"); 
        JButton jButton2 = new JButton("Dismiss");
        jButton1.addActionListener(handler); 
        jButton2.addActionListener(handler);
        buttonPanel.setLayout(new GridLayout(2, 3, 3, 3));
        n.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        buttonPanel.add(jButton2, null);
        buttonPanel.add(jButton1, null);
        n.pack();
4

1 回答 1

1

我刚刚使用 GridBagLayout 对面板的前几个字段进行了编码,以向您展示如何使用布局。

您必须完成编码才能创建完整的表单。

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagPanel {

    protected static final Insets leftInsets = new Insets(10, 10, 0, 0);
    protected static final Insets rightInsets = new Insets(10, 10, 0, 10);

    protected JPanel mainPanel;

    protected String agentID;

    public GridBagPanel() {
        createPartControl();
    }

    private void createPartControl() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());

        int gridy = 0;

        JLabel stateLabel = new JLabel("State:");
        addComponent(mainPanel, stateLabel, 0, gridy, 1, 1, leftInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField stateField = new JTextField("Running");
        stateField.setEnabled(false);
        stateLabel.setLabelFor(stateField);
        addComponent(mainPanel, stateField, 1, gridy++, 1, 1, rightInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel transLabel = new JLabel("Amount transferred:");
        addComponent(mainPanel, transLabel, 0, gridy, 1, 1, leftInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField transField = new JTextField(5);
        transField.setEnabled(false);
        transLabel.setLabelFor(transField);
        addComponent(mainPanel, transField, 1, gridy++, 1, 1, rightInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel opsLabel = new JLabel("Operations Completed:");
        addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField opsField = new JTextField("0");
        opsField.setEnabled(false);
        opsLabel.setLabelFor(opsField);
        addComponent(mainPanel, opsField, 1, gridy++, 1, 1, rightInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel agentLabel = new JLabel("Agent ID: ");
        addComponent(mainPanel, opsLabel, 0, gridy, 1, 1, leftInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField agentField = new JTextField(agentID);
        agentField.setEnabled(false);
        agentLabel.setLabelFor(agentField);
        addComponent(mainPanel, agentField, 1, gridy++, 1, 1, rightInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

    }

    protected void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
            int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

}
于 2013-04-23T08:19:04.873 回答