2012-01-07 8 views
6

मैं इस कार्यक्रम की जरूरत है खुल जाएगा बनाने का तरीका - मुख्य JFrame 2 बटनदो बटन के साथ एक खिड़की है कि एक नई विंडो

  1. बटन है
  2. Button2

जब मैं बटन पर क्लिक करें यह नए विकल्पों के साथ नई जेएफआरएएम विंडो खोलनी है, जबकि अगर मैं बटन 2 पर क्लिक करता हूं तो एक और विंडो खोलें।

इन 2 नई विंडो में मुझे अगले और पिछले जैसे बटन जोड़ना होगा।

मुझे कोई समस्या है, जब मैं बटन 1 खोलता हूं, फिर 2 विंडो खोलें और मुख्य जेएफआरएम अभी भी दिखाई दे।

झूले पर मेरा पहला कार्यक्रम:

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

public class example { 

public static void main (String[] args){  
    JFrame frame = new JFrame("Test"); 
    frame.setVisible(true); 
    frame.setSize(500,200); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    frame.add(panel); 
    JButton button = new JButton("hello agin1"); 
    panel.add(button); 
    button.addActionListener (new Action1()); 

    JButton button2 = new JButton("hello agin2"); 
    panel.add(button2); 
    button.addActionListener (new Action2()); 
} 
static class Action1 implements ActionListener {   
    public void actionPerformed (ActionEvent e) {  
    JFrame frame2 = new JFrame("Clicked"); 
    frame2.setVisible(true); 
    frame2.setSize(200,200); 
    JLabel label = new JLabel("you clicked me"); 
    JPanel panel = new JPanel(); 
    frame2.add(panel); 
    panel.add(label);  
    } 
} 
static class Action2 implements ActionListener {   
    public void actionPerformed (ActionEvent e) {  
    JFrame frame3 = new JFrame("OKNO 3"); 
    frame3.setVisible(true); 
    frame3.setSize(200,200); 

    JLabel label = new JLabel("kliknales"); 
    JPanel panel = new JPanel(); 
    frame3.add(panel); 
    panel.add(label); 
    } 
} 
} 
+4

* "मैं अब न कैसे, लेकिन मैं रेलवे यह जरूरत है," दोनों वर्तनी-चेकर्स * और shift कुंजियों भरपूर हैं। –

उत्तर

6

आप अपने ActionListener दो बार button में जोड़ें। तो इसके अलावा

JButton button2 = new JButton("hello agin2"); 
    panel.add(button2); 
    button2.addActionListener (new Action2());//note the button2 here instead of button 

को button2 के लिए अपने कोड सही, का उपयोग करके correct thread पर अपनी स्विंग कार्रवाई करने EventQueue.invokeLater

+0

मैं कोशिश करता हूं लेकिन मैं यह नहीं कर सकता; /। कृपया मेरे कोड – Lukii007

+3

की मरम्मत करें मेरे द्वारा पोस्ट किया गया कोड आपकी प्रतिलिपि बनाई गई थी, और मैंने केवल '2' जोड़ा। आपको वह खुद करने में सक्षम होना चाहिए। और 'invokeLater' कॉल को सीधे प्रदान किए गए लिंक से सीधे कॉपी किया जा सकता है – Robin

+0

मैं @Robin से सहमत हूं कि आपने बटन 2 को दूसरी पंक्ति पर घोषित करने के बाद बटन बटन दिया है .. बटन ... बटन ** ** ** के बजाय। – Adnan

संबंधित मुद्दे