简体   繁体   中英

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)

the following code does not show 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:

  • 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.
  • 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.
  • Avoid use of null layouts. 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.
  • 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. 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. With your code, you'd have to manually change the width of every component added to the GUI, which is prone to bug creation.

Because you are manually laying out your components, you are needed to set layout to null ( setLayout(null); )
so before adding any component add this line in your code.

fr.setLayout(null);

Now you will get this : 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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