这里是我制作的课程..
package testdnevnik;
import javax.swing.JFrame;
public class Diary {
StartWindow startWindow = new StartWindow();
static JFrame frame = new JFrame("Diary");
int n = 0;
public Diary() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
//frame.add(new ImagePanel("Img//bg2.jpeg"));
frame.setVisible(true);
frame.add(startWindow);
startWindow.checkStudents(n);
startWindow.chooseOption();
}
public static void main(String[] args) {
new Diary();
}
}
这是主类,这里是开始窗口的面板..
package testdnevnik;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import static testdnevnik.Diary.frame;
public class StartWindow extends JPanel {
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JLabel headline = new JLabel();
JButton viewStudents = new JButton();
JButton editStudents = new JButton();
JButton addStudents = new JButton();
JButton removeStudents = new JButton();
JLabel usernameLbl = new JLabel();
JLabel passwordLbl = new JLabel();
JTextField usernameFld = new JTextField();
JPasswordField passwordFld = new JPasswordField();
JButton logIn = new JButton("Log In");
JButton signUp = new JButton("Sign Up");
public StartWindow () {
jp.setLayout( new BorderLayout() );
// headline
headline.setText("Diary");
headline.setLocation(200, 30);
headline.setHorizontalAlignment(JLabel.CENTER);
headline.setFont(new Font("Serif", Font.BOLD, 60));
headline.setForeground(Color.ORANGE);
headline.setSize(300, 80);
headline.setVisible(true);
// Buttons
//View Students - Button
viewStudents.setText("View All Students");
viewStudents.setSize(200,40);
viewStudents.setLocation(70 , 200);
viewStudents.setVisible(true);
// Edit Students - Button
editStudents.setText("Edit Students");
editStudents.setSize(200,40);
editStudents.setLocation(70 , 300);
editStudents.setVisible(true);
// Add Students - Button
addStudents.setText("Add New Students");
addStudents.setSize(200,40);
addStudents.setLocation(70 ,400);
addStudents.setVisible(true);
// Remove Students - Button
removeStudents.setText("Remove Students");
removeStudents.setSize(200,40);
removeStudents.setLocation(70 , 500);
removeStudents.setVisible(true);
// username and password labels and input field
usernameLbl.setText("Username: ");
usernameLbl.setBounds(320, 200, 108, 40);
usernameLbl.setFont(new Font(null,Font.BOLD, 20));
usernameLbl.setVisible(true);
usernameFld.setBounds(450, 205, 200, 30); // username field input
usernameFld.setVisible(true);
passwordLbl.setText("Password: ");
passwordLbl.setBounds(320, 300, 108, 40);
passwordLbl.setFont(new Font(null,Font.BOLD, 20));
passwordLbl.setVisible(true);
passwordFld.setBounds(450, 305, 200, 30); // password field input
passwordFld.setVisible(true);
// button for log in and sign up
logIn.setBounds(580, 400, 70, 40);
logIn.setVisible(true);
signUp.setBounds(480, 400, 90, 40);
signUp.setVisible(true);
// panel
jp.setSize(new Dimension(800,600));
jp.add(viewStudents); // add ViewStudents button to the panel
jp.add(editStudents); // add EditStudents button to the panel
jp.add(addStudents); // add AddStudents button to the panel
jp.add(removeStudents); // add RemoveStudents button to the panel
jp.add(usernameLbl); // username label
jp.add(passwordLbl); // password label
jp.add(usernameFld); // username input field
jp.add(passwordFld); // password input field
jp.add(logIn); //add log in button to the panel
jp.add(signUp); //add sign up button to the panel
jp.add(headline); // add headline label to the panel
jp.add(new ImagePanel("Img//bg2.jpg")); // add background image to the panel
// adding panel to the frame
frame.add(jp);
validate();
}
public void checkStudents(int n) {
int numberOfStudents = n;
if (numberOfStudents == 0) {
removeStudents.setEnabled(false);
}
}
public void chooseOption() {
viewStudents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "view students", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
editStudents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "edit students", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
addStudents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "add students", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
removeStudents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "remove students", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
logIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "succesful log in", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
signUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "sign up form", "alert", JOptionPane.INFORMATION_MESSAGE);
}
});
}
}
这里是图像背景的类
package testdnevnik;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
当我从NetBeans运行程序时,一切正常,运行完美,但是当我在NetBeans中单击“清理并生成项目”并从文件夹中的jar文件运行时,背景为白色,但组件已添加到面板很好,只是背景图像消失了,为什么??