2016-08-02 5 views
1

これは私の運動の説明です: ユーザーがボタンの1つをクリックすると、中央のパネルが のランダムな色に変わります。一番上のLabelは、テキストを4つの文字列の配列でランダムな要素 に変更する必要があります。 4つのボタンのボタン配列を使用します。 ユーザーがラベル上でカーソルを動かすたびに、テキストの色はランダムな色 とフォントに変わる必要があります。マウスがラベルを離れると、デフォルトに戻ります。ボタン配列を追加する方法がわからない - 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の配列を作成することができます。クイックスイッチステートメントを使用して色を定義することができます。

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

関連する問題