JFrame窗口中仅可见单个项目

时间:2020-07-27 08:48:05

标签: java swing jframe awt layout-manager

我是在看完《新波士顿》的视频后写的,然后,当我运行时,只有最后添加的项目才可见,而所有其他textFields [textField1, textField2 and textField3]都不可见。在他的视频中效果很好,但是当我尝试时,只有passwordField可见。我是一个初学者,无法找到问题所在。请帮助我解决问题所在,以及应该采取什么措施来减轻此错误。一个小的请求,请解释一些细节,因为我是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);
        
    }

}



这是窗口的图像:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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

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

我的建议如下:

  1. 请勿扩展JFrame(或JPanel)。
  2. JPanel中添加一个JFrame
  3. 将组件(和/或容器)添加到该JPanel

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

enter image description here

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);
    }
}
相关问题