1

Hey guys so I am able to set the default frame for my popup window that is called from a separate class. As you can probably tell I am an extreme noob to java. Any help would be appreciated.

This is the code that calls the second class to run.

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());}
  }
}

And here is the second Class

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);
 }
}

And here is my issue visually after executing the code. The bottom window is what I want the top one to look like.

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chris McDonald
  • 73
  • 3
  • 11
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) The log-in should be displayed in a modal `JDialog` or a `JOptionPane`. 2) AFAIU the pop-up should use the current PLAF. How was the PLAF set? For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 27 '13 at 23:13
  • How do I attach the SSCCE? – Chris McDonald Mar 27 '13 at 23:19
  • The same way you 'attached' the code snippet and class above, except self contained, one source file. Read the [linked document](http://sscce.org/) for details. – Andrew Thompson Mar 27 '13 at 23:22
  • Don't think I did it right but oh well. – Chris McDonald Mar 27 '13 at 23:28

2 Answers2

6

You have to use UIManager to "set the look and feel" of your windows:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
evuez
  • 3,257
  • 4
  • 29
  • 44
  • ..sometimes to be accompanied with [`updateComponentTreeUI(Component)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#updateComponentTreeUI%28java.awt.Component%29). Without an SSCCE, it is hard to say one way or the other. See [this example](http://stackoverflow.com/a/5630271/418556) that can change PLAF at run-time (and all dialogs etc. also change). – Andrew Thompson Mar 27 '13 at 23:27
  • In a main() method, with appropriate try/catch (see http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html) – evuez Mar 27 '13 at 23:38
2

As evuez has already stated (+1), you simply need to install the system look and feel.

Check out How to set the look and feel for more details.

The following example basically installs the system look and feel before it does anything else. This is very important, otherwise you could end up with very nasty paint effects and mismatched rendering updates.

enter image description here

Obviously, I'm using the Mac OS look and feel, but it will use what ever is the system look and feel for what ever platform you run it on.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test02 {

    public static void main(String[] args) {
        new Test02();
    }

    public Test02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }
                JFrame frame = new JFrame("The Java Cave ");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBackground(Color.BLACK);
            JLabel label = new JLabel("Welcome to the Jave Cave, be afraid, be very afraid");
            label.setForeground(Color.GREEN);
            add(label);
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366