大家好,我可以为从单独的类调用的弹出窗口设置默认框架。正如您可能会说的那样,我对 java 来说是一个极端的菜鸟。任何帮助,将不胜感激。
这是调用第二个类运行的代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText(" Enter Username:");
label1.setForeground(Color.green);
text1 = new JTextField(10);
label2 = new JLabel();
label2.setText(" Enter Password:");
label2.setForeground(Color.green);
text2 = new JPasswordField(10);
SUBMIT=new JButton("SUBMIT");
SUBMIT.setOpaque(true);
SUBMIT.setBackground(Color.BLACK);
SUBMIT.setForeground(Color.green);
panel=new JPanel(new GridLayout(4,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN or DIE!!!!!");
panel.setBackground(Color.black);
setDefaultLookAndFeelDecorated(true);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("McDinger") && value2.equals("welcome1")) {
ExerciseSevenPt2 page=new ExerciseSevenPt2();
page.setVisible(true);
JLabel label = new JLabel(" Welcome to The Java Cave, "+value1 + ". " + "Are you Worthy of the Cave?");
label.setOpaque(true);
label.setForeground(Color.green);
label.setBackground(Color.black);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password OR ELSE!!!!!");
UIManager UI=new UIManager();
UI.put("OptionPane.messageForeground", Color.red);
UI.put("OptionPane.background", Color.black);
UI.put("Panel.background", Color.black);
JOptionPane.showMessageDialog(this,"Incorrect login or password Genius", "Error",JOptionPane.ERROR_MESSAGE);
setDefaultLookAndFeelDecorated(true);
}
}
}
class ExerciseSeven
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
这是第二课
import javax.swing.*;
import java.awt.*;
class ExerciseSevenPt2 extends JFrame
{
ExerciseSevenPt2()
{
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("The Java Cave ");
setSize(400,70);
setLocationRelativeTo(null);
}
}
这是我在执行代码后视觉上的问题。底部窗口是我希望顶部窗口的样子。