I can't seem to figure out why my JFrame is empty. Where am I going wrong?
import javax.swing.*;
import java.awt.FlowLayout;
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
JCheckBox box2 = new JCheckBox("Air Conditioning");
JCheckBox box3 = new JCheckBox("Manual Tranmission");
JCheckBox box4 = new JCheckBox("Leather Seats");
JRadioButton radio1 = new JRadioButton("Car");
JRadioButton radio2 = new JRadioButton("Pickup Truck");
JRadioButton radio3 = new JRadioButton("Minivan");
JTextField text = new JTextField();
ButtonGroup group = new ButtonGroup();
public void newGUI() {
setLayout(new FlowLayout());
JPanel panel = new JPanel();
JPanel textPanel = new JPanel();
add(textPanel);
add(panel);
panel.add(box1);
panel.add(box2);
panel.add(box3);
panel.add(radio1);
panel.add(radio2);
panel.add(radio3);
group.add(radio1);
group.add(radio2);
group.add(radio3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GUI Example");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
You forgot to add the contentPane in your jFrame, something like this
frame.setContentPane(panel);
I notice you're using inheritance to build your jFrame, so in this case you need to instantiate your own class. I've refactored your code with the minimun to run an jFrame.
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
public static void main(String[] args) {
JFrame frame = new GUIExample("GUI Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(box1);
frame.setContentPane(panel);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Basically you create an JFrame, create an JPanel, add your components to this panel and set the panel to your frame with setContentPane(panel).
I'm sorry I can't test this right now, so if someone could and fix if needed, would really appreciate, but is something like this.
First of all this is quite a good try to what you are trying to do. However there seem to be some basic misunderstandings as to how you are coding the GUI.
There are two ways of making a GUI in java. Either you create frames and panel objects then add your components to them, or you can create and extend your own classes by extends JFrame. But in this case you have tried to do both. You have extended JFrame and you have created the Objects.
I actually quite like how you've tried to extended the JFrame, so I've continued this and made code that add stuff to your screen!
See below - GUIExample.java:
import javax.swing.*;
import java.awt.FlowLayout;
public class GUIExample extends JFrame {
JCheckBox box1 = new JCheckBox("Satellite Radio");
JCheckBox box2 = new JCheckBox("Air Conditioning");
JCheckBox box3 = new JCheckBox("Manual Tranmission");
JCheckBox box4 = new JCheckBox("Leather Seats");
JRadioButton radio1 = new JRadioButton("Car");
JRadioButton radio2 = new JRadioButton("Pickup Truck");
JRadioButton radio3 = new JRadioButton("Minivan");
JTextField text = new JTextField();
ButtonGroup group = new ButtonGroup();
GUIExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
newGUI();
}
public void newGUI() {
setLayout(new FlowLayout());
JPanel panel = new JPanel();
JPanel textPanel = new JPanel();
add(textPanel);
add(panel);
panel.add(box1);
panel.add(box2);
panel.add(box3);
panel.add(radio1);
panel.add(radio2);
panel.add(radio3);
group.add(radio1);
group.add(radio2);
group.add(radio3);
}
}
test.java:
public class test {
public static void main(String[] args) {
GUIExample e = new GUIExample();
}
}
To get this working. Compile both of the files and then run your program through test.java.
Moreover your original solution, nowhere did you add a newGUI() call so all that code becomes redundant. However as the main method is static, you wouldn't be able to call it anyways.
I hope this helps!
Related
I've been spending the past few hours trying to figure out how to use a panel in my GUI class from a class named algorithms. I know this question has been asked before, But I cant seem to figure out how to incorporate it into my code, so I would appreciate any form of guidance.
Specifically, in my algorithms class I was testing trying to change the JLabel named title by creating a instance of my GUI class and then trying to use title.setText(), however it returns a Null Pointer Exception.
The title JLabel is located on my JPanel named topPanel
I know I'm not doing something right, I've struggled with this for the past few hours and cant seem to make any progress. Like I said, any guidance would be appreciated.
GUI.java
import javax.swing.*;
import java.awt.*;
import java.util.Date;
public class GUI extends JFrame {
public JFrame myFrame;
public JPanel firstFitDisplay,topPanel;
public JLabel title;
public Timer timer;
public int count=0;
public GUI(){
}
public void initGUI(){
JFrame myFrame = new JFrame();
myFrame.setTitle("CIS 452 Dynamic Memory Allocation Project");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setResizable(true);
myFrame.setLayout(new BorderLayout(6,6));
myFrame.setSize(1000,700);
myFrame.setVisible(true);
//myFrame.getContentPane().setBackground(Color.white);
//setup panels
JPanel infoPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel bottom = new JPanel();
infoPanel.setBackground(Color.cyan);
centerPanel.setBackground(Color.red);
topPanel.setBackground(Color.yellow);
bottom.setBackground(Color.pink);
infoPanel.setPreferredSize(new Dimension(200,1000));
centerPanel.setPreferredSize(new Dimension(500,500));
topPanel.setPreferredSize(new Dimension(750,20));
bottom.setPreferredSize(new Dimension(750,25));
//setup layout for panels, so that we can add subpanels
infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.PAGE_AXIS));
//add panels to main frame
myFrame.add(infoPanel,BorderLayout.WEST);
myFrame.add(centerPanel,BorderLayout.CENTER);
myFrame.add(topPanel,BorderLayout.NORTH);
myFrame.add(bottom,BorderLayout.SOUTH);
// setup sub panels for infoPanel
JPanel infoSubPanel = new JPanel();
infoSubPanel.setBackground(Color.pink);
infoSubPanel.setPreferredSize(new Dimension(50,90));
infoPanel.add(infoSubPanel);
//setting up text for infoSubPanel
JLabel subPanelTitle = new JLabel("Next Process Size to be allocated");
JLabel firstFitNextUpLabel = new JLabel("First:0");
JLabel bestFitNextUpLabel = new JLabel("Best:0");
JLabel worstFitNextUpLabel = new JLabel("Worst:0");
infoSubPanel.add(subPanelTitle);
infoSubPanel.add(firstFitNextUpLabel);
infoSubPanel.add(bestFitNextUpLabel);
infoSubPanel.add(worstFitNextUpLabel);
//subClockPanel
JPanel infoSubClockPanel = new JPanel();
infoSubClockPanel.setBackground(Color.GRAY);
infoSubClockPanel.setPreferredSize(new Dimension(50,90));
infoPanel.add(infoSubClockPanel);
//setting up text for sub clock panel
JLabel clockText = new JLabel("Seconds passed: ");
int ten = 10;
JLabel clockCounter = new JLabel("0");
infoSubClockPanel.add(clockText);
infoSubClockPanel.add(clockCounter);
//logic for running timer;
timer = new Timer(1000, e -> {
clockCounter.setText(String.valueOf(count++));
});
timer.start();
//setting up sub panels for the main center panel
centerPanel.setLayout(new FlowLayout());
JPanel firstFitDisplay = new JPanel();
JPanel bestFitDisplay = new JPanel();
JPanel worstFitDisplay = new JPanel();
firstFitDisplay.setPreferredSize(new Dimension(200,500));
bestFitDisplay.setPreferredSize(new Dimension(200,500));
worstFitDisplay.setPreferredSize(new Dimension(200,500));
centerPanel.add(firstFitDisplay);
centerPanel.add(bestFitDisplay);
centerPanel.add(worstFitDisplay);
//center components
centerPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
firstFitDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
//setup title
JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
topPanel.add(title);
}
}
and here is algorithms.java
import javax.swing.*;
import java.awt.*;
public class algorithms {
public static void main(String[] args) {
GUI f = new GUI();
f.initGUI();
f.title.setText("HHHHHHHHHHH");
}
}
so your mistake is that you never define title in your GUI class. I think you intended to with this line:
JLabel title = new JLabel("Dynamic Memory Allocation Simulator");
This actually creates a new local variable called title instead of defining the global one. So the global variable is still null. To fix this simply change that to like to this:
this.title = new JLabel("Dynamic Memory Allocation Simulator");
Now it will define the global variable! This shouldn't give a null pointer exception now.
This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.
package test;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("");
textfield = new JTextField("enter text here");
JPanel bottom = new JPanel(new BorderLayout());
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel (new BorderLayout());
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom, BorderLayout.PAGE_END);
centre.add(subCentre, BorderLayout.CENTER);
}
}
Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:
You never added the panels to the frame
You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.
One simple way to achieve your desired UI is like so(I added a main method to show the window):
import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
public Test() {
super("test");
JPanel buttons = new JPanel();
JPanel centre = new JPanel();
add(buttons, BorderLayout.SOUTH); //these lines add the
add(centre, BorderLayout.CENTER); //panels to the frame
JButton clear = new JButton("Clear"); // No need
JButton copy = new JButton("Copy"); // to declare
JLabel label = new JLabel("Label"); // these
JTextField textfield = new JTextField("enter text here"); // privately
buttons.add(copy);
buttons.add(clear);
centre.add(label);
centre.add(textfield);
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
} //end constructor
//added main method to run the UI
public static void main(String[] args) {
new Experiments();
} //end main
} //end class
And it shows the window:
I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?
package lab6;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
private JLabel label;
private JButton clear;
private JButton copy;
private JTextField textfield;
public Gui(){
super("test");
clear = new JButton("Clear");
copy = new JButton("Copy");
label = new JLabel("label");
textfield = new JTextField("enter text here");
JPanel masterPanel = new JPanel(new BorderLayout());
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(100, 200));
JPanel bottom = new JPanel();
JPanel subBottom = new JPanel();
subBottom.add(copy);
subBottom.add(clear);
JPanel centre = new JPanel ();
JPanel subCentre = new JPanel();
subCentre.add(label);
subCentre.add(textfield);
bottom.add(subBottom);
centre.add(subCentre);
masterPanel.add(bottom, BorderLayout.PAGE_END);
masterPanel.add(top, BorderLayout.PAGE_START);
masterPanel.add(centre, BorderLayout.CENTER);
add(masterPanel);
}
}
I have a JPanel which is in a box layout but I am unsure how to align the JPanel to center of the window (and stay centered even if window is resized) I've tried looking for a solution but all questions seem over complicated compared to what it is that I'm looking for.
import java.awt.*;
import javax.swing.*;
public class Stacker extends JFrame {
public Stacker() {
super("Stacker");
setSize(430, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create top panel
JPanel commandPane = new JPanel();
BoxLayout vertical = new BoxLayout(commandPane,
BoxLayout.Y_AXIS);
commandPane.setLayout(vertical);
JButton subscribe = new JButton("Subscribe");
JButton unsubscribe = new JButton("Unsubscribe");
JButton refresh = new JButton("Refresh");
JButton save = new JButton("Save");
commandPane.add(subscribe);
commandPane.add(unsubscribe);
commandPane.add(refresh);
commandPane.add(save);
JMenuItem j1 = new JMenuItem("File");
JMenuItem j2 = new JMenuItem("Open");
JMenuItem j3 = new JMenuItem("Close");
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Feeds");
menu.add(j1);
menu.add(j2);
menu.add(j3);
menubar.add(menu);
setJMenuBar(menubar);
// create bottom panel
/*JPanel textPane = new JPanel();
JTextArea text = new JTextArea(4, 70);
JScrollPane scrollPane = new JScrollPane(text);
// put them together
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(commandPane);
add(scrollPane); */
setJMenuBar(menubar);
add(commandPane);
setVisible(true);
}
public static void main(String[] arguments) {
Stacker st = new Stacker();
}
}
You say you're using a BoxLayout, but is the JPanel with the BoxLayout the JPanel you want to center, or does it contain the JPanel you want to center?
If it contains the JPanel you want to center, then you can add a glue on either side of the JPanel to be centered. If it is the JPanel you want to center, then you can use GridBagLayout or BoxLayout to achieve the effect you're talking about.
Googling something like "Java center component" will give you a ton of results.
for this idea (still not clear from your description) use GridBagLayout without set for GridBagConstraints
.
.
.
import java.awt.*;
import javax.swing.*;
public class CenteredJPanel {
private JFrame frame = new JFrame("Test");
private JPanel panel = new JPanel();
private JButton subscribe = new JButton("Subscribe");
private JButton unsubscribe = new JButton("Unsubscribe");
private JButton refresh = new JButton("Refresh");
private JButton save = new JButton("Save");
public CenteredJPanel() {
panel.setLayout(new GridBagLayout());
panel.add(subscribe);
panel.add(unsubscribe);
panel.add(refresh);
panel.add(save);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CenteredJPanel centeredJLabel = new CenteredJPanel();
}
});
}
}
I'm trying to draw a gui like shown in the figure, but somehow I'm not able to place the objects in right place (I guess that the problem is with the layout) the textArea is suppose to go in the middle... but is not showing at all
package Chapter22Collections;
import javax.swing.*;
import java.awt.*;
public class Exercise226 extends JFrame {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
public Exercise226() {
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
jTextDisplay = new JTextArea();
jTextAdd = new JTextField(8);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
add(p1, BorderLayout.NORTH);
add(jTextDisplay, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
}
public static void main(String... args) {
Exercise226 gui = new Exercise226();
gui.setTitle("Numbers");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 200);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
}
The JTextArea is actually where you expect it to be but has no outline border. It is usual to place the component in a JScrollPane which will give this effect:
add(new JScrollPane(jTextDisplay), BorderLayout.CENTER);
or simply
add(new JScrollPane(jTextDisplay));
To make the textArea re-size with the window, try BoxLayout. Box is "A lightweight container that uses a BoxLayout object as its layout manager."
Box p1 = new Box(BoxLayout.X_AXIS);
How could I add spacing/padding between the elements in the frame? So the text area is more visible and centered.
Borders and padding. E.G.
Compared with:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class Exercise226 {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
private JPanel gui;
public Exercise226() {
gui = new JPanel(new BorderLayout(5,5));
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
// set the size constraints using columns/rows
jTextDisplay = new JTextArea("Here I am!", 6,20);
jTextAdd = new JTextField(8);
JPanel p1 = new JPanel(new GridLayout(1,3,3,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3,3,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
JPanel textAreaContainer = new JPanel(new GridLayout());
textAreaContainer.add(new JScrollPane(jTextDisplay));
textAreaContainer.setBorder(new TitledBorder("Text Area Here"));
gui.add(p1, BorderLayout.PAGE_START);
gui.add(textAreaContainer, BorderLayout.CENTER);
gui.add(p2, BorderLayout.PAGE_END);
gui.setBorder(new EmptyBorder(4,4,4,4));
}
public Container getGui() {
return gui;
}
public static void main(String... args) {
JFrame f = new JFrame();
Exercise226 gui = new Exercise226();
f.setContentPane(gui.getGui());
f.setTitle("Numbers");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
This code:
Primarily provides 'white space' in the GUI using different constructors for the layouts that accept 2 int arguments for horizontal & vertical spacing.
Also adds 2 borders:
An empty border around the entire GUI to provide some spacing between it and the frame decorations.
A titled border around the text area, to make it very obvious.
Does implement a change for one unnecessary part of the original code. Instead of extending frame, it simply retains an instance of one.
Uses the JScrollPane container for the text area, as suggested by #Reimeus. It adds a nice beveled border of its own to an element that needs no scroll bars.
Creates a textAreaContainer specifically so that we can set a titled border to surround the scroll pane - without interfering with its existing border. It is possible to use a CompoundBorder for the scroll pane that consists of the existing border (scroll.getBorder()) & the titled border. However that gets complicated with buttons & other elements that might change borders on selection or action. So to set an 'outermost border' for a screen element (like the text area here) - I generally prefer to wrap the entire component in another container first.
Does not create and show the GUI on the EDT. Swing GUIs should be created and modified on the EDT. Left as an exercise for the user. See Concurrency in Swing for more details.
Old Code
The original code on this answer that provides the 'comparison GUI image' seen above. IT is closely based on the original code but with the text area wrapped in a scroll pane (and gaining a beveled border because of that) & given some text to display.
import javax.swing.*;
import java.awt.*;
public class Exercise226 extends JFrame {
private JButton jbSort;
private JButton jbReverse;
private JButton jbAdd;
private JButton jbShuffle;
private JLabel jlAddnum;
private JTextArea jTextDisplay;
private JTextField jTextAdd;
public Exercise226() {
jbSort = new JButton("Sort");
jbReverse = new JButton("Reverse");
jbShuffle = new JButton("Shuffle");
jbAdd = new JButton("Add");
jlAddnum = new JLabel("Add number here: ");
// set the size constraints using columns/rows
jTextDisplay = new JTextArea("Here I am!", 6,20);
jTextAdd = new JTextField(8);
setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(jlAddnum);
p1.add(jTextAdd);
p1.add(jbAdd);
JPanel p2 = new JPanel(new GridLayout(1,3));
p2.add(jbSort);
p2.add(jbReverse);
p2.add(jbShuffle);
add(p1, BorderLayout.NORTH);
add(new JScrollPane(jTextDisplay), BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
}
public static void main(String... args) {
Exercise226 gui = new Exercise226();
gui.setTitle("Numbers");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//gui.setSize(300, 200);
gui.pack();
//gui.setLocationRelativeTo(null);
gui.setLocationByPlatform(true);
gui.setVisible(true);
}
}
Hey guys I wanted to create a JScrollPane but it won't work... and I don't know why... here's my code...
public class test extends JFrame{
public test(){
setSize(1000,600);
}
private static JButton[] remove;
private static JPanel p = new JPanel();
public static void main(String[]args){
p.setLayout(null);
JFrame t=new test();
remove = new JButton[25];
for(int i=0;i<25;i++){
remove[i]=new JButton("Remove");
remove[i].setBounds(243,92+35*i,85,25);
p.add(remove[i]);
}
JScrollPane scrollPane = new JScrollPane(p);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
t.add(scrollPane);
t.setVisible(true);
}
Umm and Im pretty sure the frame isn't big enough for these 25 buttons... But if i delete that p.setLayout(null); A horizontal scroll bar will be created automatically... I don't really know what is wrong with my code... Pls help thank you very much!
You need to set p's preferredSize for this to work.
p.setPreferredSize(new Dimension(800, 2000));
Or you could have p extend JPanel and then override the getPreferredSize() method to return the proper dimension.
And I agree -- get rid of your null layouts. Learn about and use the layout managers if you want to use Swing correctly and have robust Swing GUI's.
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Foo extends JFrame {
private static final int BUTTON_COUNT = 25;
public static void main(String[] args) {
JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, 20));
btnPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
AbstractAction removeAction = new AbstractAction("Remove") {
#Override
public void actionPerformed(ActionEvent evt) {
JButton src = (JButton) evt.getSource();
JPanel container = (JPanel) src.getParent();
container.remove(src);
container.revalidate();
container.repaint();
}
};
for (int i = 0; i < BUTTON_COUNT; i++) {
JButton removeBtn = new JButton(removeAction);
btnPanel.add(removeBtn);
}
JPanel borderPanel = new JPanel(new BorderLayout());
borderPanel.add(btnPanel, BorderLayout.NORTH);
JScrollPane scrollpane = new JScrollPane(borderPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.setPreferredSize(new Dimension(400, 800));
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scrollpane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The issue is that a scroll pane checks the component inside it for a "preferred size" so a pane with a null layout has a preferred size of (0,0). Which it ignores.
You should do something along the lines of:
p.setPreferredSize(1000,600);
And you should see some scroll bars appear, I'm not sure how accurate they will be though.