How to open new JFrame by clicking on button in other JFrame?

Java and button events?

  • So im trying to make a java application and i want to make a button the does something when it is clicked, i cant figure out how... here is my code so far: import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MattisCoolClassMain { /** * @param args */ public static void main(String[] args) { JFrame mainframe = new JFrame(); mainframe.setDefaultCloseOperation(J… mainframe.setVisible(true); mainframe.setBounds(0,0,300,300); mainframe.setTitle("Hello Friend"); JPanel mainpanel = new JPanel(); mainframe.add(mainpanel); mainpanel.setBounds(0,0,300,300); mainpanel.setBackground(Color.CYAN); JLabel mainlabel = new JLabel(); mainpanel.add(mainlabel); mainlabel.setBounds(150,150,100,100)… mainlabel.setText("RAJ BUT EXPELS GREEN PISS"); JButton mainbutton = new JButton(); mainpanel.add(mainbutton); mainbutton.setBounds(200,200,100,100… mainbutton.setText("Click me for spam"); // TODO Auto-generated method stub } } So i looked up online, and i saw that i need to use a "action listener", i don't know how i am supposed to get that? Do make a new class? A new Method? -please note i am a great programmer, but i have never messed with java... so please dont use big terms xD thank you very much for any advice, tips, or answers you may be able to give me :-)

  • Answer:

    You need an action listener to catch what action is performed. Here is a quick reference: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Matthew at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.