简体   繁体   English

Java.awt:未显示第二个TextArea

[英]Java.awt: Second TextArea is not shown

I'm trying to understand how Java.awt works (we need to create a GUI without GUI editor) 我试图理解Java.awt是如何工作的(我们需要在没有GUI编辑器的情况下创建GUI)

the following code does not show 2 TextAreas: 以下代码不显示2 TextAreas:

Frame fr = new Frame("Parser");
Panel buttons = new Panel();
Panel inputText = new Panel();
Panel outputText = new Panel();
String here = new String ("Insert code here...");
TextArea input = new TextArea(here, 9, 96, TextArea.SCROLLBARS_VERTICAL_ONLY);
TextArea output = new TextArea(here, 9,96,TextArea.SCROLLBARS_VERTICAL_ONLY);

public Window(){
    fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                fr.dispose();
             }
         }
    );

    fr.setSize(700, 400);
    fr.setLocation(200,100);
    fr.setResizable(false);

    fr.add(buttons);
    fr.add(inputText);
    fr.add(outputText);

    buttons.setBounds(new Rectangle(0,0,700,60));
    buttons.setBackground(new Color(200,200,200));

    inputText.setBounds(new Rectangle(0,60,700,170));
    inputText.setBackground(new Color(255,255,255));
    inputText.add(input);

    outputText.setBounds(new Rectangle(0,230,700,170));
    outputText.setBackground(new Color(200,200,200));
    outputText.add(output);

}

Obtained result: 获得的结果:

只有1个文本区域的窗口

Expected result: 预期结果:

在此输入图像描述

Your code does not respect the layout managers that your containers are using. 您的代码不尊重容器正在使用的布局管理器。 I believe that AWT Frames use a BorderLayout by default (edit: yes they do, per the Frame API . Suggestions: 我相信AWT Frames默认使用BorderLayout(根据Frame API编辑:是的)。建议:

  • In general avoid AWT for Swing which has much greater power and flexibility, although it too is showing its age, just less so than AWT. 一般来说,避免AWT for Swing具有更强大的功能和灵活性,虽然它也显示出它的年龄,但不如AWT。
  • Read up on and use layout managers in a smart way to do your heavy lifting for you. 阅读并以智能方式使用布局管理器为您完成繁重的工作。 Here it looks like a BoxLayout could help you. 这看起来像BoxLayout可以帮助你。
  • Avoid use of null layouts. 避免使用null布局。 While yes, that could offer you a quick and easy fix for your current code, it leads to the creation of very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. 虽然是的,这可以为您提供对当前代码的快速而简单的修复,但它会导致创建非常不灵活的GUI,虽然它们在一个平台上看起来很好但在大多数其他平台或屏幕分辨率上看起来很糟糕而且很难更新和维护。
  • Avoid setting bounds, sizes or locations of any components, and again let the components and their container's layout managers set the sizes for you. 避免设置任何组件的边界,大小或位置,并再次让组件及其容器的布局管理器为您设置大小。


For example: 例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class MyWindow extends JPanel {
   private static final int ROWS = 10;
   private static final int COLS = 50;
   private static final String[] BUTTON_NAMES = { "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday" };
   private static final int GAP = 3;
   private JTextArea inputTextArea = new JTextArea(ROWS, COLS);
   private JTextArea outputTextArea = new JTextArea(ROWS, COLS);

   public MyWindow() {
      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
      for (String btnName : BUTTON_NAMES) {
         buttonPanel.add(new JButton(btnName));
      }
      outputTextArea.setFocusable(false);
      outputTextArea.setEditable(false);

      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(buttonPanel);
      add(putInTitledScrollPane(inputTextArea, "Input Text"));
      add(putInTitledScrollPane(outputTextArea, "Output Text"));
   }

   private JPanel putInTitledScrollPane(JComponent component,
         String title) {
      JPanel wrapperPanel = new JPanel(new BorderLayout());
      wrapperPanel.setBorder(BorderFactory.createTitledBorder(title));
      wrapperPanel.add(new JScrollPane(component));
      return wrapperPanel;
   }

   private static void createAndShowGui() {
      MyWindow mainPanel = new MyWindow();

      JFrame frame = new JFrame("MyWindow");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Which displays as: 其中显示为:

在此输入图像描述

Use of layout managers gives you much greater ease when it comes to changing or enhancing your GUI. 在更改或增强GUI时,使用布局管理器可以更加轻松。 For example, since I'm setting my JTextArea's width with a COL constant, if I change the COL constant, the whole GUI widens, even the buttons and the button JPanel, since the layout managers are handling all the sizing. 例如,由于我使用COL常量设置JTextArea的宽度,如果我更改COL常量,整个GUI会加宽,甚至是按钮和按钮JPanel,因为布局管理器正在处理所有大小调整。 With your code, you'd have to manually change the width of every component added to the GUI, which is prone to bug creation. 使用您的代码,您必须手动更改添加到GUI的每个组件的宽度,这很容易产生错误。

Because you are manually laying out your components, you are needed to set layout to null ( setLayout(null); ) 因为您手动布局组件,所以需要将布局设置为null( setLayout(null);
so before adding any component add this line in your code. 所以在添加任何组件之前,请在代码中添加此行。

fr.setLayout(null);

Now you will get this : 现在你会得到这个: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM