首页 > 解决方案 > 我的 JFrame 不会显示我的容器并且没有错误

问题描述

我不知道它以前有什么问题,我只是通过添加 if 和读取文件来更改它,代码是一个通用的 jframe,它在其中读取文件并设置它读取文件时出现的按钮,但它不再起作用了。我已经测试过它是否正确读取了文件,所以它不是数据。下面是我的代码。如果缩进不正确,我很抱歉谢谢

import java.awt.AWTException;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project 
 Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

 /**
 *
 * @author cyoung
 */
public class Frametemplate {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;
    String text1 = "";

    boolean Button1 = false;

String Redirect1 = "";
String Redirect2 = "";
String Class ="";
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {

                new Frametemplate().Start1();

             }
    });
    // TODO code application logic here
}
public  void add(Container pane){
    try {
            FileInputStream saveFile = new FileInputStream("JFrame.sav");
            ObjectInputStream save = new ObjectInputStream(saveFile);


            text1 = (String) save.readObject();

            text7 = (String) save.readObject();
            Class = (String) save.readObject();


            save.close();
        } catch (Exception exc) {

            exc.printStackTrace();
        }
    try {
            FileInputStream saveFile = new FileInputStream("JFrame1.sav");
            ObjectInputStream save = new ObjectInputStream(saveFile);
            Button1 = (boolean) save.readObject();



            save.close();
        } catch (Exception exc) {

            exc.printStackTrace();
        }
    JOptionPane.showMessageDialog(null, Button5+Class+text5+Redirect1);


    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
    URL resource = getClass().getResource("Graphitebackground.v2.jpg");


    ImageIcon i2 = new ImageIcon(resource);
    URL resource3 = getClass().getResource("Graphitebackground.v4.jpg");


    ImageIcon i3 = new ImageIcon(resource3);
    URL resource1 = getClass().getResource("Graphitebackground.v3.jpg");
    ImageIcon i1 = new ImageIcon(resource1);  
    JLabel background = new JLabel(i2);
    background.setSize(new Dimension(1000,1000));
    background.setVisible(true);
    background.setLayout(new GridBagLayout());
    JButton button;
pane.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
}

    if (Button1 == true){
        JLabel label2 = new JLabel();
        if (shouldWeightX) {
        c.weightx = 0.5;
        }
        label2.setText(text1);
        label2.setPreferredSize(new Dimension(30,-50));

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = -1;
        label2.setForeground(Color.RED);
        label2.setFont(new Font("Arial", Font.BOLD,50));
        background.add(label2, c);
    }




   //third row
        background.add(l1, c);
        pane.add(background);
    }
}



public  void Start1(){











   JFrame frame = new JFrame("Tank Game Online");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    //Set up the content pane.
    add(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    frame.setVisible(true);

}

}

标签: javajframecontainers

解决方案


它不起作用,因为我将所有按钮添加到背景中,并且背景被添加到 jframe 打开的窗格中,但是将背景添加到窗格中是在 if 语句中 if Maintext = true 这就是它的原因什么都不显示。


推荐阅读