2012-04-01 82 views
0

這是我的代碼下面我無法在其中添加第二個按鈕。我莫名其妙地設法創建一個按鈕,當按下按鈕按下按鈕時顯示另一個按鈕並打印偶數

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Example2 extends JFrame { 

public Example2() { 
     initUI(); 
    } 

public final void initUI() { 
     JPanel panel = new JPanel(); 
     getContentPane().add(panel); 
     panel.setLayout(null); 
     panel.setToolTipText("A Panel container"); 

     JButton button = new JButton("Even"); 
     button.setBounds(100, 60, 100, 30); 
     button.setToolTipText("A button component"); 

     JButton button2 = new JButton("Odd"); 
     button2.setBounds(100, 60, 100, 30); 
     button2.setToolTipText("A button component"); 



     panel.add(button); 
     panel.add(button2); 

     setTitle("Tooltip"); 
     setSize(300, 200); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

    } 
    public static void main(String[] args) { 
       Example2 ex = new Example2(); 
       ex.setVisible(true); 

    } 

} 
+0

是的,這是可能的。 – 2012-04-01 03:43:01

+1

是的。你當然可以。 – 2012-04-01 03:43:33

+2

在卡住之前你嘗試了什麼? – dasblinkenlight 2012-04-01 03:43:50

回答

2

一些修補,你可以嘗試如何顯示設置偶數:

  1. getContentPane().add(panel)部分添加的按鈕後,面板。

您還應該首先定義一個您要顯示的偶數的列表或數組,以及JTextArea或JTextField或另一種顯示文本的方法。對於這些組件,只需使用setText(String string)方法。

相關問題