为什么我的JButton打开了同一个JFrame的多个示例?LoginWn首先打开,并有一个按钮供新用户输入他们的信息,该按钮在另一个JFrame上打开。然后将用户名和密码保存到一个文件中。NewUsrWn扩展了LoginWn以访问我为NewUsrWn类放置在那里的静态变量。问题可能是因为NewUserWn上的继承,但我我想在改变所有东西之前先确定一下。如果这是问题,他们有办法解决这个问题吗?而不必删除继承并使两个类完全分离?(P.S我已经省略了一些不必要的代码来解决主要问题)。
package loggerPack;
public class CreatesJFrameWithCompnents {
public static void main(String[] args) {
LoginWn loginWn = new NewUsrWn( );
}
}
package loggerPack;
public class LoginWn implements ActionListener, KeyListener {
JButton loginBtn = new JButton( );
JButton optBtn = new JButton( );
JButton exitBtn = new JButton( );
JTextField usrNmFld = new JTextField( );
JPasswordField pswdFld = new JPasswordField( );
JPanel panel1 = new JPanel( );
public LoginWn( ) {
frame.setBounds(100, 100, 727, 482);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set Listener for Window
JLayeredPane layeredPane = new JLayeredPane( );
GroupLayout groupLayout = new GroupLayout(frame.getContentPane( ));
frame.addKeyListener(new KeyAdapter( ) {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode( );
if (keyCode == KeyEvent.VK_UP) {
try {
sfx1.soundClip = ("C:\\Users\\Etienne Shelton\\Desktop\\Confirmation Sound.wav");
sfx1.iniateSfx( );
}
catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Audio Clip Failed To Start!!");
}
}
}
});
frame.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
exitForm( );
}
});
layeredPane = new JLayeredPane( );
groupLayout = new GroupLayout(frame.getContentPane( ));
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 744,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 463,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
loginBtn.setIcon(new ImageIcon("C:\\Users\\Etienne Shelton\\Desktop\\Java IDE
Files\\Glossy Blue JButton.png"));
loginBtn.setFocusable(false);
loginBtn.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e)
String retrvdUsrNm;
String retrvdPswd;
retrvdUsrNm = usrNmFld.getText( );
retrvdPswd = pswdFld.getText( );
try {
BufferedReader inputFile = new BufferedReader(new FileReader("userDataFile.txt"));
inputFile.readLine( );
for (int i = 0; i < fileEntries.length; i++) {
if (retrvdUsrNm.equals(cnfrmdUsrNm) && retrvdPswd.equals(cnfrmdPswd)) {
loginGd = true;
sfx1.soundClip = ("C:\\Users\\Etienne Shelton\\Desktop\\Confirmation Sound.wav");
sfx1.iniateSfx( );
new SelectionWn( );
}
else if (!retrvdUsrNm.equals(cnfrmdUsrNm) &&
!retrvdPswd.equals(cnfrmdPswd)) {
JOptionPane.showMessageDialog(frame,
"You Must Enter The Correct Username and Password!");
}
}
inputFile.close( );
}
catch (Exception ex) {
System.out.print("Error");
}
package loggerPack;
public class NewUsrWn extends LoginWn implements TestLbl {
//Instances
public JLabel lblNewLabel = new JLabel("");
public JPanel panel = new JPanel( );
public static JButton setBtn = new JButton( );
public static JTextField usrTxtFld = new JTextField( );
public static JPasswordField pswdTxtFld = new JPasswordField( );
//GUI Components
public NewUsrWn( ) {
JLayeredPane layeredPane = new JLayeredPane( );
frame.setVisible(true);
frame.setBounds(100, 100, 512, 499);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane( ));
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(layeredPane, GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(layeredPane, GroupLayout.PREFERRED_SIZE, 461, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
usrTxtFld.setBounds(152, 109, 184, 23);
usrTxtFld.setColumns(10);
usrTxtFld.setForeground(Color.BLACK);
usrTxtFld.setBackground(Color.WHITE);
layeredPane.setLayer(usrTxtFld, 3);
layeredPane.add(usrTxtFld);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Etienne Shelton\\Desktop\\Java IDE Files\\Galaxy Background 1 Resized.jpg"));
lblNewLabel.setBounds(0, 0, 496, 461);
layeredPane.add(lblNewLabel);
panel.setBackground(Color.LIGHT_GRAY);
panel.setBounds(107, 76, 279, 374);
layeredPane.setLayer(panel, 2);
layeredPane.add(panel);
setBtn.setIcon(new ImageIcon("C:\\Users\\Etienne Shelton\\Desktop\\Java IDE Files\\Glossy Blue JButton.png"));
setBtn.setBounds(193, 222, 89, 23);
setBtn.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
setBtn.requestFocus( );
setBtnActionPerformed(e);
}
});
layeredPane.setLayer(setBtn, 3);
layeredPane.add(setBtn);
//Set Letters
setLtrs.setBounds(223, 222, 89, 23);
layeredPane.setLayer(setLtrs, 4);
layeredPane.add(setLtrs);
pswdTxtFld.setForeground(Color.BLACK);
pswdTxtFld.setBackground(Color.WHITE);
pswdTxtFld.setColumns(10);
pswdTxtFld.setBounds(152, 188, 184, 23);
pswdTxtFld.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
pswdTxtFld.requestFocus( );
}
});
layeredPane.setLayer(pswdTxtFld, 3);
layeredPane.add(pswdTxtFld);
//Back Btn
bckBtn.setIcon(new ImageIcon("C:\\Users\\Etienne Shelton\\Desktop\\Java IDE Files\\Glossy Blue JButton.png"));
bckBtn.setBounds(193, 256, 89, 23);
bckBtn.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
frame.dispose( );
}
});
layeredPane.setLayer(bckBtn, 3);
layeredPane.add(bckBtn);
frame.getContentPane( ).setLayout(groupLayout);
}
}
1条答案
按热度按时间r1zhe5dt1#
在CreatesJFrameWithComponents类中,您正在创建NewUsrWn对象,但将其存储在LoginWn引用中:
String getString = new String();
由于NewUsrWn扩展了LoginWn,因此当您创建NewUsrWn时,它也将调用LoginWn的构造函数。这可能是您看到同一JFrame的多个示例的原因。
从LoginWn中删除NewUsrWn的继承。相反,为每个窗口创建单独的示例并独立处理其组件。当类之间存在明确的“is-a”关系时,应使用继承,并且在这种情况下,似乎两个类都表示具有不同功能的不同窗口。
public class StringName {
字符串
}
public class LoginWn {
型
}
public class Uncategorized {
型
}