Java - JMenuBar Hidden When Using Glass Pane - java

The problem seems simple, but I can't seem to get around it.
I am using GlassPane in my frame (also the ContentPane). So when I add JMenuBar to the frame it doesn't show up. If/when I am using GlassPane at other times, everything works absolutely fine. I did some research, what I understand is that JMenuBar is shown on RootPane and I believe GlassPane is somehow hiding it.
I need to know if there is any way to get JMenuBar while using glassPane?
Thanks
UPDATE:
I am setting glassPane.setOpaque(false)
UPDATE:
The actual lines of code are much more but here are the ones that are relative to the problem.
(mainPanel and notificationPanel are self constructed classes extending from JPanel)
public class Demo extends JFrame {
/////////////////////////////////////////////////////////////////////////
// JMenuBar
private final JMenuBar mainMenuBar;
private final JMenu fileMenu;
private final JMenuItem exitFileMenu;
/////////////////////////////////////////////////////////////////////////
// CONTENT PANE & COMPONENTS
private final JPanel contentPanel;
private final JPanel buttonPanel;
private final JButton button1;
/////////////////////////////////////////////////////////////////////////
// GLASSPANE AND COMPONENTS
private final JPanel glassPanel;
private final JPanel buttonPanel2;
private final JButton button2;
public Demo() {
super();
this.mainMenuBar = new JMenuBar();
this.fileMenu = new JMenu("File");
this.exitFileMenu = new JMenuItem("EXIT");
this.contentPanel = new JPanel(new BorderLayout());
this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.button1 = new JButton("Button 1");
this.glassPanel = new JPanel(new BorderLayout());
this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
this.button2 = new JButton("Button 2");
}
public void initGUI() {
this.fileMenu.add(this.exitFileMenu);
this.mainMenuBar.add(this.fileMenu);
this.buttonPanel.add(this.button1);
this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH);
this.buttonPanel2.add(this.button2);
this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH);
super.setContentPane(this.contentPanel);
super.setGlassPane(this.glassPanel);
this.glassPanel.setOpaque(false);
this.glassPanel.setVisible(true);
super.setExtendedState(JFrame.MAXIMIZED_BOTH);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setJMenuBar(mainMenuBar);
super.setVisible(true);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.initGUI();
}
}

You are using BorderLayout and the option BorderLayout.NORTH for your glassPanel. It takes the entire space in the north and it overlaps your entire menu. Therefore you don't see anything anymore. Change for example your panel creation into:
this.glassPanel = new JPanel();
Then your panel will be resized to only fit your button and you will see your menu behind. You can play around with some layouts and see which one fits. But just remember that the glass pane is always on top of everything. Just a little side note: When you add your button directly to 'glassPanel' (without using 'buttonPanel2') you can remove the little "borders". Otherwise you could resize it to perfectly fit your button. Both is possible, but if you only want to have a single component (like your button) then I would add it directly.

Alright guys I accidentally found the solution to the problem. It was in-fact simple, if you are using nested Panels within the 'glassPane' then simply set each nested panel's opacity to false. If you don't the nested panels will show its background to each of its bounds and overlap any underlying layer(s).
Here's the working code of the above Demo.
public class Demo extends JFrame {
/////////////////////////////////////////////////////////////////////////
// JMenuBar
private final JMenuBar mainMenuBar;
private final JMenu fileMenu;
private final JMenuItem exitFileMenu;
/////////////////////////////////////////////////////////////////////////
// CONTENT PANE & COMPONENTS
private final JPanel contentPanel;
private final JPanel buttonPanel;
private final JButton button1;
/////////////////////////////////////////////////////////////////////////
// GLASSPANE AND COMPONENTS
private final JPanel glassPanel;
private final JPanel buttonPanel2;
private final JButton button2;
public Demo() {
super();
this.mainMenuBar = new JMenuBar();
this.fileMenu = new JMenu("File");
this.exitFileMenu = new JMenuItem("EXIT");
this.contentPanel = new JPanel(new BorderLayout());
this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
this.button1 = new JButton("Button 1");
this.glassPanel = new JPanel(new BorderLayout());
this.buttonPanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
this.button2 = new JButton("Button 2");
}
public void initGUI() {
this.fileMenu.add(this.exitFileMenu);
this.mainMenuBar.add(this.fileMenu);
this.buttonPanel.add(this.button1);
this.contentPanel.add(this.buttonPanel, BorderLayout.NORTH);
this.buttonPanel2.add(this.button2);
this.buttonPanel2.setOpaque(false);
this.glassPanel.add(this.buttonPanel2, BorderLayout.NORTH);
super.setContentPane(this.contentPanel);
super.setGlassPane(this.glassPanel);
this.glassPanel.setOpaque(false);
this.glassPanel.setVisible(true);
super.setExtendedState(JFrame.MAXIMIZED_BOTH);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setJMenuBar(mainMenuBar);
super.setVisible(true);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.initGUI();
}
}
Now Also remember always to set the glassPane's Opacity after you call 'setGlassPane(JPanel)' Otherwise the GlassPane remain Opaque. (The nested Panels you may set before or after calling the said method)

Related

How to add to a JPanel from another class

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.

How do I align components in Java Swing?

I'm building a simple beginner app in Java and I need your help with aligning components. What I'm trying to do is to align component(JLabel "name") to the left side of the panel. I've already tried with "new FlowLayout(FlowLayout.LEFT)" but it didn't work so I'm asking you to help me. Here is the picture of the frame and source code below it.
public class firstClass extends JFrame implements ActionListener {
private JFrame frame1;
private JFrame frame2;
private JPanel mainPanelFirst;
private JPanel secondPanel;
private JButton newWindowButton;
private int mulitplyPanels;
private JLabel leftLabel;
private JLabel rightLabel;
private JComboBox leftCB;
private JComboBox rightCB;
First Window:
public JFrame createMainUI(){
frame1 = new JFrame("Main frame");
frame1.setSize(600,600);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame1.setVisible(true);
mainPanelFirst = new JPanel();
mainPanelFirst.setLayout(new FlowLayout());
frame1.add(mainPanelFirst);
newWindowButton = new JButton("Open new window");
newWindowButton.addActionListener(this);
mainPanelFirst.add(newWindowButton);
return frame1;
}
Second Window(include the label I want to align):
public JFrame createSecondUI() {
frame2 = new JFrame("Second frame");
frame2.setSize(600, 600);
frame2.setResizable(false);
frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame2.setVisible(true);
secondPanel = new JPanel();
secondPanel.setLayout(new FlowLayout());
secondPanel.setBackground(Color.gray);
frame2.add(secondPanel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(70,400,20));
topPanel.setBackground(Color.WHITE);
secondPanel.add(topPanel);
leftLabel = new JLabel("Name:");
topPanel.add(leftLabel);
return frame2;
}
#Override
public void actionPerformed(ActionEvent e) {
createSecondUI();
}
}
Thank you for your help :)
Warning read this as well: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
Since that the JFrame is non-resizable, give the topPanel a defined size:
JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(600,100));
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT,400,20));
I would suggest using a Layout Manager for your app. The one you're looking for is most likely BorderLayout, unless you want specific abilities to control where and how your objects are laid out.
Hope this helps
Layout Managers
How to use BorderLayout

How do I use nested panels in my GUI application?

I created a drawing application that lets a user choose pen colors but I have having trouble with the layout. I created multiple panels but when I run it, all the buttons are still in one panel. Is there a way to fix this?
public class DrawingGUI extends JPanel {
private JRadioButton penColor1, penColor2, penColor3, randomPenColor, eraser;
private JButton clearButton;
private static Color defaultColor = Color.BLACK;
private static boolean isRandomSelected = false;
private final static int DIAMETER = 12;
protected static boolean canDraw;
private ArrayList<PointTracker> points;
public DrawingGUI() {
setBackground(Color.WHITE);
points = new ArrayList<PointTracker>();
JPanel drawPanel = new JPanel();
JLabel instructions = new JLabel("Enter your information:");
JPanel instructionsPanel = new JPanel();
instructionsPanel.add(instructions);
drawPanel.add(instructionsPanel);
JPanel colorPanel1 = new JPanel();
penColor1 = new JRadioButton("Red");
drawPanel.add(penColor1);
penColor1.addActionListener(new ToolListener());
drawPanel.add(colorPanel1);
JPanel colorPanel2 = new JPanel();
penColor2 = new JRadioButton("Blue");
drawPanel.add(penColor2);
penColor2.addActionListener(new ToolListener());
drawPanel.add(colorPanel2);
JPanel colorPanel3 = new JPanel();
penColor3 = new JRadioButton("Yellow");
drawPanel.add(penColor3);
penColor3.addActionListener(new ToolListener());
drawPanel.add(colorPanel3);...(So on)
all the buttons are still in one panel
Why is that a problem. That is what I would expect to happen.
Why are you creating a separate panel for each button? The whole point of using panel is to logically group components.
So I would expect you should have something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add( button1 );
buttonsPanel.add( button2 );
buttonsPanel.add( button3 );
JPanel drawPanel = new JPanel();
drawPanel.add( component1 );
drawPanel.add( component2 );
frame.add(drawPanel, BorderLayout.PAGE_START);
frame.add(buttonsPanel, BorderLayout.PAGE_END);
Above is a simple example of "nesting" two panels on a frame. Each of the panel can use a different layout manager as required.
For a working example of this approach you can check out Custom Painting Approaches. Both code examples show how you can "nest" a drawing panel and a buttons panel in a frame.

How to align JPanel in java

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();
}
});
}
}

GUI not showing as intended

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);
}
}

Categories