Java 摆动,错误,找不到符号方法 getRootPane()

Java swing, error, cannot find symbol method getRootPane()

我正在尝试为我的框架添加边框以使其看起来更好,我已经尝试了很多方法,但它们都是 return 相同类型的错误。我是 Java 的新手,所以我认为这与进口有关。这是我的代码。如果可以请帮忙。

package com.company;

import java.awt.*;
import javax.swing.*;

class gui{
    public static void main(String[] args){
        //Create the window/frame
        JFrame frame = new JFrame("Text Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

        //Creating the MenuBar and adding components
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("File");
        JMenu m2 = new JMenu("Help");
        mb.add(m1);
        mb.add(m2);
        JMenuItem m11 = new JMenuItem("Open");
        JMenuItem m12 = new JMenuItem("Save as");
        m1.add(m11);
        m1.add(m12);
        JMenuItem m21 = new JMenuItem("Wiki");
        JMenuItem m22 = new JMenuItem("More...");
        m2.add(m21);
        m2.add(m22);

        //Create the panel and the contents
        JPanel panel1 = new JPanel();
        JButton button = new JButton("Something I can put here");
        panel1.add(button);

        //Creates a Text Area
        JTextArea ta = new JTextArea();

        //Add contents to the window/frame
        frame.getContentPane().add(BorderLayout.SOUTH, panel1);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.setVisible(true);
    }
}

public class Main {
    public static void main(String[] args) {
        gui.main(args);
    }
}

由于以下行,您问题中的代码无法编译:

getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

您需要将其更改为:

frame.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

在我这样做之后 运行 你的代码我得到了:

这是否解决了您的问题?

为了完整起见,这里是我运行为了得到上面window.

的代码
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class gui {

    public static void main(String[] args) {
        //Create the window/frame
        JFrame frame = new JFrame("Text Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));

        //Creating the MenuBar and adding components
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("File");
        JMenu m2 = new JMenu("Help");
        mb.add(m1);
        mb.add(m2);
        JMenuItem m11 = new JMenuItem("Open");
        JMenuItem m12 = new JMenuItem("Save as");
        m1.add(m11);
        m1.add(m12);
        JMenuItem m21 = new JMenuItem("Wiki");
        JMenuItem m22 = new JMenuItem("More...");
        m2.add(m21);
        m2.add(m22);

        //Create the panel and the contents
        JPanel panel1 = new JPanel();
        JButton button = new JButton("Something I can put here");
        panel1.add(button);

        //Creates a Text Area
        JTextArea ta = new JTextArea();

        //Add contents to the window/frame
        frame.getContentPane().add(BorderLayout.SOUTH, panel1);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.setVisible(true);
    }
}