Chris McDonald
Chris McDonald

Reputation: 73

JFrame Swing how to change frame from popup window to look like windows theme

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

Upvotes: 1

Views: 4358

Answers (2)

evuez
evuez

Reputation: 3387

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

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Upvotes: 6

MadProgrammer
MadProgrammer

Reputation: 347184

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

    }

}

Upvotes: 2

Related Questions