2016-03-29 10 views
0

私のクラスのプロジェクトをしようとしています。私は3列のデータを構築することになっています。Java GUIの3列の境界線のレイアウト

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列を取得します:ここでGUIクラスのコードです。私は3列が欲しい。私はそれが国境のレイアウトとは何かを持っていると推測しているが、私はそれを行うように見えない。助けてください!

+0

するためのHow to Use GridLayoutを見てください...あまり混乱し、より良いレスポンス

になります'java.awt.TextArea'ではなく、' javax.swing.JTextArea'を使用していなければなりません。すぐに問題を解決することはできませんが、おそらく他のものを解決するでしょう – MadProgrammer

+1

[実行可能な例](https: /stackoverflow.com/help/mcve)を参照してください。これはコードダンプではありませんが、あなたがしていることの例はあなたが持っている問題を強調表示します。これにより、混乱が少なくなり、より良い応答が得られます。 – MadProgrammer

答えて

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 INSEADを使用して検討したい、が、これは

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; 
     } 

    } 

} 

は詳細

+1

@Harshad Kapoor:BoxLayoutも見てください。[here](http://stackoverflow.com/a/15104660/230513) – trashgod

関連する問題