1

大家好,我可以为从单独的类调用的弹出窗口设置默认框架。正如您可能会说的那样,我对 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);
 }
}

这是我在执行代码后视觉上的问题。底部窗口是我希望顶部窗口的样子。

在此处输入图像描述

4

2 回答 2

6

您必须使用UIManager 来“设置窗口的外观和感觉”:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
于 2013-03-27T23:19:54.320 回答
2

正如evuez 已经说过的(+1),您只需要安装系统外观。

查看如何设置外观以了解更多详细信息。

以下示例基本上是在执行其他任何操作之前安装系统外观。这非常重要,否则您最终可能会得到非常讨厌的绘制效果和不匹配的渲染更新。

在此处输入图像描述

显然,我使用的是 Mac OS 的外观和感觉,但它将使用系统外观和感觉,用于您运行它的任何平台。

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

    }

}
于 2013-03-27T23:40:02.403 回答