JPanel 在 JFrame 上没有改变 [英] JPanel not changing on JFrame

查看:41
本文介绍了JPanel 在 JFrame 上没有改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的想法是拥有一个全局"JFrame,然后我可以根据需要添加/删除 JPanel,以制作流畅的应用程序.目前,当我尝试从第一个 JPanel 更改为第二个时,第二个不会显示.我的代码如下:

The idea is to have one "global" JFrame which I can then add/remove JPanels as needed to make a smooth flowing application. Currently, when I try changing from the first JPanel to the second, the second won't display. My code is below:

Handler(运行应用程序的类):

Handler (class to run the app):

package com.example.Startup;

import com.example.Global.Global_Frame;

public class Handler 
{
public Handler()
{
    gf = new Global_Frame();

        gf.getAccNum();
        gf.setVisible(true);
}

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

Global_Frame gf = null;
}

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

Global_Vars gv = null;
Global_Frame gf = null;
}

全局框架:

package com.example.Global;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.example.FirstRun.AccDetails;
import com.example.FirstRun.FirstTimeRun;

public class Global_Frame extends JFrame 
{
private static final long serialVersionUID = 1L;

ActionListener val = new ActionListener()
{
    public void actionPerformed(ActionEvent e) 
    {
        getUserDetails();
    }
};

public Global_Frame()
{

    try 
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // get look and feel based on OS
    } 
    catch (ClassNotFoundException ex) // catch all errors that may occur
    {
        Logger.getLogger(Global_Frame.class.getName()).log(Level.SEVERE, null, ex);
    } 
    catch (InstantiationException ex) 
    {
        Logger.getLogger(Global_Frame.class.getName()).log(Level.SEVERE, null, ex);
    } 
    catch (IllegalAccessException ex) 
    {
        Logger.getLogger(Global_Frame.class.getName()).log(Level.SEVERE, null, ex);
    } 
    catch (UnsupportedLookAndFeelException ex) 
    {
        Logger.getLogger(Global_Frame.class.getName()).log(Level.SEVERE, null, ex);
    }

    EventQueue.invokeLater(new Runnable()
    {
        public void run() //run the class's constructor, therefore starting the UI being built
        {
            initComponents();
        }
    });
}

public void initComponents()
{
    setPreferredSize(new Dimension(600, 400)); // setting measurements of jframe

    revalidate(); // revalidate the elements that will be displayed
    repaint(); // repainting what is displayed if going coming from a different form
    pack(); // packaging everything up to use
    setLocationRelativeTo(null); // setting form position central
}

public void getAccNum()
{
    setPreferredSize(new Dimension(600, 400)); // setting measurements of jframe
    FirstTimeRun panel1 = new FirstTimeRun(val);
    add(panel1);

    revalidate();
    repaint();
    pack();
}

public void getUserDetails()
{
    getContentPane().removeAll();

    resizing(750, 500);
    AccDetails panel2 = new AccDetails();
    add(panel2);

    revalidate();
    repaint();
    pack();
}

private void resizing(int width, int height)
{
    timer = new Timer (10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            getContentPane().removeAll();
            setPreferredSize(new Dimension(sizeW, sizeH));

            revalidate();
            repaint();
            pack();

            if (!wToggle)
                sizeW += 2;

            if (!hToggle)
            sizeH += 2;

            if (toggle)
            {
                setLocationRelativeTo(null);
                toggle = false;
            }
            else
                toggle = true;

            if (sizeW == width)
                wToggle = true;

            if (sizeH == height)
                hToggle = true;

            if (hToggle && wToggle)
                timer.stop();
        }
    });

    timer.start();
}

//variables used for window resizing
private Timer timer;
private int sizeW = 600;
private int sizeH = 400;
private boolean toggle = false;
private boolean wToggle = false;
private boolean hToggle = false;

public int accNum = 0;
}

第一面板:

package com.example.FirstRun;

import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class FirstTimeRun extends JPanel 
{
private static final long serialVersionUID = 1L;

public FirstTimeRun()
{
}

public FirstTimeRun(ActionListener val)
{
    initComponents(val);
}

private void initComponents(ActionListener val) // method to build initial view for user for installation
{
    pnlStart = new JPanel[1];
    btnNext = new JButton();
    pnlStart[0] = new JPanel();

    btnNext.setText("Next"); // adding text to button for starting
    btnNext.setPreferredSize(new Dimension(80, 35)); //positioning start button
    btnNext.addActionListener(val);
    pnlStart[0].add(btnNext); // adding button to JFrame

    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    add(pnlStart[0]);
}

// objects used in UI
private JPanel[] pnlStart;
private JButton btnNext;
}

第二面板:

package com.example.FirstRun;

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AccDetails extends JPanel
{
private static final long serialVersionUID = 1L;

public AccDetails()
{
    accAssets();
}

private void accAssets()
{   
    // instantiating elements of the GUI
    pnlAccDetails = new JPanel[2];
    lblWelcome = new JLabel();
    lblMain = new JLabel();

    for (int i = 0; i < 2; i++)
        pnlAccDetails[i] = new JPanel();

    lblWelcome.setText("Welcome to Example_App"); // label welcoming user
    pnlAccDetails[0].setLayout(new BoxLayout(pnlAccDetails[0], BoxLayout.LINE_AXIS));
    pnlAccDetails[0].add(lblWelcome); // adding label to form

    lblMain.setText("<html>The following information that is collected will be used as part of the Example_App process to ensure that each user has unique Example_App paths. Please fill in all areas of the following tabs:</html>"); // main label that explains what happens, html used for formatting
    pnlAccDetails[1].setLayout(new BorderLayout());
    pnlAccDetails[1].add(Box.createHorizontalStrut(20), BorderLayout.LINE_START);
    pnlAccDetails[1].add(lblMain, BorderLayout.CENTER); //adding label to JFrame
    pnlAccDetails[1].add(Box.createHorizontalStrut(20), BorderLayout.LINE_END);

    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    add(pnlAccDetails[0]);
    add(pnlAccDetails[1]);
}

private JLabel lblWelcome;  
private JLabel lblMain;
private JPanel[] pnlAccDetails;
}

我尝试使用 CardLayout 和revalidate();"重绘();"和包();"选项,我很困惑为什么它没有显示.在此先感谢您提供的任何帮助.

I have tried using both a CardLayout and the "revalidate();" "repaint();" and "pack();" options and I'm stumped as to why it's not showing. Thanks in advance for any help that can be offered.

在削减我的代码时,如果删除调整大小"方法,则在面板更改时会显示对象.我想避免完全删除它,因为它是更改 JFrame 大小的平滑过渡.

While cutting down my code, if the "resizing" method is removed, the objects are shown when the panels change. I would like to avoid having to remove this completely as it's a smooth transition for changing the JFrame size.

推荐答案

正如编辑中提到的,问题出在调整大小的方法上.当计时器停止时,它不会去任何地方,导致 UI 无法加载.代码的修复是清除屏幕并将调整大小的调用添加到动作侦听器.然后在之后添加对下一个方法的调用:

As mentioned in the edit, the problem lay within the resizing method. When the timer stopped, it wouldn't go anywhere, causing the UI to not load. The fix to the code is clearing the screen and adding the call to resizing to the actionlistener. Then adding a call to the next method after:

timer.stop();

感谢您让我消除周围的混乱并找到问题的根源@matt &@Hovercraft Full of Eels 为你们俩点赞.

Thanks for getting me to remove the mess around it and find the source of the problem @matt & @Hovercraft Full of Eels upvotes for both of you.

这篇关于JPanel 在 JFrame 上没有改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆