Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 7u7 setLookAndFeel不';不行?_Java_Swing_Netbeans_Compiler Errors - Fatal编程技术网

Java 7u7 setLookAndFeel不';不行?

Java 7u7 setLookAndFeel不';不行?,java,swing,netbeans,compiler-errors,Java,Swing,Netbeans,Compiler Errors,在Netbeans 7.2中,当我键入setLookAndFeel()时它表示找不到该方法。我做错了什么 import javax.swing.*; public class SalutonFrame extends JFrame { public SalutonFrame() throws UnsupportedLookAndFeelException { super("Saluton Mondo!"); setLookAndFeel();

在Netbeans 7.2中,当我键入
setLookAndFeel()时它表示找不到该方法。我做错了什么

import javax.swing.*;

public class SalutonFrame extends JFrame  {

    public SalutonFrame() throws UnsupportedLookAndFeelException {
        super("Saluton Mondo!");

        setLookAndFeel();

        setSize(350, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

要设置框架的外观,必须在构造函数之前通过UIManager进行设置。你可以这样做:

    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    JFrame someFrame = new JFrame();
或者,无论您希望使用什么样的类来代替示例中所示的基本Java外观。有关更多信息,请参阅

…找不到该方法

与方法
getPonyRide()
非常相似。如果我们构造的方法未在类或它扩展的任何类中定义,编译器将通知它们不存在

如果您键入类似..的内容,IDE通常会显示一个下拉菜单

instanceOfObject.
…或

this.
键入
时(或几分钟后,取决于开发框的速度),应显示可能的方法和属性列表。在选择一种之前,请仔细查看各种可能性

建议
  • 不要扩展框架,只需引用一个。喜欢
  • 不要调用
    setSize()
    ,而是在添加所有组件后调用
    pack()
  • 使用
    JFrame.DISPOSE ON\u CLOSE
    进行退出操作

  • 看起来你在读“Sams 24小时自学Java,第6版?”

    无论如何,您可以这样做:

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    
    public class SalutonFrame extends JFrame {
    
        public SalutonFrame() throws UnsupportedLookAndFeelException {
            super("Saluton Mondo!");
    
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                JFrame someFrame = new JFrame();
    
                setSize(350, 100);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            } 
    
            catch (Exception exc) {
                //  error handling
            }
        }
    }
    
    import javax.swing.*;
    
    public class SalutonFrame extends JFrame {
    
        public SalutonFrame() {
    
            super("Saluton Frame");
            setLookAndFeel();
            setSize(350, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        private void setLookAndFeel() {
    
            try {        
    
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    
            } catch (Exception exc) {
                //  error handling
            }
        }
    
        public static void main(String[] args) {
            SalutonFrame sal = new SalutonFrame();
        }
    }
    
    或者你可以这样做:

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    
    public class SalutonFrame extends JFrame {
    
        public SalutonFrame() throws UnsupportedLookAndFeelException {
            super("Saluton Mondo!");
    
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                JFrame someFrame = new JFrame();
    
                setSize(350, 100);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            } 
    
            catch (Exception exc) {
                //  error handling
            }
        }
    }
    
    import javax.swing.*;
    
    public class SalutonFrame extends JFrame {
    
        public SalutonFrame() {
    
            super("Saluton Frame");
            setLookAndFeel();
            setSize(350, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        private void setLookAndFeel() {
    
            try {        
    
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    
            } catch (Exception exc) {
                //  error handling
            }
        }
    
        public static void main(String[] args) {
            SalutonFrame sal = new SalutonFrame();
        }
    }
    

    另外,请注意,
    JFrame
    本身并不是一个
    JComponent
    。除了主机平台提供的组件外,它没有任何外观和感觉。很抱歉,但我不太理解您的第一个建议。您似乎在暗示,将类作为
    JFrame
    的实例是不好的。对我来说,这不太麻烦至少,这比反复引用框架要好得多。@fireshadow52如果框架没有自定义方法,则不应扩展它。另一种表达方式是“首选”。要降低“麻烦”因素,请使用IDE。