首页 > 解决方案 > J内部框架组件未初始化

问题描述

您好,我是 Java 新手,最近尝试从使用 netbeans IDE 转为使用原始代码来制作我的 JIternalFrames,但它显示时要么没有组件,要么根本不显示。

为了纠正这个问题,我试图改变我设置组件的顺序,例如我最初将它设置为 initvariables > addcomponentstopanes > setsizes/text > setvisible,我也尝试过使用 repaint() 和 revalidate() 到 no利用。

package tester;

import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestChunk extends javax.swing.JInternalFrame 
{

    JInternalFrame consumeWindow;
    JScrollPane coScrollPane;
    JPanel panel;
    JButton eatBerriesButton, eatMushroomsButton, eatFishButton;

    public void startComponents()
    {
        consumeWindow = new JInternalFrame("Consume Panel");
        panel = new JPanel();
        eatBerriesButton = new JButton();
        eatMushroomsButton = new JButton();
        eatFishButton = new JButton();
        GridLayout myLayout;

        consumeWindow.setBorder(null);
        consumeWindow.setResizable(true);
        myLayout = new GridLayout(0, 3, 10, 5);
        panel.setLayout(myLayout);

        consumeWindow.setSize(500, 325);
        panel.setSize(500, 650);
        eatBerriesButton.setBounds(5, 5, 150, 50);
        eatMushroomsButton.setBounds(5, 60, 150, 50);
        eatFishButton.setBounds(5, 105, 150, 50);

        panel.add(eatMushroomsButton);
        panel.revalidate();
        panel.repaint();
        panel.add(eatBerriesButton);
        panel.revalidate();
        panel.repaint();
        panel.add(eatFishButton);
        panel.revalidate();
        panel.repaint();

        coScrollPane = new JScrollPane(panel);
        coScrollPane.setSize(500, 325);
        coScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        coScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        consumeWindow.setContentPane(coScrollPane);
        consumeWindow.revalidate();
        consumeWindow.repaint();

        consumeWindow.setVisible(true);
        consumeWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

问题是我最新的 JInternalFrame(完全用代码编写的)没有显示,当我让它显示时,它没有任何组件。当前的内部框架应该在 JScrollPane 中包含 3 个按钮,每个按钮之间大约有 5 个间隙。(如果您想要更多代码进行说明,请随时提出要求)。

标签: javaswinguser-interface

解决方案


推荐阅读