Java GUI 3列边框布局

时间:2016-03-29 23:21:37

标签: java swing layout-manager

我正在尝试为我的班级做一个项目。我应该建立3列数据。这是GUI类的代码:

public void displayArray(String[] wordArray) {
    Container myContentPane = project1JFrame.getContentPane();
    TextArea arrayArea = new TextArea();
    for (int i = 0; i < wordArray.length; i++) {
        if (wordArray[i] != null) {
            arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
        }
    } //for
    myContentPane.add(arrayArea, BorderLayout.WEST);
    project1JFrame.setVisible(true);

} //displayArray

public void displaySortedArray(String[] wordArray) {
    Container myContentPane = project1JFrame.getContentPane();
    TextArea arrayArea = new TextArea();
    for (int i = 0; i < wordArray.length; i++) {
        if (wordArray[i] != null) {
            arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
        }
    } //for
    myContentPane.add(arrayArea, BorderLayout.CENTER);
    project1JFrame.setVisible(true);

} //displaySortedArray

public void displaySortedList(WordList myList) {

    Container myContentPane = project1JFrame.getContentPane();
    TextArea listArea = new TextArea();
    WordListIterator myIt;
    listArea.setText("");
    myIt = myList.reset();

    while (myIt.hasNext()) {
        myList.append(myIt.next() + "\n");
    }
    myContentPane.add(listArea, BorderLayout.EAST);
    project1JFrame.setVisible(true);
}

当我尝试将此代码与我的主程序一起运行时,我只获得2列。我想要3列。我猜这与边框布局有关但我似乎无法做到这一点。求救!

1 个答案:

答案 0 :(得分:2)

似乎对我有用......

BorderLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
            add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
            add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
        }

        protected JTextArea makeTextArea(String text) {
            JTextArea ta = new JTextArea(10, 20);
            ta.setText(text);
            return ta;
        }

    }

}

考虑提供展示您问题的runnable example。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将减少混淆和更好的响应

虽然,我考虑使用GridLayout内容......

GridLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(1, 3));
            add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
            add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
            add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
        }

        protected JTextArea makeTextArea(String text) {
            JTextArea ta = new JTextArea(10, 20);
            ta.setText(text);
            return ta;
        }

    }

}

有关详细信息,请查看How to Use GridLayout