所以我有一个登录类,如果登录凭据是正确的,它基本上会打开一个新窗口。如果凭证是正确的,我会打电话给另一个班级。问题是,窗口出现但它是空的。这是我试图打开的窗口。它非常基本,你可以看到: (摘要:它只是通过网格包布局添加一些按钮和文本字段,这些按钮打开另一个窗口)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Library extends JFrame{
private static JFrame frame;
private GridBagConstraints padding;
private JButton addB;
private JTextField aB;
private JButton issueB;
private JTextField iB;
private JButton holdB;
private JTextField hB;
private JButton renewB;
private JTextField rB;
private JButton logout;
private ImageIcon logo;
private JMenuBar menubar;
private JMenu file;
private JMenuItem exit;
public Library(){
frame = new JFrame();
frame.setLayout(new GridBagLayout());
padding = new GridBagConstraints();
}
//deals with the adding of textfield and label of adding book
public void addBLabels()
{
addB = new JButton("Add Book: ");
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 4;
padding.weightx = 1;
padding.gridy = 0;
padding.weighty = 1;
frame.add(addB, padding);
aB = new JTextField(30);
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 5;
padding.weightx = 1;
padding.gridy = 0;
padding.weighty = 1;
frame.add(aB, padding);
event1 butt = new event1();
addB.addActionListener(butt);
}
public class event1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
AddBookWindow bookWin = new AddBookWindow(Library.this);
bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
bookWin.setTitle("Add Book");
bookWin.setSize(500,200);
bookWin.setLocation(300,300);
bookWin.setVisible(true);
bookWin.setResizable(false);
}
}
//deals with issue book labels and textfield
public void issueBLabels(){
issueB = new JButton("Issue Book: ");
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 4;
padding.weightx= 1;
padding.gridy = 2;
padding.weighty = 1;
frame.add(issueB, padding);
iB = new JTextField(30);
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 5;
padding.weightx = 1;
padding.gridy = 2;
padding.weighty = 1;
frame.add(iB, padding);
event2 iss = new event2();
issueB.addActionListener(iss);
}
public class event2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
AddBookWindow bookWin = new AddBookWindow(Library.this);
bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
bookWin.setTitle("Issue Book");
bookWin.setSize(500,200);
bookWin.setLocation(300,300);
bookWin.setVisible(true);
bookWin.setResizable(false);
}
}
//deals with holdbook labels and textfield
public void holdBookLabels(){
holdB = new JButton("Hold Book: ");
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 4;
padding.weightx = 1;
padding.gridy = 4;
padding.weighty = 1;
frame.add(holdB, padding);
hB = new JTextField(30);
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 5;
padding.weightx = 1;
padding.gridy = 4;
padding.weighty = 1;
frame.add(hB, padding);
event3 hold = new event3();
holdB.addActionListener(hold);
}
public class event3 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
AddBookWindow bookWin = new AddBookWindow(Library.this);
bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
bookWin.setTitle("Hold Book");
bookWin.setSize(500,200);
bookWin.setLocation(300,300);
bookWin.setVisible(true);
bookWin.setResizable(false);
}
}
//deals with the renewbook labels and textfield
public void renewBookLabels(){
renewB = new JButton("Renew Book: ");
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 4;
padding.weightx = 1;
padding.gridy = 6;
padding.weighty = 1;
frame.add(renewB, padding);
rB = new JTextField(30);
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 5;
padding.weightx = 1;
padding.gridy = 6;
padding.weighty = 1;
frame.add(rB, padding);
event4 renew = new event4();
renewB.addActionListener(renew);
}
public class event4 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
AddBookWindow bookWin = new AddBookWindow(Library.this);
bookWin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
bookWin.setTitle("Renew Book");
bookWin.setSize(500,200);
bookWin.setLocation(300,300);
bookWin.setVisible(true);
bookWin.setResizable(false);
}
}
//deals with adding the logout button
public void logOutButton(){
logout = new JButton("Logout");
padding.gridx = 5;
padding.weightx = 1;
padding.gridy = 10;
padding.weighty = 1;
padding.gridwidth = 1;
frame.add(logout, padding);
}
//deals with adding the image
public void addImage() throws IOException{
InputStream imageStream = this.getClass().getResourceAsStream("0521-1005-0822-0024_brunette_girl_smiling_and_holding_a_stack_books.jpg");
BufferedImage image = ImageIO.read(imageStream);
JLabel picLabel = new JLabel(new ImageIcon(image));
padding.gridheight = 10;
padding.fill = GridBagConstraints.VERTICAL;
padding.gridx = 0;
padding.weightx = 1;
padding.gridy = 0;
padding.weighty = 1;
frame.add(picLabel, padding);
}
private void menuBar(){
menubar = new JMenuBar();
padding.fill = GridBagConstraints.HORIZONTAL;
padding.gridx = 0;
padding.weightx = 1;
padding.gridy = 0;
padding.weighty = 1;
frame.add(menubar, padding);
file = new JMenu("File");
menubar.add(file);
exit = new JMenuItem("exit");
file.add(exit);
}
public static void main(String args[]) throws IOException{
Library gui = new Library();
//not working right now gui.menuBar();
gui.addBLabels();
gui.issueBLabels();
gui.holdBookLabels();
gui.renewBookLabels();
gui.logOutButton();
gui.addImage();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("La Biblioteca");
frame.setSize(880,430);
frame.setVisible(true);
frame.setResizable(false);
}
}
基本上在我的Login类中,我的if语句说:
if(login credentials are correct)
Library gui = new Library();
gui.setSize(880,430);
gui.setVisible(true);
gui.addBLabels();
gui.issueBLabels();
gui.holdBookLabels();
gui.renewBookLabels();
gui.logOutButton();
gui.addImage();
答案 0 :(得分:2)
首先看看The Use of Multiple JFrames, Good/Bad Practice?。
问题是Library
从JFrame
延伸,但您有另一个JFrame
作为(static
)实例字段......
public class Library extends JFrame{
private static JFrame frame;
构建Library
类时,您正在使用frame
字段,但是当您使用Library
类设置它的属性时,您有两个不同的帧。
解决方案,不要从JFrame
或任何其他顶级容器扩展,您不会向其添加任何有意义的功能,并且限制了组件的重复使用。
相反,请考虑使用CardLayout
,有关详细信息,请参阅How to Use CardLayout ...