简体   繁体   中英

Need to add a “public static void main(String[] args)”,

I'm trying to make a code in Eclipse that change the color and text on MyButton when clicked.

Edit: Got some help and the problem Error got solved. I now know that the code need "public static void main(String[] args)" where should that be put?

After I try to run this code I get the Error "The selection can't be launched, and there are no recent launches". However, I've googled it and saw that the error may be a common problem in eclipse. I am, although new to java and presume it would be more logical if there's wrong with my code rather than eclipse hate me.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

    public class Makke extends JFrame implements ActionListener{


        private JButton MyButton;

        public Makke(){
            setLayout(null);
            setSize(300, 250);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyButton = new JButton("Tryck För Blå!");
            MyButton.setBackground(Color.YELLOW);
            MyButton.setBounds(100, 190, 60, 30);
            MyButton.addActionListener(this);
            add (MyButton);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == MyButton) {
                MyButton = new JButton("Tryck För Gul!");
                MyButton.setBackground(Color.BLUE);

            }

        }
        }

Translation: There is some Swedish in the code "Tryck för blå" = "Click to get blue", "Tryck för gul" = "Click to get yellow" ^^

You are creating a new button everytime you click on it, but no clicklistener is added to that new object... My recomendation is to do this (or maybe better if you move to javaFX)

JButton myButton = new JButton("Text Button");
myButton.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
      myButton.setText("Tryck För Gul!");
      myButton.setBackground(Color.BLUE);
  }
}

1.You should set setVisible( boolean b) to true .

It Shows or hides this Window depending on the value of parameter b .

setVisible(true);

Add this line should be in the constructor of Makke class.

Read setVisible(boolean) doc


2.Your actionPerformed(.) method

if (e.getSource() == MyButton) {
   //MyButton = new JButton("Tryck För Gul!"); remove this line
  //otherwise every time new button object will be created.
    MyButton.setText("Tryck För Gul!");//to change the button text
    MyButton.setBackground(Color.BLUE);
}

3.Need main method to run your app. Create a class contains main(-) to run your app, in that create object of Makke class.

public class Main {
public static void main(String []args)throws Exception {

        new Makke();//now your window will be appeared.
}
}

Read - Why main(-) method needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM