简体   繁体   English

java框架对象的问题

[英]issue with java frame objects

I am working on my homework assignment. 我正在完成我的家庭作业。 I have a main class that uses the following call to access the following JSplash class. 我有一个主类使用以下调用来访问以下JSplash类。

JSplash splash = new JSplash();

However when I use that in my main method to create a frame with the JSplash class it compiles....but no window comes up. 但是当我在我的main方法中使用它来创建一个带有JSplash类的框架时,它会编译....但是没有窗口出现。 I cannot understand what I am doing incorrectly. 我无法理解我做错了什么。

The DFrame class it implements is a class I created as an extension of JFrame I will post it at the bottom of this code as well 它实现的DFrame类是我创建的类作为JFrame的扩展我将在此代码的底部发布它

package InventoryApp;

//Import
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Curtis
 */
public class JSplash extends DFrame implements ActionListener
{
//declaration of variable objects
Font myFont = new Font("Arial", Font.BOLD, 20);
JButton myButton = new JButton("Click Me!");
Color bgColor = new Color(0,0,255);
Color firstColor = new Color(255,255,255);
String first = "Welcome to DaemoDynamics!";
String last = "Click the Button";
String middle = "";
String middle2 = "";
private static int count = 1;  
JSplash frame = new JSplash();
//Constructor
public JSplash()
        {
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLayout (new BorderLayout());
            add(myButton, BorderLayout.SOUTH);
            setDefaultLookAndFeelDecorated(true);
            getContentPane().setBackground(bgColor);
            //adds action listener
            myButton.addActionListener(this);
            final int TALL = 316;
            final int WIDE = 304;
            frame.setSize(WIDE, TALL);
            frame.setVisible(true);
        }
//Paint method
@Override
public void paint(Graphics e)
{
    super.paint(e);
    e.setFont(myFont);
    e.setColor(firstColor);
    e.drawString(first, 14, 80);
    e.drawString(last, 70, 240);
    e.drawString(middle, 75, 150);
    e.drawString(middle2, 60, 175);
}

//Listener Method
@Override
public void actionPerformed(ActionEvent e) 
{
    //First Time button hit
    if(count == 1)
    {
        middle = "Brighter Business";
            middle2 = "for A Brighter Future";
            last = "Click Again to Begin";
            repaint();
        //increases button count
        count ++;
    }
    else//if button count is not 1
    {
        frame.setVisible(false);
        FinalProject app = new FinalProject();
    }  
}
}

Now, the following code is my DFrame class. 现在,以下代码是我的DFrame类。 It works properly when I use it with other classes. 当我将其与其他类一起使用时,它可以正常工作。

package InventoryApp;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;

/**
 *
 * @author Curtis
 */
public class DFrame extends JFrame
{
//variables
final int WIDE = 304;
final int HIGH = 316;
String x = "";
Container con = getContentPane();
//frame constructor
public DFrame()
{
    super("Item Inventory Application");
    setSize(WIDE, HIGH);
    setDefaultLookAndFeelDecorated(true);
    con.setBackground(Color.BLUE);
    con.setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

errors: 错误:

    Exception in thread "main" java.lang.StackOverflowError
    at java.util.HashMap.get(HashMap.java:317)
    at java.awt.VKCollection.findCode(AWTKeyStroke.java:885)
    at java.awt.AWTKeyStroke.getVKValue(AWTKeyStroke.java:613)
    at java.awt.AWTKeyStroke.getAWTKeyStroke(AWTKeyStroke.java:568)
    at javax.swing.KeyStroke.getKeyStroke(KeyStroke.java:310)
    at javax.swing.LookAndFeel.loadKeyBindings(LookAndFeel.java:436)
    at javax.swing.LookAndFeel.makeComponentInputMap(LookAndFeel.java:388)
    at javax.swing.plaf.basic.BasicMenuBarUI.getInputMap(BasicMenuBarUI.java:120)
    at  
    javax.swing.plaf.basic.BasicMenuBarUI.installKeyboardActions(BasicMenuBarUI.java:106)
    at javax.swing.plaf.basic.BasicMenuBarUI.installUI(BasicMenuBarUI.java:75)
    at javax.swing.plaf.metal.MetalMenuBarUI.installUI(MetalMenuBarUI.java:65)
    at javax.swing.JComponent.setUI(JComponent.java:664)
    at javax.swing.JMenuBar.setUI(JMenuBar.java:136)
    at javax.swing.JMenuBar.updateUI(JMenuBar.java:145)
    at javax.swing.JMenuBar.<init>(JMenuBar.java:113)
    at javax.swing.plaf.metal.MetalTitlePane$SystemMenuBar.<init>  
    ( MetalTitlePane.java:846)
    at javax.swing.plaf.metal.MetalTitlePane$SystemMenuBar.<init>
    (MetalTitlePane.java:846)
    at javax.swing.plaf.metal.MetalTitlePane.createMenuBar(MetalTitlePane.java:364)
    at
    javax.swing.plaf.metal.MetalTitlePane.installSubcomponents(MetalTitlePane.java:279)
    at javax.swing.plaf.metal.MetalTitlePane.<init>(MetalTitlePane.java:178)
    at javax.swing.plaf.metal.MetalRootPaneUI.createTitlePane(MetalRootPaneUI.java:318)
    at    

  javax.swing.plaf.metal.MetalRootPaneUI.installClientDecorations(MetalRootPaneUI.java:272)
    at javax.swing.plaf.metal.MetalRootPaneUI.propertyChange(MetalRootPaneUI.java:416)
    at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
    at  java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:283)
    at java.awt.Component.firePropertyChange(Component.java:8422)
    at javax.swing.JComponent.firePropertyChange(JComponent.java:4510)
    at javax.swing.JRootPane.setWindowDecorationStyle(JRootPane.java:444)
    at javax.swing.JFrame.frameInit(JFrame.java:266)
    at javax.swing.JFrame.<init>(JFrame.java:225)
    at InventoryApp.DFrame.<init>(DFrame.java:30)
    at InventoryApp.JSplash.<init>(JSplash.java:40)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)
    at InventoryApp.JSplash.<init>(JSplash.java:37)

This last line goes on about 100 times 最后一行大约持续100次

I don't know why you've done this, but this is what's causing you the problem: 我不知道你为什么要这样做,但这就是造成这个问题的原因:

JSplash frame = new JSplash();
//Constructor
public JSplash()
        {
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLayout (new BorderLayout());
            add(myButton, BorderLayout.SOUTH);
            setDefaultLookAndFeelDecorated(true);
            getContentPane().setBackground(bgColor);
            //adds action listener
            myButton.addActionListener(this);
            final int TALL = 316;
            final int WIDE = 304;
            frame.setSize(WIDE, TALL);
            frame.setVisible(true);
        }

Each time you create a new instance of the JSplash, it creates a new instance of the JSplash until you run out stack space. 每次创建JSplash的新实例时,它都会创建一个新的JSplash实例,直到您用完堆栈空间。

It should look something like this. 看起来应该是这样的。

//Constructor
public JSplash()
        {
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            setLayout (new BorderLayout());
            add(myButton, BorderLayout.SOUTH);
            setDefaultLookAndFeelDecorated(true);
            getContentPane().setBackground(bgColor);
            //adds action listener
            myButton.addActionListener(this);
            final int TALL = 316;
            final int WIDE = 304;
            setSize(WIDE, TALL);
            setVisible(true);
        }

I know you have a reference else where, you don't need a reference like this, you can simply call setVisible(false) 我知道你有一个引用别的地方,你不需要像这样的引用,你可以简单地调用setVisible(false)

Remove this line from your JSplash code 从您的JSplash代码中删除此行

JSplash frame = new JSplash();

and use this. 并使用这个。 instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM