My program complies and displays a blank frame. I have tried multiple ways, but I am thinking this should work, I don't understand why it doesn't?
The code is simple it just shows a blank box and a button that informs the user the action Listener is working.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Udemy {
public Udemy() {
JFrame f = new JFrame();
f.setTitle("La's Frame");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
f.getContentPane().add(panel);
JButton b1 = new JButton("Click me");
panel.add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action Listener is working");
}
});
}
public static void main(String[] args){
Udemy ud = new Udemy();
}
}
Here, I moved f.setVisible(true); to the bottom and everything works fine
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Udemy {
public Udemy(){
JFrame f = new JFrame();
f.setTitle("La's Frame");
f.setSize(400,400);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
f.getContentPane().add(panel);
JButton b1 = new JButton("Click me");
panel.add(b1);
f.setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action Listener is working");
}
});
}
public static void main(String[] args){
Udemy ud = new Udemy();
}
}
Related
I am creating a JFrame holding a JPanel(p1) and a JButton(b1)....p1 contains another JPanel(sub1) holding a JLabel(l1)...after i Click on b1,i want a String to be printed in the JLabel...but it is not printing anything.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class panAccDel implements ActionListener {
JFrame frame = new JFrame();
JButton b1;
JLabel l1;
String intro = "Hello there";
JPanel sub1,p1;
public static void main(String[] args) {
panAccDel panAccDel = new panAccDel();
}
public panAccDel(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
b1 = new JButton("Print Data");
sub1 = new JPanel();
p1.add(sub1);
p1.add(b1);
p1.setLayout(new GridLayout(2,1));
sub1.setBackground(Color.RED);
l1 = new JLabel();
sub1.add(l1);
frame.add(p1);
frame.setVisible(true);
frame.setSize(700,700);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
l1.setText(intro);
}
}
}
This is a trial Code.
Any Suggestions???
The listener needs added to the button. For example:
b1.addActionListener(this);
On button click, I am creating new JFrame, adding a JButton inside it and setting it visible. JFrame is visible but the JButton is not visible.
I tried finding answers on stackoverflow but everyone says to set the JFrame visible after adding the components. I did that also but still, the issue is not solved
below is button code
#Override
public void actionPerformed(ActionEvent arg0) {
popup.showPopup();
}
I have made a class named "popup" with a show popup method.
Below is the code of popup.java class
package justin;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class popupFrame {
private JFrame f = new JFrame("Please wait...");
public void showPopup() {
System.out.println("Showing Popup");
f.setSize(300, 150);
f.setLayout(new FlowLayout());
f.add(new JButton("Test"));
f.setVisible(true);
}
}
It should show the JFrame with items added in it on the click of button.
Please check the below link for my complete code:
https://github.com/jamesfdz/Justin-code
Since I can't see the rest of your code, I have posted an example instead.
public class ControlInterface{
public ControlInterface() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.add(new JButton("Test"));
frame.setVisible(true);
}
}
And the calling class:
public class BusinessLogic{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300, 300);
JButton button = new JButton("Popup");
frame.add(button);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
new ControlInterface();
}
}
});
}
}
I have a problem with JFrame. All I want to do is to create a JFrame for Login with a Button, and when the Button is pressed: it close the Login Frame and opens the Program Frame.
This is my Login Frame:
public static void main(String[] args) {
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LoginPanel primary = new LoginPanel(frame);
frame.setPreferredSize (new Dimension (650, 500));
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
Which opens the Login Panel by passing the Frame in the constructor, the Login Panel:
public class LoginPanel extends JPanel {
JFrame fr;
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
fr.setVisible(false);
fr.dispose();
}
}
public LoginPanel(JFrame frame) {
fr = frame;
JButton submit = new JButton("Button Login");
submit.addActionListener(new submitButton());
add(submit);
}
This is the problem:
When I click on the Button "Button Login" of the LoginPanel, it succesfully opens the new ProgramFrame but it doesn't close at all the old frame (LoginFrame). The LoginFrame becomes smaller, very little, but remains:
Thanks in advance for the help! :)
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
this.dispose(); //changed line
}
}
well your panel is closed bt the jframe is still opened without the jpanel
i have made some changes to your code now both will disposed at the same time
You first initialise JFrame completely, so after inside JButton click event first hide JFrame later dispose it.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HideLoginPage{
public static void main(String[] args){
HideLoginPage loginPage = new HideLoginPage();
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 200, 200, 100);
loginPage.setPane(frame);
frame.setVisible(true);
}
public void setPane(final JFrame frame){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton submit = new JButton("Login");
submit.setSize(100, 30);
panel.add(submit);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JFrame newFrame = new JFrame();
newFrame.setBounds(400, 200, 400, 400);
newFrame.setVisible(true);
frame.setVisible(false);
frame.dispose();
}
});
frame.getContentPane().add(panel);
}
}
I m trying to make a main menu with START button, CONTROLS button and HELP button.
I made a backboard for it but I need help with making options.
I tried to make it, but the error says that local variable gameFrame is accessed from within inner class; needs to be declared final
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame {
public static void main(String[] args) {
JFrame gameFrame = new JFrame("PoopMan");
gameFrame.setSize(900, 800);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setResizable(false);
gameFrame.setVisible(true);
gameFrame.getContentPane().setBackground(Color.yellow);
JPanel panel = new JPanel();
JButton button1 = new JButton();
gameFrame.add(panel);
panel.add(button1);
gameFrame.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(gameFrame.getComponent(0), "START");
}
});
}
}
I believe you would like to initialize your Game in a non-static context. Do the following.
public class Main {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GameFrame();
}
});
}
}
And
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame extends JFrame {
private void init() {
this.setSize(900, 800);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.getContentPane().setBackground(Color.yellow);
JPanel panel = new JPanel();
JButton button1 = new JButton("START");
this.add(panel);
panel.add(button1);
this.setLayout(new FlowLayout());
this.setMinimumSize(new Dimension(300, 300));
this.pack();
this.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(getComponent(0), "START");
}
});
}
public GameFrame() {
super("PoopMan");
init();
}
}
This way, you start the GameFrame on another thread, and you can continue your work within the GameFrame class.
That is quite simple. Please note that this is a place to inquire about issues regarding your code, and not a place to look for answers.
What you should be researching is known as JMenuBar (refer to This Tutorial for Assitance)
Seeing as you are new, I will assist you this once. Below is the code that will accomplish what you ask. (Tried and Tested)
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame
{
static private JMenuBar menuBar;
static private JMenu startMenu, controlsMenu, helpMenu;
static private JMenuItem startBtn;
static private JMenuItem controls;
static private JMenuItem hlpBtn;
public static void main(String[] args)
{
JFrame gameFrame = new JFrame("PoopMan");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.getContentPane().setBackground(Color.yellow);
menuBar = new JMenuBar();
startMenu = new JMenu("File");
controlsMenu = new JMenu("Controls");
helpMenu = new JMenu("Help");
startBtn = new JMenuItem("Start");
controls = new JMenuItem("Controls");
hlpBtn = new JMenuItem("Help");
gameFrame.add(menuBar, BorderLayout.PAGE_START);
menuBar.add(startMenu);
menuBar.add(controlsMenu);
menuBar.add(helpMenu);
startMenu.add(startBtn);
controlsMenu.add(controls);
helpMenu.add(hlpBtn);
gameFrame.pack();
gameFrame.setResizable(false);
gameFrame.setVisible(true);
gameFrame.setSize(900,800);
}
}
I am not certain as to why you are receiving a Final error. It works fine.
From later edits to the question:
JButton button1 = new JButton("Button");
JPanel panel = new JPanel();
panel.setBackground(Color.yellow);
gameFrame.add(panel);
panel.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(gameFrame.getComponent(0), "START");
}
});
I really can't figure out why this JPanel "p" isn't appearing?
I thought I coded it right for the JPanel p to be in the middle of the Jframe and should make the whole JFrame RED but it doesn't seem to do that and the buttons and JPanel aren't appearing. Sorry. I know I am probably stupid but please help. :?
Here is the code.
package com.gorillalogic.henry;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Notepad {
private JFrame f; // creates all GUI components
private JPanel p;
private JButton b1;
public Notepad() {
gui();
}
public void gui() {
f = new JFrame("Notepad");
p = new JPanel();
b1 = new JButton("Quit");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setSize(600, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
p.setBackground(Color.RED);
p.add(b1);
f.add(p, BorderLayout.CENTER);
}
public static void main(String[] args) {
new Notepad();
}
}
Thanks in advance. :)
p.setOpaque(true);
You need to do that.