Java Applet gridlayout问题

时间:2014-02-24 19:00:51

标签: java swing applet layout-manager grid-layout

我的代码有点问题。我创建了一个5,1,0,0的gridlayout。我有一个文本字段,3个按钮和一个标签,其中用户输入的结果的结果分析显示在底部。现在结果可能会出现多行,具体取决于句子中的单词大小,我的问题是当显示多行结果时,我的程序布局发生变化,我不知道如何保持相同但只是标签或如果需要,Applet窗口本身会调整大小?

 public class assignment_tauqeer_abbasi extends JApplet implements ActionListener {

    JTextArea textInput;     // User Input.
    JLabel wordCountLabel;   // To display number of words.

    public void init() {

//这里的代码是Applet的自定义,包括背景颜色,文本颜色,背景文本,标签和按钮

      setBackground(Color.black);
       getContentPane().setBackground(Color.black);

       textInput = new JTextArea();
       textInput.setBackground(Color.white);

       JPanel south = new JPanel();
       south.setBackground(Color.darkGray);
       south.setLayout( new GridLayout(5,1,0,0) );

       /* Creating Analyze and Reset buttons */

       JButton countButton = new JButton("Analyze");
       countButton.addActionListener(this);
       south.add(countButton);

       JButton resetButton = new JButton("Reset");
       resetButton.addActionListener(this);
       south.add(resetButton);

       JButton fileButton = new JButton("Analyze Text File");
       fileButton.addActionListener(this);
       south.add(fileButton);

       /* Labels telling the user what to do or what the program is outputting */

       wordCountLabel = new JLabel("  No. of words:");
       wordCountLabel.setBackground(Color.black);
       wordCountLabel.setForeground(Color.red);
       wordCountLabel.setOpaque(true);
       south.add(wordCountLabel);

       /* Border for Applet. */

       getContentPane().setLayout( new BorderLayout(2,2) );

       /* Scroll bar for the text area where the user will input the text they wish to analyse. */

       JScrollPane scroller = new JScrollPane( textInput );
       getContentPane().add(scroller, BorderLayout.CENTER);
       getContentPane().add(south, BorderLayout.SOUTH);

    } // end init();

    public Insets getInsets() {
          // Border size around edges. 
       return new Insets(2,2,2,2);
    }

// Applet自定义结束

这是我的布局代码。任何帮助都会得到赞赏!

2 个答案:

答案 0 :(得分:1)

GridLayout将根据最大单元格的内容调整每个单元格的大小。请考虑使用different layoutcombination of layouts代替。

答案 1 :(得分:0)

您使用过的gridLayout可能会使您使用的五个内容复杂化。尝试使用流布局,这将自动为正在输入的新内容腾出空间。

相关问题