我不知道它以前有什么问题,我只是通过添加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);
}
}
答案 0 :(得分:0)
它不起作用,因为我将所有按钮添加到背景中,并且背景被添加到jframe打开的窗格中,但是将背景添加到窗格中的if语句中,如果Maintext = true,那是为什么它什么也没显示。