我正在嘗試為我的框架添加一個邊框,以使其看起來更好,并且我嘗試了很多東西,但它們都回傳相同型別的錯誤。我是 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);
}
}
uj5u.com熱心網友回復:
由于以下行,您問題中的代碼無法編譯:
getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
您需要將其更改為:
frame.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
在我這樣做并運行你的代碼之后,我得到了:

這能解決您的問題嗎?
為了完整起見,這是我為了獲得上述視窗而運行的代碼。
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/491730.html
