How do i add the JPanel to JFrame? It is really confusing me. I want to add the JPanel to the JFrame. I've tried all sorts of things including the extend but I cant get it to work.
events
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
mainJFrame.setLayout(BorderLayout());
mainJFrame.setVisible(true);
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainJFrame.setSize(600,400);
mainJFrame.setVisible(true);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
}
}
Time
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Time{
public static void selectButton()
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
}
Have a look over this source. Note the comments.
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class events {
public static void main (String args[]) {
Time timeObject = new Time();
JFrame mainJFrame;
mainJFrame = new JFrame();
// Coding by magic!
//mainJFrame.setLayout(BorderLayout());
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// don't do this, just call pack() later
//mainJFrame.setSize(600,400);
mainJFrame.setLayout(new BorderLayout());
mainJFrame.setTitle("Travel Agent System");
mainJFrame.setBackground(Color.BLUE);
timeObject.selectButton();
mainJFrame.add(timeObject.getGUI());
mainJFrame.pack();
// should be last.
mainJFrame.setVisible(true);
}
}
class Time {
private JPanel buttonPanel;
// don't use static unless necessary - it is not necessary.
//public static void selectButton() {
public void selectButton() {
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton);
buttonPanel.add(moneyButton);
buttonPanel.add(hotelButton);
buttonPanel.add(exitButton);
}
public JComponent getGUI() {
return buttonPanel;
}
}
Pass JFrame object to selectButton() :
timeObject.selectButton(mainJFrame);
Then use that JFrame object to add JPanel to it.
public static void selectButton(JFrame frame)
{
JButton timeButton = new JButton("Time");
JButton moneyButton = new JButton("Money");
JButton hotelButton = new JButton("Hotel");
JButton exitButton = new JButton("Exit");
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.RED);
buttonPanel.add(timeButton,moneyButtons,hotelButton,exitButton);
frame.getContentPane().add(buttonPanel,BorderLayout.CENTER); // i've added to CENTER.
}
This will add JPanel to CENTER of your JFrame.
Here is a good example of implementing a JFrame class and then adding JPanels to the JFrame and to other JPanels.
Link to another StackOverflow question/answer
Related
I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}
I don't know how to resolve this case:
I have a JFrame with JPanel on it. I added two JButtons to this JPanel.
Class MainFrame
import java.awt.Color;
import javax.swing.JFrame;
public class MainFrame extends JFrame{
public MainFrame(){
this.setSize(100,100);
MainPanel panel = new MainPanel();
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
and MainPanel with two buttons
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements ActionListener{
JButton button, example;
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New");
button.addActionListener(this);
JButton example = new JButton("example");
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}
}
and start class Main
public class Main {
public static void main (String[] args){
MainFrame frame = new MainFrame();
}
}
What should I do to change background color second button?
You have your button variables defined twice, once as an instance variable and once as a local variable.
Get rid of the local variable:
//JButton example = new JButton("example");
example = new JButton("example");
Now your ActionListener code can reference the instance variable.
In your example:
JButton button, example; // <-- Here, you create your two (protected) variables
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New"); // <-- And here, you create a local variable
button.addActionListener(this);
JButton example = new JButton("example"); // <-- Here, another local variable
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}
I am a beginner here and I am trying to make a GUI that will display two buttons, addCard and deleteCard. However, the compiler is showing errors, and I cannot seem to find the error. Thank you in advance!
package studyfast;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Scratch{
JFrame projectFrame = new JFrame();
projectFrame.setSize(1000, 600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panelEditCard = new JPanel();
JButton addCard = new JButton();
JButton deleteCard = new JButton();
panelEditCard.add(addCard);
panelEditCard.add(deleteCard);
projectFrame.add(panelEditCard);
projectFrame.setVisible(true);
public static void main(String[] args){
new Scratch();
}
}
You need to move your code inside the Scratch class inside a constructor
package studyfast;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Scratch
{
JFrame projectFrame = new JFrame();
JPanel panelEditCard = new JPanel();
JButton addCard = new JButton();
JButton deleteCard = new JButton();
public Scratch()
{
projectFrame.setSize(1000, 600);
projectFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panelEditCard.add(addCard);
panelEditCard.add(deleteCard);
projectFrame.add(panelEditCard);
projectFrame.setVisible(true);
}
public static void main(String[] args)
{
new Scratch();
}
}
I am trying to create a Java GUI application that contains a label and button. When the button is clicked the background color of the first panel is changed. I've got the label and button but getting errors whenever I click the button. Also, I wanted the first panel to originally have a yellow background then switch to whatever color. Here's my code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class ChangeDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel;
public static void main(String[] args)
{
ChangeDemo gui = new ChangeDemo();
gui.setVisible(true);
}
public ChangeDemo()
{
super ("ChangeBackgroundDemo");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new BorderLayout());
biggerPanel.setBackground(Color.YELLOW);
JLabel namePanel = new JLabel("Click the button to change the background color");
biggerPanel.add(namePanel, BorderLayout.NORTH);
add(namePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(this);
buttonPanel.add(changeButton);
add(buttonPanel);
}
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if(buttonString.equals("Change Color"))
biggerPanel.setBackground(Color.RED);
else
System.out.println("Unexpected Error!");
}
}
I made a few changes to your code.
First, you must start a Swing application with a call to SwingUtilities.invokeLater.
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
Second, you use Swing components. You only extend a Swing component when you want to override a method of the Swing component.
Third, I made a action listener specifically for your JButton. That way, you don't have to check for a particular JButton string. You can create as many action listeners as you need for your GUI.
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
Finally, I changed the background color of the JButton panel.
Here's the entire ChangeDemo class.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ChangeDemo implements Runnable {
private boolean isYellow;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
#Override
public void run() {
frame = new JFrame("Change Background Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel(
"Click the button to change the background color");
nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
namePanel.add(nameLabel);
mainPanel.add(namePanel);
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.YELLOW);
isYellow = true;
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Here is working demo based on amendments to your code, haven't had time to tidy it up but hopefully you'll get the gist of it. Problem was you hand't added Panels to the borders (north, south etc.) in order to color them. Hopefully this helps.
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class ChangeDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT= 200;
private JPanel biggerPanel = new JPanel();
private JPanel namePanel = new JPanel();
public static void main(String[] args)
{
ChangeDemo gui = new ChangeDemo();
gui.setVisible(true);
}
public ChangeDemo()
{
super ("ChangeBackgroundDemo");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,3));
//JPanel biggerPanel = new JPanel();
this.biggerPanel.setLayout(new BorderLayout());
this.biggerPanel.setBackground(Color.YELLOW);
JLabel nameLabel = new JLabel("Click the button to change the background color");
namePanel.add(nameLabel);
namePanel.setBackground(Color.YELLOW);
//this.biggerPanel.add(namePanel, BorderLayout.NORTH);
add(namePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(this);
changeButton.setActionCommand("Change Color");
buttonPanel.add(changeButton);
add(buttonPanel);
}
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if(buttonString.equals("Change Color"))
this.namePanel.setBackground(Color.RED);
else
System.out.println("Unexpected Error!");
}
}
A while back I started a project that soon built up a shed load of code, most of the code was made up of components and their properties. All was going well until I hit an error. Off the top of head, the error was something on the line of exceeding the code limit of a constructor, roughly 65000 bytes.
This error has literally bought me and my project to halt. At the same time I have also found major problems in my understanding of SWING.
What I tried was to break my game code into logical sections, putting each section into a different class. For example, a jpanel which dealt with buying and selling would be its own .java file. Another jpanel that dealt with shipping would be in another .java file.
What I hoped to achieve was a JFrame that called each of these jpanels when the user requested it at the press of a jbutton. However, this didn't quite work as I wished, putting me in a position where I need help.
What I have done is simplified my problem by creating an example framework, hoping that somebody could point out what I need to be looking at, possibly even a solution.
I have created a JFrame which holds a panel called bg, which itself holds 2 JButtons (btn1 and btn2). In a different class file I have created a JPanel called panel1, and in another class again I have created another JPanel called panel2.
When the user opens the application they are presented with a frame and the option of two buttons, when any of these buttons are pressed I would like for either panel1 or
panel2 to open. How would this be done?
Any help would be greatly appreciated. Thanks in advance.
////////////// frame
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
}
if (a.getSource() == btn2){
}
}
}
////////////////////// panel1
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel1 implements ActionListener {
public JButton btn3;
public panel1(){
JPanel a = new JPanel();
a.setSize(280, 110);
a.setLocation(155, 10);
a.setBackground(Color.red);
a.setVisible(true);
btn3 = new JButton("open bb");
btn3.setSize(100, 30);
btn3.setLocation(10, 10);
btn3.addActionListener(this);
a.add(btn3);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn3){
}
}
}
//////////////////////////// panel2.java
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class panel2 implements ActionListener {
public JButton btn4;
public panel2(){
JPanel b = new JPanel();
b.setSize(280, 110);
b.setLocation(155, 10);
b.setBackground(Color.blue);
b.setVisible(true);
btn4 = new JButton("open");
btn4.setSize(100, 30);
btn4.setLocation(10, 10);
btn4.addActionListener(this);
b.add(btn4);
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn4){
}
}
}
You don't need to split your panels into different classes for something this simple. Try keeping everything together:
package panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame implements ActionListener {
public JPanel bg,panel1,panel2;
public static JButton btn1, btn2;
public Frame(){
JFrame f = new JFrame();
f.setSize(308, 205);
f.setLayout(null);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
bg = new JPanel();
bg.setSize(300, 200);
bg.setLocation(0, 0);
bg.setLayout(null);
bg.setBackground(Color.black);
bg.setVisible(true);
btn1 = new JButton("open 1");
btn1.setSize(135, 30);
btn1.setLocation(10, 10);
btn1.addActionListener(this);
btn2 = new JButton("open 2");
btn2.setSize(135, 30);
btn2.setLocation(155, 10);
btn2.addActionListener(this);
bg.add(btn1);
bg.add(btn2);
f.add(bg);
panel1 = new JPanel();
panel1.setSize(280, 110);
panel1.setLocation(155, 10);
panel1.setBackground(Color.red);
panel1.setVisible(false);
bg.add(panel1);
panel2 = new JPanel();
panel2.setSize(280, 110);
panel2.setLocation(155, 10);
panel2.setBackground(Color.blue);
panel2.setVisible(false);
bg.add(panel2);
}
public static void main(String[] args) {
new Frame();
}
#Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == btn1){
panel1.setVisible(true);panel2.setVisible(false);
}
if (a.getSource() == btn2){
panel1.setVisible(false);panel2.setVisible(true);
}
}
}