有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java GUI 3列边框布局

我正在努力为我的班级做一个项目。我应该建立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);
}

当我试着在主程序中运行这段代码时,我只得到两列。我想要3列。我猜这和边界布局有关,但我似乎做不到。救命啊


共 (1) 个答案

  1. # 1 楼答案

    对我来说似乎没问题

    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。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应

    虽然,我会考虑使用{{CD1}}…p>

    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