首页 > 解决方案 > JFrame 窗口中只有单个项目可见

问题描述

我在看到 thenewboston 的视频后写了这个,然后,当我跑的时候,只有最后添加的项目是可见的,而所有其他文本字段[textField1, textField2 and textField3]都不可见。它在他的视频中完美运行,但当我尝试时,只有密码字段可见。我是初学者,我无法找到问题所在。请帮我找出问题所在,我应该怎么做才能减轻这个错误。一个小请求,请解释一下,因为我是 Java 和 GUI 的新手。

package learningPackage;

import java.awt.FlowLayout;
//gives the layout
import java.awt.event.ActionListener;
//this makes the field wait for an event.
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
//creates a field where we can type text.
import javax.swing.JPasswordField;
//creates a text field where the text typed is hidden with asterics.

class tuna extends JFrame
{
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    
    private JPasswordField passwordField;
    
    public tuna()
    {
        super("Title Text");
        
        textField1 = new JTextField(10);
        //sets a the default value of 10. 
        add(textField1);
        
        textField2 = new JTextField("Enter a Text");
        //sets default text of "Enter a Text" without the quotes
        add(textField2);
        
        textField3 = new JTextField("Uneditable", 20);
        //Displays Uneditable with a default value of 20.
        //To make this Uneditable, you must do this...
        textField3.setEditable(false);
        //this makes the textField uneditable.
        add(textField3);
        
        passwordField = new JPasswordField("myPassword");
        add(passwordField);
        
        thehandler Handler = new thehandler();      
        textField1.addActionListener(Handler);
        textField2.addActionListener(Handler);
        textField3.addActionListener(Handler);
        passwordField.addActionListener(Handler);
        /*
         * The addActionListener method takes an object of Event Handler class. 
         * 
         * Therefore, we must create an object of Event Handler Class.
         * 
         * 
         * The addActionListener method takes an object because, sometimes, we might
         * have different classes with different code to execute. So, we pass the object to 
         * identify which class code is to be executed. 
         */
    }
    
    class thehandler implements ActionListener
    {
        /*
         * In order to handle events in Java, you need Event handler Class.
         * and, that Event Handler Class must implement ActionListener.
         * 
         * What the ActionListener does is that 
         * 
         * It will wait for some Event to happen and after that particular event happens, 
         * it will implement some piece of code.
         */
        public void actionPerformed(ActionEvent event)
        {
            String string = "";
            
            if(event.getSource()==textField1)
                string = String.format("Text Field 1 : %s", event.getActionCommand());
            else if(event.getSource()==textField2)
                string = String.format("Text Field 2 : %s", event.getActionCommand());
            else if(event.getSource()==textField3)
                string = String.format("Text Field 3 : %s", event.getActionCommand());
            else if(event.getSource()==passwordField)
                string = String.format("Password Field : %s", event.getActionCommand());
            
            //when the user presses enter key after entering text, this actually stores the 
            //thing entered into the String. 
            
            JOptionPane.showConfirmDialog(null, string);
        }
    }
    
}

public class Apples {

    public static void main(String[] args) 
    {
        
        tuna Srivathsan = new tuna();
        Srivathsan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this would close the window when X button is pressed.
        
        Srivathsan.setSize(500, 500);
        Srivathsan.setVisible(true);
        Srivathsan.setLocationRelativeTo(null);
        
    }

}



这是窗口的图像:

在此处输入图像描述

标签: javaswingjframeawtlayout-manager

解决方案


import java.awt.FlowLayout;
//gives the layout

该语句导入布局并使其可用于代码,但未在任何容器上设置布局。

虽然 aJPanel默认布局为FlowLayout,但(的内容窗格)的默认布局JFrameBorderLayout. A在 5 个约束中的每一个中BorderLayout最多接受 5 个组件或容器(如)。JPanel如果没有给出约束,则默认为CENTER. 如果将多个组件添加到CENTER(或 a 的任何其他区域BorderLayout),则除了其中一个组件之外的所有组件都将无法出现。我不记得它是第一个还是最后一个出现,但这是一个有争议的问题,因为代码不应该这样做。

我的建议如下:

  1. 不要扩展JFrame(或JPanel)。
  2. 添加一个JPanelJFrame.
  3. 将组件(和/或容器)添加到JPanel.

这是实施第 2 点和第 3 点的示例。第 1 点未实施(不包括电池)。

在此处输入图像描述

import javax.swing.*;

public class SingleComponentLayoutProblem extends JFrame {

    private final JTextField textField1;
    private final JTextField textField2;
    private final JTextField textField3;

    private final JPasswordField passwordField;

    public SingleComponentLayoutProblem() {
        super("Title Text");
        
        JPanel panel = new JPanel(); // uses FlowLayout be DEFAULT
        add(panel);

        textField1 = new JTextField(10);
        //sets a the default value of 10. 
        panel.add(textField1);

        textField2 = new JTextField("Enter a Text");
        //sets default text of "Enter a Text" without the quotes
        panel.add(textField2);

        textField3 = new JTextField("Uneditable", 20);
        //Displays Uneditable with a default value of 20.
        //To make this Uneditable, you must do this...
        textField3.setEditable(false);
        //this makes the textField uneditable.
        panel.add(textField3);

        passwordField = new JPasswordField("myPassword");
        panel.add(passwordField);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            SingleComponentLayoutProblem sclp = 
                    new SingleComponentLayoutProblem();
            sclp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //this would close the window when X button is pressed.
            
            // don't guess sizes! ..
            //sclp.setSize(500, 500); 
            // .. instead ..
            sclp.pack();
            sclp.setVisible(true);
            sclp.setLocationRelativeTo(null);
        };
        SwingUtilities.invokeLater(r);
    }
}

推荐阅读