Trying to display JOptionPane on button click - java

I am trying to make a JOptionPane display on button click but I keep getting an error saying I'm missing a return statement in my constructor. Any help would be really appreciated. Thanks
This is my code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class lab9Part1 extends JFrame implements ActionListener {
JButton button = new JButton("Show Message Dialog");
JFrame box = new JFrame();
public lab9Part1() {
super("lab9Part1");
Container c = getContentPane();
button.addActionListener(this);
c.add(button, BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
}
public static Void main (String [] args){
JFrame frame = new lab9Part1();
}
public void actionPerformed(ActionEvent e){
if (e.getSource()== button) {
JOptionPane.showMessageDialog(box,"hello","This is Cal and this is my first message dialog", JOptionPane.INFORMATION_MESSAGE);
}
}
}

You've used Void (uppercase V) instead of void (lowercase v) in your method declaration of main. It should be:
public static void main (String [] args){
JFrame frame = new lab9Part1();
}

Related

Why AWT frame's text field is not appearing?

import java.awt.*;
import java.awt.event.*;
public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
example3()
{
t = new TextField();
t.setBounds(20,20,170,20);
add(t);
b = new Button("click me");
b.setBounds(100,120,80,30);
add(b);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
t.setText("welcome");
}
public static void main(String args[])
{
example3 obj = new example3();
}
}
Initially I added a text field to the the frame object and I created a button also and added this to frame object but I am not getting correct output.
When I executed this program the text field does not appear. Why?
I got output like this:enter image description here
I would like to suggest only one change in line number 11, that increase the x-coordinate and y-coordinate value of setBounds as per your requirement because your code is working but that field is not at a visible location.
as example:-
your code:- t.setBounds(20,20,170,20);
after changing:- t.setBounds(50,50,170,20);
import java.awt.*;
import java.awt.event.*;
public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
example3()
{
t = new TextField();
// t.setBounds(20,20,170,20);
//Changes has been done,here.
t.setBounds(50,50,170,20);
add(t);
b = new Button("click me");
b.setBounds(100,120,80,30);
add(b);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
t.setText("welcome");
}
public static void main(String args[])
{
example3 obj = new example3();
}
}
I hope, it will be helpful.

JButton size is set to default size when visibility is changed

Like I said the JButton GR is set to the default size (size of window) when I click JButton MN.
When the program is started the JButton GR has the right size (200 by 20), when clicked the menu button appears also at the right size (200 by 20), but when the menu button is clicked the GR JButton is at its default size. When the full size GR JButton is clicked the Menu button reappears with the right size.
I'm using BlueJ (school dose not allow other IDEs).
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class MAIN
{
public static void main(String args[])
{
ActionClass actionEvent = new ActionClass();
//Main window
JFrame Program1 = new JFrame("Program1");
Program1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Program1.setPreferredSize(new Dimension(800, 600));
Program1.pack();
Program1.setVisible(true);
//menu button (returns to home Menu)
JButton MN = new JButton("MENU");
MN.setBounds(300, 10, 200, 20);
MN.setVisible(false);
Program1.add (MN);
//MN.setActionCommand("1");
// Enter GRC
JButton GR = new JButton("GRC");
GR.setBounds(300, 40, 200, 20);
GR.setVisible(true);
Program1.add (GR);
//GR.setActionCommand("2");
GR.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent GRH)
{
MN.setVisible(true);
GR.setVisible(false);
}
}
);
MN.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent MNH)
{
MN.setVisible(false);
GR.setVisible(true);
}
}
);
}
}
Like you said the Jbutton GR is ...
JFrame has BorderLayout as default LayoutManager in API,
use Java naming conventions
use LayoutManager instead of NullLayout and wrong way
setVisible(true); should be last code line, because your are in risk that all JComponents are added to already visible container (e.g. mouse over repainting those JComponents)
use Initial Thread
simplest as is possible
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
public class Main {
private JFrame myProgram1 = new JFrame("myProgram1");
private JButton myMN = new JButton("MENU");
private JButton myGR = new JButton("myGRC");
public Main() {
myGR.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent myGRH) {
myMN.setVisible(true);
myGR.setVisible(false);
}
});
myProgram1.add(myMN, BorderLayout.SOUTH);
myMN.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent myMNH) {
myMN.setVisible(false);
myGR.setVisible(true);
}
});
myProgram1.add(myGR, BorderLayout.NORTH);
myProgram1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProgram1.pack();
myProgram1.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}

How to implement the ActionListener to get the Exit Button to work

Here is my code, but the error shows that the implemented ActionListener is not correct. I also declared the buttons so how do I make the system exit? What did I do wrong? Thanks in advance
import javax.swing.*;
import java.awt.*;
import java.awt.FlowLayout;
public class MyFrame extends JFrame implements ActionListener {
public MyFrame() {
// set flow layout for the frame
this.getContentPane().setLayout(new FlowLayout());
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
JButton Find = new JButton("Find");
JButton Clear = new JButton("Clear");
// add buttons to frame
add(ExitBtn);
add(Find);
add(Clear);
}
public void actionPerformed(ActionEvent e){
System.exit(0);
ExitBtn.addActionListener(this);
}
public static void main(String[] args) {
MyFrame mf = new MyFrame();
mf.pack();
mf.setVisible(true);
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
onClick:
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
I think you should move the ExitBtn.addActionListener(this) call to the constructor of MyFrame class so that it looks like this:
JButton ExitBtn = new JButton();
ExitBtn.setText("Exit");
ExitBtn.addActionListener(this)
and actionPermormed method looks like this:
#Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}

Action Event error

Why doesn't my program work? I want to use the borderlayout, and each button to do different thing. I did a lot of research but I am still getting errors, and just am lost.
thank you for your help in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Guard10 {
public static void main(String[] args)
{
new Guard10()
}
public Guard10()
{
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("Show BorderLayout");
myFrame.setSize(300, 200);
myFrame.setLocationRelativeTo(null);
// Add buttons to the frame
JButton labelButton = new JButton ("one");
labelButton.addActionListener(new LabelListener()
{
#Override
public void actionPerformed (ActionEvent event)
{
System.out.println ("You clicked it!");
}
});
////////////
JButton button2 = new JButton ("two");
button2.addActionListener(new Button2Listener()
{
#Override
public void actionPerformed (ActionEvent event)
{
System.out.println ("YAY!");
}
}
myFrame.add(labelButton,BorderLayout.SOUTH);
myFrame.add(button2,BorderLayout.NORTH)
myFrame.setVisible(true);
}
}
You simply forgot some semicolons and brackets. The corrected code below is working. Also, use a standard ActionListener if you do nothing but overriding actionPerformed.
public class Guard10 {
public static void main(String[] args)
{
new Guard10();
}
public Guard10()
{
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("Show BorderLayout");
myFrame.setSize(300, 200);
myFrame.setLocationRelativeTo(null);
// Add buttons to the frame
JButton labelButton = new JButton ("one");
labelButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed (ActionEvent event)
{
System.out.println ("You clicked it!");
}
});
////////////
JButton button2 = new JButton ("two");
button2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed (ActionEvent event)
{
System.out.println ("YAY!");
}
});
myFrame.add(labelButton,BorderLayout.SOUTH);
myFrame.add(button2,BorderLayout.NORTH);
myFrame.setVisible(true);
}
}
If you try to compile you get all the errors in the stacktrace. Just look at the lines to find out what is wrong. I'd also advice you to use an IDE like Eclipse or IntelliJ Idea, they will mark syntax errors before compiling while you type the code.

Make a Panel open when i click a button

does anyone know how to make a GUI button open up a new JPanel in java? its not on google. its to show an about panel. thanks for the help!
I guess JDialog is what you need.
See this for details : How to Make Dialogs
Here is a sample :
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CreateDialogFromOptionPane {
public static void main(final String[] args) {
JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Click me to show dialog!");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane optionPane = new JOptionPane("Is this what you need?", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
JDialog dialog = optionPane.createDialog("Dialog");
dialog.setVisible(true);
}
});
}
}
I think that by implements CardLayout you can solve that
first you would need to create an event handler for your button, then in your handler you should create your panel and make it visible. if you want more of a popup you should use like:
JOptionPane.showMessageDialog(frame, "This is my message");
that will create a warning message, you could also create your own costume dialog i would suggest reading this
You can show panel using an undecorated JDialog
public static void main(String args[])
{
final JDialog bwin = new JDialog();
bwin.addWindowFocusListener(new WindowFocusListener()
{
#Override
public void windowLostFocus(WindowEvent e)
{
bwin.setVisible(false);
bwin.dispose();
}
#Override
public void windowGainedFocus(WindowEvent e)
{
}
});
bwin.setUndecorated(true);
JLabel label = new JLabel("About");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(label);
panel.setPreferredSize(new Dimension(200,200));
bwin.add(panel);
bwin.pack();
bwin.setVisible(true);
}

Categories

Resources