为什么带有 GridBagLayout 的面板不显示内容?

发布于 2025-01-11 13:57:33 字数 4416 浏览 2 评论 0原文

我在这里写了一些东西。如果我不这样做,它就可以工作 backPanel.setLayout(new GridBagLayout);

但如果没有网格包,内容将保留在最大化屏幕的左上角。

使用网格袋,我只能在框架中看到红色的 backPanel。嗯,屏幕中间有一个灰色像素。我假设那是我的面板,但我无法将其放大。我尝试了 setSize 但它没有改变。另外,我还有 panel.setBounds(0, 0, getWidth(),getHeight());。我不知道为什么我删除了它。

我的主文件在另一个文件中。目前它唯一做的就是调用 LoginFrame

这是代码:

package first;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class LoginFrame extends JFrame implements ActionListener {

    private JTextField textField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 300);

        JPanel backPanel = new JPanel(new GridBagLayout());
        backPanel.setBackground(Color.RED);
        
        JPanel panel = new JPanel();
        panel.setSize(500, 300);
        panel.setBackground(Color.LIGHT_GRAY);
        panel.setLayout(null);

        JLabel label;
        panel.add(label = new JLabel("Username:"));
        label.setBounds(20, 100, 100, 25);

        panel.add(textField = new JTextField());
        textField.setBounds(140, 100, 200, 25);

        panel.add(label = new JLabel("Password:"));
        label.setBounds(20, 145, 100, 25);

        panel.add(passwordField = new JPasswordField());
        passwordField.setBounds(140, 145, 200, 25);

        panel.add(label = new JLabel("CTC Bank"));
        label.setFont(new Font("New Times Roman", Font.BOLD, 50));
        label.setBounds(0, 0, getWidth(), 100);
        label.setHorizontalAlignment(JLabel.CENTER);

        JButton button;
        panel.add(button = new JButton("Login"));
        button.setBounds(140, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);

        panel.add(button = new JButton("Register"));
        button.setBounds(240, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
        
        
        //add(panel);
        backPanel.add(panel);
        add(backPanel, BorderLayout.CENTER);
        revalidate();
        repaint();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static JButton defaultActionKeyEnter(JButton button, int desiredKeyCode) {

        InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);

        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
        inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));

        inputMap.put(spaceKeyPressed, "none");
        inputMap.put(spaceKeyReleased, "none");

        return button;
    }
    
    // Unfinished code dont worry bout it...
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Login")) {
            if (textField.getText().equals("Heinz")
                    && (new String(passwordField.getPassword()).equals("password123"))) {
                // color = Color.GREEN;
            } else {
                JOptionPane.showMessageDialog(this, "Wrong Username or Password", "Error", JOptionPane.WARNING_MESSAGE);
                // color = Color.RED;
            }
        } else {
            JOptionPane.showMessageDialog(this, "Cya");
            dispose();
            setVisible(false);
        }
        // panel.setBackground(color);
    }
}

我已经看到了有关此问题的问题,但没有一个答案对我的情况有帮助。

调用以下内容没有帮助。

revalidate();
repaint();

我是否可能以错误的顺序添加了它?

您觉得这段代码怎么样?你认为这样干净吗?

I wrote a little something here. It's working if I don't backPanel.setLayout(new GridBagLayout);

But without the grid bag, the content stays in the top left I maximise the screen.

With the grid bag I only get the red backPanel in the frame. Well, there is a gray pixel in the middle of the screen. I'm assuming that's my panel, but I can't make it bigger. I tried setSize but it doesn't change. Also, I had the panel.setBounds(0, 0, getWidth(),getHeight());. I'm not sure why I removed it.

My main is in the other file. The only thing it does at the moment is to call the LoginFrame.

Here is the code:

package first;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class LoginFrame extends JFrame implements ActionListener {

    private JTextField textField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 300);

        JPanel backPanel = new JPanel(new GridBagLayout());
        backPanel.setBackground(Color.RED);
        
        JPanel panel = new JPanel();
        panel.setSize(500, 300);
        panel.setBackground(Color.LIGHT_GRAY);
        panel.setLayout(null);

        JLabel label;
        panel.add(label = new JLabel("Username:"));
        label.setBounds(20, 100, 100, 25);

        panel.add(textField = new JTextField());
        textField.setBounds(140, 100, 200, 25);

        panel.add(label = new JLabel("Password:"));
        label.setBounds(20, 145, 100, 25);

        panel.add(passwordField = new JPasswordField());
        passwordField.setBounds(140, 145, 200, 25);

        panel.add(label = new JLabel("CTC Bank"));
        label.setFont(new Font("New Times Roman", Font.BOLD, 50));
        label.setBounds(0, 0, getWidth(), 100);
        label.setHorizontalAlignment(JLabel.CENTER);

        JButton button;
        panel.add(button = new JButton("Login"));
        button.setBounds(140, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);

        panel.add(button = new JButton("Register"));
        button.setBounds(240, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
        
        
        //add(panel);
        backPanel.add(panel);
        add(backPanel, BorderLayout.CENTER);
        revalidate();
        repaint();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static JButton defaultActionKeyEnter(JButton button, int desiredKeyCode) {

        InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);

        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
        inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));

        inputMap.put(spaceKeyPressed, "none");
        inputMap.put(spaceKeyReleased, "none");

        return button;
    }
    
    // Unfinished code dont worry bout it...
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Login")) {
            if (textField.getText().equals("Heinz")
                    && (new String(passwordField.getPassword()).equals("password123"))) {
                // color = Color.GREEN;
            } else {
                JOptionPane.showMessageDialog(this, "Wrong Username or Password", "Error", JOptionPane.WARNING_MESSAGE);
                // color = Color.RED;
            }
        } else {
            JOptionPane.showMessageDialog(this, "Cya");
            dispose();
            setVisible(false);
        }
        // panel.setBackground(color);
    }
}

I have seen questions about this but none of the answers were helpful in my case.

Calling the following didn't help.

revalidate();
repaint();

Did I maybe add it in the wrong order?

And how does the code look like to you? Would you consider this clean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2025-01-18 13:57:33

backPanel 的布局会错误计算“panel”的尺寸,因为“panel”没有自己的布局管理器,无法正确参与布局管理。

解决此问题的一种方法是在“backPanel”上也使用 setLayout(null),或者直接将“panel”添加到 JFrame。

使用第一个建议(“backPanel.setLayout(null);”创建后),加上以下主要方法:

    public static void main(String[] args) {
        new LoginFrame();
    }

我得到这个:

在此处输入图像描述

The layout of backPanel will mis-calculate the dimensions of "panel" because "panel" does not participate in layout management properly, without a layout manager of its own.

One solution to this is to use setLayout(null) also on the "backPanel", or add "panel" directly to the JFrame.

With the first suggestion ("backPanel.setLayout(null);" just after it is created), plus the following main method:

    public static void main(String[] args) {
        new LoginFrame();
    }

I get this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文