只有一个项目在JFrame窗口中可见。

huangapple go评论89阅读模式
英文:

Only single item is visible in the JFrame Window

问题

以下是翻译好的部分:

package learningPackage;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

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);
        add(textField1);

        textField2 = new JTextField("Enter a Text");
        add(textField2);

        textField3 = new JTextField("Uneditable", 20);
        textField3.setEditable(false);
        add(textField3);

        passwordField = new JPasswordField("myPassword");
        add(passwordField);

        thehandler Handler = new thehandler();
        textField1.addActionListener(Handler);
        textField2.addActionListener(Handler);
        textField3.addActionListener(Handler);
        passwordField.addActionListener(Handler);
    }

    class thehandler implements ActionListener {
        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());

            JOptionPane.showConfirmDialog(null, string);
        }
    }
}

public class Apples {
    public static void main(String[] args) {
        tuna Srivathsan = new tuna();
        Srivathsan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Srivathsan.setSize(500, 500);
        Srivathsan.setVisible(true);
        Srivathsan.setLocationRelativeTo(null);
    }
}

请注意,我已按照您的要求,仅提供了代码的翻译部分,没有添加额外的内容。如果您需要进一步的帮助或解释,请随时提问。

英文:

I wrote this after seeing videos of thenewboston and then, when I ran, only the last item added was visible and all other textFields [textField1, textField2 and textField3] are not visible. It worked perfectly in his videos but when I tried, only the passwordField was visible. I'm a beginner and I was unable to find what's the problem. Please help me out what is the problem and what should I do to mitigate this error. One small request, please explain a bit detail because I a newbie to Java and 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);
}
}

Here is the image of the window :

只有一个项目在JFrame窗口中可见。

答案1

得分: 2

import java.awt.FlowLayout;

// 导入布局
那个语句**导入**了布局并使其在代码中可用但不会在任何容器上设置布局

虽然`JPanel`**默认**布局是`FlowLayout`,(`JFrame`的内容窗格的默认布局是`BorderLayout`。`BorderLayout`在每个5个约束中最多接受5个组件或容器`JPanel`)。如果不给出约束它默认为`CENTER`。如果将多个组件添加到`CENTER`(`BorderLayout`的任何其他区域),除了一个组件外其他所有组件都将无法显示我记不清是第一个还是最后一个出现了但这是一个无关紧要的问题因为代码不应该这样做

我的建议如下

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

这里有一个实现第2和第3点的示例第1点没有被实现不包括电池)。

```java
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(); // 默认使用FlowLayout
        add(panel);

        textField1 = new JTextField(10); 
        // 设置默认值为10。
        panel.add(textField1);

        textField2 = new JTextField("Enter a Text"); 
        // 设置默认文本为"Enter a Text"(不带引号)
        panel.add(textField2);

        textField3 = new JTextField("Uneditable", 20); 
        // 显示Uneditable,默认值为20。
        // 要使此字段不可编辑,您必须执行以下操作...
        textField3.setEditable(false); 
        // 这将使textField不可编辑。
        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);
            // 当按下X按钮时,将关闭窗口。
            
            // 不要猜测大小!..
            // sclp.setSize(500, 500);
            // .. 取而代之 ..
            sclp.pack();
            sclp.setVisible(true);
            sclp.setLocationRelativeTo(null);
        };
        SwingUtilities.invokeLater(r);
    }
}
英文:
import java.awt.FlowLayout;
//gives the layout

That statement imports the layout & makes it available to the code, but does not set the layout on any container.

While a JPanel has a default layout of FlowLayout, the default layout of the (content pane of the) JFrame is BorderLayout. A BorderLayout accepts up to 5 components or containers (like JPanel) in each of 5 constraints. If no constraint is given, it defaults to the CENTER. If more than one component is added to the CENTER (or any other area of a BorderLayout), all but one of those components will fail to appear. I cannot recall if it is the first or last that appears, but it's a moot point, because the code should not do that.

My advice would be as follows:

  1. Don't extend JFrame (or JPanel).
  2. Add one JPanel to the JFrame.
  3. Add the components (and/or containers) to that JPanel.

Here is an example implementing points 2 & 3. Point 1 is not implemented (batteries not included).

只有一个项目在JFrame窗口中可见。

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);
}
}

huangapple
  • 本文由 发表于 2020年7月27日 16:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定