Action Event error - java

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.

Related

Trying to display JOptionPane on button click

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

What am I doing wrong with this Java JPanel/JFrame/JButton?

What am I doing wrong with this Java JPanel/JFrame/JButton? This is my program and right now I've been trying to be able to repaint it, but it won't work. Please help! I am a beginner with JPanel and JFrame. I'm only adding this sentence to allow it to post.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Clicks implements MouseListener
{
private static int clicks, clickersInt, clickersCost;
private static JButton clickersB, click;
private static JLabel clickersL, clicksL;
private static JPanel panel;
public static void main(String[] args)
{
JFrame clicksFrame=new JFrame ("Clicks");
clicksFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clicks=0;
panel=new JPanel(new GridBagLayout());
click=new JButton("Click Me!");
clickersB=new JButton("Cost: 10");
clickersL=new JLabel("Clickers: "+Integer.toString(clickersInt));
clicksL=new JLabel("Clicks: "+Integer.toString(clicks));
click.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clicks++;
clicksFrame.remove(panel);
clicksFrame.add(panel);
panel.validate();
panel.repaint();
}
});
clickersB.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(clicks>=clickersCost)
{
clickersInt++;
clicks-=clickersCost;
clickersCost*=2;
}
}
});
panel.add(click);
panel.add(clicksL);
panel.add(clickersL);
panel.add(clickersB);
clicksFrame.add(panel);
clicksFrame.setBackground(Color.CYAN);
clicksFrame.setVisible(true);
}
}
So, based on this little snippet...
click=new JButton("Click Me!");
clickersB=new JButton("Cost: 10");
clickersL=new JLabel("Clickers: "+Integer.toString(clickersInt));
clicksL=new JLabel("Clicks: "+Integer.toString(clicks));
click.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clicks++;
clicksFrame.remove(panel);
clicksFrame.add(panel);
panel.validate();
panel.repaint();
}
});
I would suggest that what you really want to do is...
click.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clicks++;
clicksL.setText("Clicks: "+Integer.toString(clicks));
}
});
You seem to be of the illusion that changing a variable will some how change another variable which is just a string of characters. As you've discovered, that's not how this works

Two labels overlap each other when using the Radio Button Listener

I made a simple program where there are two radio buttons each with an action listener.
After pressing first button, a label is printed and the same thing happens with the other.
The problem is that both the labels overlap after pressing first and second button.
Edit-The previous label must be removed and then new label must be on the screen.
ex-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ckbxdm{
JFrame frame;
JRadioButton r1,r2;
ButtonGroup grp;
JLabel l1,l2;
void box(){
frame=new JFrame("Hello");
r1=new JRadioButton("Login");
r2=new JRadioButton("Signup");
grp=new ButtonGroup();
grp.add(r1);
grp.add(r2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().add(r1);
frame.getContentPane().add(r2);
r1.setBounds(100,120,100,20);
r2.setBounds(200,120,100,20);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
r1.addActionListener(new listener1());
r2.addActionListener(new listener2());
}
class listener1 implements ActionListener{
public void actionPerformed(ActionEvent ae){
frame.getContentPane().repaint();
frame.getContentPane().revalidate();
l1=new JLabel("Login area");
frame.getContentPane().add(l1);
l1.setBounds(100,200,100,20);
}
}
class listener2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
frame.getContentPane().repaint();
frame.getContentPane().revalidate();
l2=new JLabel("Signup area");
frame.getContentPane().add(l2);
l2.setBounds(100,200,100,20);
}
}
public void itemStateChanged(ItemEvent ie){
frame.repaint();
}
}
public class CheckboxDemo{
public static void main(String args[]){
ckbxdm obj=new ckbxdm();
obj.box();
}
}
In addition to the answer provided above, if you want to hide the other label, you can set its visibility to false and repaint the parent component. You can learn more from here. Note that calling the revalidate method here is unnecessary since you are not removing any component from the hierarchy.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ckbxdm{
JFrame frame;
JRadioButton r1,r2;
ButtonGroup grp;
JLabel l1,l2;
void box(){
frame=new JFrame("Hello");
r1=new JRadioButton("Login");
r2=new JRadioButton("Signup");
grp=new ButtonGroup();
grp.add(r1);
grp.add(r2);
l1=new JLabel("Login area");
l1.setBounds(100,200,100,20);
l1.setVisible(false);
frame.getContentPane().add(l1);
l2=new JLabel("Signup area");
l2.setBounds(100,200,100,20);
l2.setVisible(false);
frame.getContentPane().add(l2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.getContentPane().add(r1);
frame.getContentPane().add(r2);
r1.setBounds(100,120,100,20);
r2.setBounds(200,120,100,20);
frame.setBounds(100,100,500,500);
frame.setVisible(true);
r1.addActionListener(new listener1());
r2.addActionListener(new listener2());
}
class listener1 implements ActionListener{
public void actionPerformed(ActionEvent ae){
l2.setVisible(false);
l1.setVisible(true);
frame.getContentPane().repaint();
}
}
class listener2 implements ActionListener{
public void actionPerformed(ActionEvent ae){
l1.setVisible(false);
l2.setVisible(true);
frame.getContentPane().repaint();
}
}
public void itemStateChanged(ItemEvent ie){
frame.repaint();
}
}
public class CheckboxDemo{
public static void main(String args[]){
ckbxdm obj=new ckbxdm();
obj.box();
}
}
try to change ur "sign up position" in X-Ycooridantes direction, let say change following line of code l2.setBounds(100,200,100,20);
to
l2.setBounds(200,200,100,20);
and the same way for l1 change it to l1.setBounds(50,200,100,20);
, it will definitely work

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

JComboBox getSelectedIndex() method

I was making a simple text editor where you can set font style,font size, clear all etc. To set font size I added JComboBox and implemented ItemListener. Here is my MainWindow class:
import javax.swing.*;
public class MainWindow extends JFrame{
Editor e = new Editor();
public MainWindow(){
super(".:My Text Editor:.");
getContentPane().add(e);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MainWindow();
}
});
}
}
Here is my Editor class:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Editor extends JPanel{
JPanel optionPanel = new JPanel();
JTextArea editArea = new JTextArea();
JButton boldBtn = new JButton("Bold");
JButton italicBtn = new JButton("Italic");
JButton plainBtn = new JButton("Plain");
JButton clearBtn = new JButton("Clear all");
String [] fontSizes = {"10","11","12","13","14","15","16","17","18","19","20"};
int fontSize;
JComboBox combo = new JComboBox(fontSizes);
public Editor(){
createUI();
addEvents();
}
public void createUI(){
optionPanel.add(boldBtn);
optionPanel.add(italicBtn);
optionPanel.add(plainBtn);
optionPanel.add(combo);
optionPanel.add(clearBtn);
setLayout(new BorderLayout());
add(optionPanel,BorderLayout.NORTH);
add(new JScrollPane(editArea),BorderLayout.CENTER);
setPreferredSize(new Dimension(640,480));
}
public void addEvents(){
boldBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.BOLD,fontSize));
}
});
italicBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.ITALIC,fontSize));
}
});
plainBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
int ind = combo.getSelectedIndex();
System.out.println(ind);
fontSize = Integer.parseInt(fontSizes[ind]);
editArea.setFont(new Font("Sans Serif",Font.PLAIN,fontSize));
}
});
clearBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
editArea.setText("");
}
});
}
}
Now, weird thing what happened is when I put System.out.println(ind); line just to see what index the getSelectedIndex() method returns me. Depending on which item I click, it returns me this:
1
1
0
0
2
2
3
3
Why is this happening? Shouldn't return me just 1 0 2 3? Thanks in advance.
JCombobox fire itemStateChanged twice for SELECTED and DESELECTED that you differentiate with ItemEvent.getStateChanged(). So wrap your code in an if like this:
public void itemStateChanged( ItemEvent event ) {
if( event.getStateChanged() == ItemEvent.SELECTED ) {
// code here
}
}
Whenever you change the selection in a JComboBox, the itemStateChanged event is triggered twice, once for DESELECT of the old selected item and once for SELECT for the new selected item.
If you only want your code to be executed once, just do:
if (e.getStateChange() == ItemEvent.SELECTED) {
...
}
Seems that itemStateChanged is triggered twice. I think that the ItemEvent parameter is not the same each time. Perhaps you should check the event type before doing something.
Sorry I can't check now, but I will do it later if you still need help.

Categories

Resources