2016-08-02 21 views
1

這是我的練習描述: 當用戶單擊其中一個按鈕時,中間的面板必須更改爲 隨機顏色。頂部的標籤必須將其文本更改爲一個由四個字符串組成的隨機元素 。使用四個按鈕的按鈕陣列。每當 用戶將光標移動到標籤上時,文本顏色必須更改爲隨機顏色 和字體。當鼠標離開標籤時,它必須回到默認狀態。不知道如何添加按鈕陣列 - java

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.event.*; 
//2.3.5 - 1 
public class UserGui extends JFrame implements ActionListener, WindowListener, MouseListener { 
    private JLabel label; 
    private JButton button[] = new JButton[4]; 
    public UserGui() { 
     super("My Frame"); 
     this.setSize(300,150); 
     JMenuBar myMenuBar = new JMenuBar(); 
     ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13)); 
     setLayout(new GridBagLayout()); 
     JPanel colorize = new JPanel(); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.HORIZONTAL; 


     // from here 
     //strings put in array for label change(by hovering over) 
     String[] myStringArray = new String[]{"Funky","Classic","Legendary","Awesome"}; 
     Border border = BorderFactory.createLineBorder(Color.BLACK, 5); 
     label = new JLabel("Java Wins!"); 
     c.ipady = 40; 
     c.weightx = 0.0; 
     c.gridwidth = 4; 
     c.gridx = 0; 
     c.gridy = 0; 

     setJMenuBar(myMenuBar); 
     JMenu myMenu = new JMenu("File"); 
     JMenuItem quitItem = new JMenuItem("Quit"); 
     add(label); 
     addWindowListener(this); 
     myMenu.add(quitItem); 
     myMenuBar.add(myMenu);  
     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     quitItem.addActionListener(this); 
     setVisible(true); 
    } 

    public void windowOpened(WindowEvent e) { 
     JOptionPane.showMessageDialog(null, "Welcome!"); 
    } 
    /* 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getActionCommand().equals("")) 
     { 
      colorize.setText(field.getText()); 
     } 
    } 
    */ 
    public void actionPerformed(ActionEvent e) { 
     Object source = e.getSource(); 
     if (source.equals("Quit")) { 
      System.exit(1); 
     } 
    } 
    public void windowActivated(WindowEvent e) {} 
    public void windowClosed(WindowEvent e) {} 
    public void windowDeactivated(WindowEvent e) {} 
    public void windowDeiconified(WindowEvent e) {} 
    public void windowIconified(WindowEvent e) {} 
    public void mouseClicked(MouseEvent e) {} 
    public void mouseReleased(MouseEvent e) {} 
    public void mousePressed(MouseEvent e) {} 
    public void mouseEntered(MouseEvent e) {} 
    public void mouseExited(MouseEvent e) {} 
    public void windowClosing(WindowEvent e) { 
     int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?"); 
     if (response == JOptionPane.YES_OPTION) { 
      System.exit(0); // Exits application 
     } 
    } 

    public static void main(String args[]) { 
     JFrame frame = new UserGui(); 
     frame.setVisible(true); 
    } 
} 

回答

3

您可以創建一個Jbutton數組,如下所示,使用for循環來實例化它們並將它們添加到面板。您可以使用快速切換語句來定義顏色。

JButton[] buttons = new JButton[4]; 
    for(int i = 0; i < buttons.length; i++){ 
     switch (i) { 
      case 0: 
       buttons[i] = new JButton("Red"); 
       break; 
      case 1: 
       buttons[i] = new JButton("Blue"); 
       break; 
      case 2: 
       buttons[i] = new JButton("Green"); 
       break; 
      case 3: 
       buttons[i] = new JButton("Black"); 
       break; 
      default: 
       break; 
     } 
     panel.add(buttons[i]); 
    } 
+0

非常感謝!我現在也瞭解 – Workwork