2012-06-30 62 views
6

坐在我的電腦上約13小時,我覺得我的眼睛在流血。 我找到了一個我喜歡叫GuiGenie的小貴編輯。 它適用於創建與按鈕和所有好東西的窗口。 問題是我想單擊我的第一個菜單中的按鈕,並打開我製作的其他菜單。 我剛剛開始編程4周前,所以我是一個完整的noob。 我有一種感覺它搞亂,因爲主要方法了,但我不知道和13幾個小時坐在這裏試圖以百萬計的事情是讓我發瘋了:) 這是我走到這一步,Java通過點擊某個按鈕打開一個新窗口

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

public class MyPanel extends JPanel { 
private JTextField How; 
private JLabel jcomp2; 
private JLabel jcomp3; 
private JButton jcomp4; 

public MyPanel() { 
    //construct components 
    How = new JTextField (1); 
    jcomp2 = new JLabel ("How long were you parked?"); 
    jcomp3 = new JLabel ("Minutes"); 
    jcomp4 = new JButton ("openNewWindow"); 

    //adjust size and set layout 
    setPreferredSize (new Dimension (315, 85)); 
    setLayout (null); 

    //add components 
    add (How); 
    add (jcomp2); 
    add (jcomp3); 
    add (jcomp4); 

    //set component bounds (only needed by Absolute Positioning) 
    How.setBounds (245, 50, 60, 25); 
    jcomp2.setBounds (35, 30, 185, 50); 
    jcomp3.setBounds (250, 30, 60, 20); 
    jcomp4.setBounds (0, 0, 315, 25); 

     jcomp4.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 
} 


public static void main (String[] args) { 
    JFrame frame = new JFrame ("MyPanel"); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add (new MyPanel()); 
    frame.pack(); 
    frame.setVisible (true); 
} 
} 

當按下按鈕時,我想讓它打開這個新窗口

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

public class MyPanel2 extends JPanel { 
private JButton jcomp1; 
private JButton jcomp2; 
private JButton jcomp3; 
private JTextField jcomp4; 

public MyPanel2() { 
    //construct components 
    jcomp1 = new JButton ("test1"); 
    jcomp2 = new JButton ("test2"); 
    jcomp3 = new JButton ("test3"); 
    jcomp4 = new JTextField (5); 

    //adjust size and set layout 
    setPreferredSize (new Dimension (395, 156)); 
    setLayout (null); 

    //add components 
    add (jcomp1); 
    add (jcomp2); 
    add (jcomp3); 
    add (jcomp4); 

    //set component bounds (only needed by Absolute Positioning) 
    jcomp1.setBounds (20, 45, 100, 25); 
    jcomp2.setBounds (135, 60, 100, 25); 
    jcomp3.setBounds (260, 35, 100, 25); 
    jcomp4.setBounds (105, 115, 100, 25); 
} 


public static void main (String[] args) { 
    JFrame frame = new JFrame ("MyPanel"); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add (new MyPanel2()); 
    frame.pack(); 
    frame.setVisible (true); 
} 
} 

如果有人可以幫助,我將非常感激! 我有很多尊重你的專業人士,因爲如果你是專業人士,你可能比世界上99.9%更聰明。這東西傷害了我的大腦。

+14

停止採取了修改後的代碼試圖隨機的東西,休息一下,睡一夜,然後讀取Swing指南:HTTP ://docs.oracle.com/javase/tutorial/uiswing/。理解一個JPanel必須包含在一個JDialog的JFrame中才能顯示在新窗口中,並且應該只有一個主要方法。你不需要兩個。 –

回答

4

下面是myPanel類的代碼,用這一個:

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 
import javax.swing.event.*; 

public class MyPanel extends JPanel { 
private JTextField How; 
private JLabel jcomp2; 
private JLabel jcomp3; 
private JButton jcomp4; 

public MyPanel() { 
    //construct components 
    How = new JTextField (1); 
    jcomp2 = new JLabel ("How long were you parked?"); 
    jcomp3 = new JLabel ("Minutes"); 
    jcomp4 = new JButton ("openNewWindow"); 

    jcomp4.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      JFrame frame = new JFrame ("MyPanel"); 
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().add (new MyPanel2()); 
      frame.pack(); 
      frame.setVisible (true); 

     } 
    }); 

    //adjust size and set layout 
    setPreferredSize (new Dimension (315, 85)); 
    setLayout (null); 

    //add components 
    add (How); 
    add (jcomp2); 
    add (jcomp3); 
    add (jcomp4); 

    //set component bounds (only needed by Absolute Positioning) 
    How.setBounds (245, 50, 60, 25); 
    jcomp2.setBounds (35, 30, 185, 50); 
    jcomp3.setBounds (250, 30, 60, 20); 
    jcomp4.setBounds (0, 0, 315, 25); 

     jcomp4.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 
} 


public static void main (String[] args) { 
    JFrame frame = new JFrame ("MyPanel"); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add (new MyPanel()); 
    frame.pack(); 
    frame.setVisible (true); 
} 
} 
+0

+1,作爲另一種選擇:-) –

+4

應該在[事件派發線程](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)上構建和操作Swing GUI對象_only_, 。 – trashgod

8

這裏是你可以做的,對於這種情況,你有多個Forms or Windows你可以做的是使用JPanel這可將此CardLayout設置爲LayoutManager,然後您可以將兩個JPanel s添加到它並使用相同的方法訪問它們。

使用Absolute Positioning時不要使用setBounds()這實際上並不是將組件放入父容器的正確方法。請使用setLocation(...)setSize(...)方法。考慮不要儘可能爲您使用絕對定位。贊成從Java文檔拍攝之前所說線的某些線路如下:

雖然可能沒有佈局管理器做的,你應該使用 佈局管理器,如果在所有可能的。佈局管理器可以更輕鬆地將外觀調整爲與外觀相關的外觀,不同的 字體大小,容器的不斷變化的大小以及不同的語言環境。 佈局管理器也可以很容易地被其他容器重用,以及 其他程序。

由於程序的輸出在任何意義上都不是一種舒緩的體驗。 Atleast LayoutManager可以讓您的工作更輕鬆,因爲您無需爲每個組件指定位置和大小。嘗試穿過Layout Mangers Tutorials,儘快習慣。他們是真正的生活儲蓄:-)

這裏是您SOURCE CODE

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

public class CardLayoutExample 
{ 
    private JPanel contentPane; 
    private MyPanel panel1; 
    private MyPanel2 panel2; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Card Layout Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(
      BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new CardLayout()); 
     panel1 = new MyPanel(contentPane); 
     panel2 = new MyPanel2(); 
     contentPane.add(panel1, "Panel 1"); 
     contentPane.add(panel2, "Panel 2"); 
     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new CardLayoutExample().displayGUI(); 
      } 
     }); 
    } 
} 

class MyPanel extends JPanel { 

    private JTextField How; 
    private JLabel jcomp2; 
    private JLabel jcomp3; 
    private JButton jcomp4; 
    private JPanel contentPane; 

    public MyPanel(JPanel panel) { 

     contentPane = panel; 
     //construct components 
     How = new JTextField (1); 
     jcomp2 = new JLabel ("How long were you parked?"); 
     jcomp3 = new JLabel ("Minutes"); 
     jcomp4 = new JButton ("openNewWindow"); 

     //adjust size and set layout 
     setPreferredSize (new Dimension (315, 85)); 
     setLayout (null); 

     //set component bounds (only needed by Absolute Positioning) 
     How.setBounds (245, 50, 60, 25); 
     jcomp2.setBounds (35, 30, 185, 50); 
     jcomp3.setBounds (250, 30, 60, 20); 
     jcomp4.setLocation(0, 0); 
     jcomp4.setSize(315, 25); 
     jcomp4.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       CardLayout cardLayout = (CardLayout) contentPane.getLayout(); 
       cardLayout.next(contentPane); 
      } 
     }); 

     //add components 
     add (How); 
     add (jcomp2); 
     add (jcomp3); 
     add (jcomp4);    
    } 
} 

class MyPanel2 extends JPanel { 
    private JButton jcomp1; 
    private JButton jcomp2; 
    private JButton jcomp3; 
    private JTextField jcomp4; 

    public MyPanel2() { 
     //construct components 
     jcomp1 = new JButton ("test1"); 
     jcomp2 = new JButton ("test2"); 
     jcomp3 = new JButton ("test3"); 
     jcomp4 = new JTextField (5); 

     //adjust size and set layout 
     setPreferredSize (new Dimension (395, 156)); 
     setLayout (null); 

     //set component bounds (only needed by Absolute Positioning) 
     jcomp1.setBounds (20, 45, 100, 25); 
     jcomp2.setBounds (135, 60, 100, 25); 
     jcomp3.setBounds (260, 35, 100, 25); 
     jcomp4.setBounds (105, 115, 100, 25); 

     //add components 
     add (jcomp1); 
     add (jcomp2); 
     add (jcomp3); 
     add (jcomp4);  
    } 
} 
+2

有用的例子,但你應該指導OP_away_使用'null'佈局。 :-) – trashgod

+1

完成並Thankx再次指出:-) –

相關問題