Creating a class and Going into the Frame - java

I am trying to create this Panel into a class but it is not working, trying to make it go into the Frame as well. I am getting the "It is not a class error"
Please explain to me what I am doing wrong. Programming is fun until you are stuck for hours on one problem.
Panel:
import java.awt.Button;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TopPanel extends JPanel {
public TopPanel{
JPanel panel = new JPanel();
JFrame frame = new JFrame("Create a frame");
frame.getContentPane().add(panel);
Button button = new Button("111");
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
Frame:
import javax.swing.*;
import java.awt.*;
public class CourseGUI extends JFrame {
public CourseGUI()
{
super("CourseGUI Frame");
JPanel topPanel = new JPanel();
topPanel.setBackground(java.awt.Color.WHITE);
Dimension d = new Dimension(800,600);
topPanel.setPreferredSize(d);
this.setLayout(new BorderLayout());
this.add(topPanel, BorderLayout.NORTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
this.setVisible(true);
}
public static void main(String[] args)
{
new CourseGUI();
}
}
Thanks in advanced.
I changed the TopPanel:
import javax.swing.*;
import java.awt.*;
public class TopPanel extends JPanel {
public TopPanel(){
JPanel panel = new JPanel();
JLabel Crse = new JLabel("Course Info");
Crse.setFont(new Font("Serif", Font.PLAIN, 14));
panel.add(Crse);
panel.add(button);
}
}

TopPanel is your class name, topPanel is your JPanel instance. (Java is case sensitive).
Lines like
TopPanel.setLayout(new BorderLayout());
TopPanel.add(Crse, BorderLayout.NORTH);
Are trying to use the class which is not what you intended...
Your are also missing () on the line public TopPanel { (the one inside the class, not the one defining the class)
Crse is a local variable in the TopPanel creator, so you can't use it inside CourseGUI()
TopPanel is creating a frame to put itself into which is weird...

Related

Java Swing: manage multiple SpringLayout

I would like to manage a complex structure of containers as a combination of SpringLayouts (a series of stand-alone SpringLayout one inside other)
In particular, a JFrame managed by a main SpringLayout that contains JPanels: every JPanel should be managed by its own SpringLayout.
Unfortunalely, if i try to apply two different SpringLayout, nothing appears on the frame
I made this simple code to show you the problem. The code contains:
A JFrame and its SprilgLayout
A JPanel and its SpringLayout
a simple JButton inside the JPanel
Here the code:
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
public class SpringLayout_cobination {
// THE OBJECTS
static JFrame frame;// The frame
static JPanel panel; // The container
static JButton Button; // Object
// THE LayOuts
static SpringLayout LayOut_Frame;
static SpringLayout LayOut_panel;
static Container contentPane;
public static void main(String[] args) {
// Start frame and componets
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(830, 420);
panel=new JPanel();
Button=new JButton("ok");
panel.add(Button);
panel.setSize(new Dimension(100,100));
frame.add(panel);
// Set the layouts
LayOut_Frame=new SpringLayout(); // Frame LayOut
frame.setLayout(LayOut_Frame);
contentPane=frame.getContentPane(); // Main Container of the frame
LayOut_panel=new SpringLayout();
panel.setLayout(LayOut_panel);
LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);
LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);
frame.setVisible(true);
}
}
apply two different SpringLayout
If I understand your requirement, I think it is possible to combine SpringLayouts as below:
import java.awt.*;
import javax.swing.*;
public class SpringLayoutCobinationTest {
private static Component makeSubPanel() {
JButton button = new JButton("ok");
SpringLayout layout = new SpringLayout();
JPanel pnl = new JPanel(layout);
layout.putConstraint(SpringLayout.NORTH, button, 10, SpringLayout.NORTH, pnl);
layout.putConstraint(SpringLayout.WEST, button, 10, SpringLayout.WEST, pnl);
pnl.add(button);
pnl.setBorder(BorderFactory.createLineBorder(Color.RED));
return pnl;
}
public Component makeUI() {
Component sub = makeSubPanel();
SpringLayout layout = new SpringLayout();
SpringLayout.Constraints c = layout.getConstraints(sub);
c.setWidth(Spring.constant(100));
c.setHeight(Spring.constant(100));
JPanel panel = new JPanel(layout);
layout.putConstraint(SpringLayout.NORTH, sub, 30, SpringLayout.NORTH, panel);
layout.putConstraint(SpringLayout.WEST, sub, 30, SpringLayout.WEST, panel);
panel.add(sub);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(new SpringLayoutCobinationTest().makeUI());
f.setSize(420, 420);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
The part of code i forgot was:
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
It seems that you must impose a Constraints dimension every time you import a component in the main frame
in my example, when I import the
The final code is:
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
public class SpringLayout_cobination {
// THE OBJECTS
static JFrame frame;// The frame
static JPanel panel; // The container
static JButton Button; // Object
// THE LayOuts
static SpringLayout LayOut_Frame;
static SpringLayout LayOut_panel;
static Container contentPane;
public static void main(String[] args) {
// Start frame and componets
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(830, 420);
panel=new JPanel();
Button=new JButton("ok");
panel.add(Button);
panel.setSize(new Dimension(100,100));
frame.add(panel);
// Set the layouts
LayOut_Frame=new SpringLayout(); // Frame LayOut
frame.setLayout(LayOut_Frame);
contentPane=frame.getContentPane(); // Main Container of the frame
LayOut_panel=new SpringLayout();
panel.setLayout(LayOut_panel);
LayOut_panel.putConstraint(SpringLayout.NORTH, contentPane,30,SpringLayout.NORTH, Button);
LayOut_panel.putConstraint(SpringLayout.WEST, contentPane,30,SpringLayout.WEST , Button);
SpringLayout.Constraints c = LayOut_Frame.getConstraints(panel);
c.setWidth(Spring.constant(200));
c.setHeight(Spring.constant(100));
LayOut_Frame.putConstraint(SpringLayout.NORTH, panel,30,SpringLayout.NORTH, contentPane);
LayOut_Frame.putConstraint(SpringLayout.WEST, panel,30,SpringLayout.WEST , contentPane);
frame.setVisible(true);
}
}

How to constantly update a JPanel

I am trying to make a program where I have two JPanels inside a JFrame, one of which contains a canvas. I am trying to find a way to get that canvas to be constantly updating so that I could create something like a game inside the canvas. I was wondering how I could make it so that the JPanel with the canvas in it is constantly refreshing so that you can see when something is changed in the canvas. Here is my code:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main
{
private JFrame frame;
private JPanel mainPanel;
private JPanel gamePanel;
private JPanel sidePanel;
public void setup()
{
frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocation(100, 50);
mainPanel = new JPanel();
Dimension d = new Dimension(800, 600);
mainPanel.setMaximumSize(d);
mainPanel.setMinimumSize(d);
mainPanel.setPreferredSize(d);
frame.add(mainPanel);
frame.pack();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
}
public void setupPanels()
{
gamePanel = new JPanel();
Dimension d = new Dimension(600, 600);
gamePanel.setMaximumSize(d);
gamePanel.setMinimumSize(d);
gamePanel.setPreferredSize(d);
gamePanel.setBackground(Color.RED);
mainPanel.add(gamePanel);
sidePanel = new JPanel();
d = new Dimension(600, 600);
sidePanel.setMaximumSize(d);
sidePanel.setMinimumSize(d);
sidePanel.setPreferredSize(d);
sidePanel.setBackground(Color.BLUE);
mainPanel.add(sidePanel);
}
public void setupGame()
{
GameArea game = new GameArea();
gamePanel.add(game);
game.start();
}
public static void main(String[] args)
{
Main main = new Main();
main.setup();
main.setupPanels();
main.setupGame();
}

Java Swing CardLayout multiple files

I am trying to get this code to work, such that I have a cardlayout container and each panel be defined in its own class and actual file. This code is not 100% my own and is a modified version of my previous stuff by another Stack overflow user. It is more or less what I need, but I need it such that it isn't automated and I can write 15 different panels with decisions made inside each one. The Main and Arrow class was modified by said user, and Imagepanel is my attempt to write a class that will be accepted by the working part of the code. The issue is the Imagepanel I insert into the container will register as existing, but nothing shows up on the panel, it's blank. The commented out portion in ImagePanel is my code that I set on the back burner in favor of the established stuff previously used in the Arrow class.
Here is the Main class
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;
public class Main extends JPanel {
private Arrow arrow = new Arrow(); //creates a new Arrow object
public Main() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new NextAction("Next")));
setLayout(new BorderLayout());
add(arrow, BorderLayout.NORTH);
add(btnPanel, BorderLayout.PAGE_END);
}
private class NextAction extends AbstractAction {
public NextAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
arrow.next(); // *** call arrow's public next method that you created
// no need to make a new CardLayout instance
}
}
private static void createAndShowGui() {
Main mainPanel = new Main();
JFrame frame = new JFrame("Iowa Budget Simulation");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Here is the Arrow class where the container is created
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.*;
class Arrow extends JPanel {
private static final long serialVersionUID = 1L;
private CardLayout cardLayout = new CardLayout(); // make me a field
private JPanel cardHolder = new JPanel(cardLayout); //creates a master JPanel
public Arrow() {
for (int i = 0; i < 5; i++) {
cardHolder.add(createCard(i), "card " + i);
}
ImagePanel pear = new ImagePanel();
cardHolder.add(pear, "Pear");
setLayout(new BorderLayout());
add(cardHolder, BorderLayout.NORTH);
}
// public method that other objects can call
public void next() {
cardLayout.next(cardHolder); // call next on the correct object
}
// simply creates a "pretty" new JPanel
private JComponent createCard(int i) {
JLabel label = new JLabel("Card " + i);
label.setFont(label.getFont().deriveFont(Font.BOLD, 50f));
float h = (float)Math.random();
Color c = Color.getHSBColor(h, 1f, 1f);
label.setForeground(c.darker());
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label);
panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
panel.setBackground(c.brighter().brighter());
panel.setPreferredSize(new Dimension(400, 300));
return panel;
}
Here is ImagePanel, my attempt at a 3rd, individual panel/class
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.*;
class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private String imgString;
private JLabel imgLabel;
public JComponent ImagePanel() {
/*
setName("Pear");
JLabel john = new JLabel("Pear");
float h = (float)Math.random();
Color c = Color.getHSBColor(h, 1f, 1f);
john.setForeground(c.darker());
JPanel panel = new JPanel(new GridBagLayout());
panel.add(john);
panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
panel.setBackground(c.brighter().brighter());
// Ensure size is correct even before any image is loaded.
setPreferredSize(new Dimension(400, 300));
return panel;
*/
JLabel label = new JLabel("Pear");
label.setFont(label.getFont().deriveFont(Font.BOLD, 50f));
float h = (float)Math.random();
Color c = Color.getHSBColor(h, 1f, 1f);
label.setForeground(c.darker());
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label);
panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
panel.setBackground(c.brighter().brighter());
panel.setPreferredSize(new Dimension(400, 300));
return panel;
}
There is no error to post, it simply displays a blank panel. Thank you for any assistance I might receive, and I apologize in advance as I am learning Java Swing GUI through YouTube and stack overflow.
public JComponent ImagePanel() { isn't a constructor, it's a method, to make it work in your code you would have to change ImagePanel pear = new ImagePanel(); to JComponent pear = new ImagePanel().ImagePanel();, but frankly that just doesn't make much sense.
Instead, change public JComponent ImagePanel() { to public ImagePanel() {, now it's the class's constructor
Next, change...
JPanel panel = new JPanel(new GridBagLayout());
panel.add(label);
panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
panel.setBackground(c.brighter().brighter());
panel.setPreferredSize(new Dimension(400, 300));
return panel;
to
setLayout(new GridBagLayout());
add(label);
setBorder(BorderFactory.createLineBorder(c.darker(), 20));
setBackground(c.brighter().brighter());
//panel.setPreferredSize(new Dimension(400, 300));
//return panel;
Don't get me started on why setPreferredSize is a bad idea
Now you can simply use
ImagePanel pear = new ImagePanel();
cardHolder.add(pear, "Pear");

How to put JOutlookBar into a JPanel in Java

How can I put a JOutlookBar into a JPanel?
Here is my code:
JFrame frame = new JFrame("JOutlookBar Test");
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel2("Two"));
outlookBar.addBar("Three", getDummyPanel3("Three"));
outlookBar.addBar("Four", getDummyPanel4("Four"));
outlookBar.addBar("Five", getDummyPanel5("Five"));
outlookBar.setVisibleBar(0);
frame.getContentPane().add(outlookBar);
frame.setSize(800, 600);
Adding frame.setVisible(true); allowed me to view your code working correctly.
Below is full working example of the code you provided, copy and paste it into a file named Library.java and it'll be grand!
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Library {
public static void main(String[] args) {
JFrame frame = new JFrame("JOutlookBar Test");
JPanel left = new JPanel();
JPanel center = new JPanel();
JOutlookBar outlookBar = new JOutlookBar();
outlookBar.addBar("One", getDummyPanel1("one"));
outlookBar.addBar("Two", getDummyPanel1("Two"));
outlookBar.addBar("Three", getDummyPanel1("Three"));
outlookBar.addBar("Four", getDummyPanel1("Four"));
outlookBar.addBar("Five", getDummyPanel1("Five"));
outlookBar.setVisibleBar(0);
//Add outlookbar to left panel
left.add(outlookBar);
//Add some content to center panel...
center.add(new JTextArea());
//Add left and center panels with layout styles
frame.setLayout(new BorderLayout());
frame.add(left, BorderLayout.WEST);
frame.add(center, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Show JFrame
frame.setVisible(true);
}
public static JPanel getDummyPanel1(String num) {
JPanel panel = new JPanel();
panel.add(new JButton("num"));
return panel;
}
}

Toggling/navigating jpanels in java swings

> I have a jframe defined in a package 'abc'. this jframe acts as a main class too.
> the same package 'abc' also contain 4 jpanels(panel1,panel2,panel3,panel4) defined in different java classes.
how one should call/show these different jpanels from main jframe class at user clicks on different buttons ?
what if I've got 40-50 such jpanels.what would be the most efficient way to toggle these panels from the main class ?
This is a very simple example that uses next() and one button to change panels.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.Border;
public class TestCards extends JFrame {
private final CardLayout cl = new CardLayout();
private final JPanel cards = new JPanel(cl);
private final Border border = BorderFactory.createEmptyBorder(60, 60, 60, 60);
public TestCards() {
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JPanel panel1 = new JPanel(new GridBagLayout());
panel1.setBorder(border);
panel1.setBackground(Color.RED);
panel1.add(new JLabel("Card 1"));
cards.add(panel1, "First Panel");
JPanel panel2 = new JPanel(new GridBagLayout());
panel2.setBorder(border);
panel2.setBackground(Color.GREEN);
panel2.add(new JLabel("Card 2"));
cards.add(panel2, "Second Panel");
JPanel panel3 = new JPanel(new GridBagLayout());
panel3.setBorder(border);
panel3.setBackground(Color.BLUE);
panel3.add(new JLabel("Card 3"));
cards.add(panel3, "Third Panel");
JButton controlButton = new JButton("Switch");
controlButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cl.next(cards);
}
});
JPanel controlPanel = new JPanel();
controlPanel.setBackground(Color.LIGHT_GRAY);
controlPanel.add(controlButton);
contentPane.add(cards);
contentPane.add(controlPanel);
cl.show(cards, "First Panel");
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
TestCards frame = new TestCards();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
An alternative (among many) would be to use 3 buttons where each one shows a specific panel (by using show instead of next).
See this for more info.
CardLayout's previous and next methods can be used to navigate forward & back respectively through the card components. To make it easier to navigate the components non-consecutively, you could place the card names in a String array:
private static final String[] CARD_NAMES = { "name_2996062106101", ... };
Then, to display a specific cardIndex, you could simply use:
CardLayout layout = (CardLayout) contentPane.getLayout();
layout.show(contentPane, CARD_NAMES[cardIndex]);
For more see How to Use CardLayout.

Categories