I am trying to add a border to my frame so that it looks better and I have tried lots of things but they all return the same type of error. I am new to Java and so I assume this has something to do with the imports. Here is my code. Please help if you can.
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);
}
}
CodePudding user response:
The code in your question does not compile because of the following line:
getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
You need to change it to:
frame.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.RED));
After I did that and ran your code I got:
Does this resolve your problem?
For the sake of completeness, here is the code I ran in order to get the above 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);
}
}