Marco Hoheneder
Grünschnabel
Hallo. Ich habe ein Problem. Ich will bei meinem Programm ein Menü erstellen weiß aber nicht wie das funktioniert.
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
Auf dieser Seite findet man ein Tutorial was mir aber leider nicht weiter hilft.
Code:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
public class ColorGameGUI {
private JFrame frame;
private JButton button;
private JLabel hitsLabel;
private JPanel colorPanel, arrPanel;
private int hits, clicks;
private int searchX, searchY;
private double percent;
private Color randomColor;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;
public static void main(String[] args){
ColorGameGUI colorgamegui=new ColorGameGUI();
colorgamegui.go();
}
public void go(){
frame=new JFrame("ClickTheRightColor");
arrPanel=new JPanel();
hitsLabel=new JLabel("Clicks: "+clicks+" Hits: "+hits+" Percent: "+percent);
colorPanel=new JPanel();
arrPanel.setLayout(new GridLayout(4, 4));
searchX=(int)(Math.random()*4);
searchY=(int)(Math.random()*4);
for(int a=0; a<4; a++){
for(int b=0; b<4; b++){
randomColor=createColor();
button=new JButton();
button.setBackground(randomColor);
button.addActionListener(new MyListener(a, b, button));
arrPanel.add(button);
if((a==searchX)&&(b==searchY)){
colorPanel.setBackground(randomColor);
}
}
}
colorPanel.setBounds(260, 25, 280, 40);
arrPanel.setBounds(260, 100, 280, 300);
hitsLabel.setBounds(260, 425, 280, 40);
frame.add(colorPanel);
frame.add(hitsLabel);
frame.add(arrPanel);
frame.setLayout(null);
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
public Color createColor(){
int red=(int) (Math.random()*255);
int blue=(int) (Math.random()*255);
int green=(int) (Math.random()*255);
Color randomColor=new Color(green, blue, red);
return randomColor;
}
class MyListener implements ActionListener{
private int x, y;
JButton button;
/**
* This method is the constructor of the MyListener class.
* @param x
* @param y
* @param button
*/
public MyListener(int x, int y, JButton button){
this.x=x;
this.y=y;
this.button=button;
}
@Override
public void actionPerformed(ActionEvent ae){
clicks++;
if((x==searchX)&&(y==searchY)){
hits++;
frame.setVisible(false);
go();
}
percent=Math.round((((double)hits)/((double)clicks))*100);
hitsLabel.setText("Clicks: "+clicks+" Hits: "+hits+" Percent: "+percent);
}
}
}
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
Auf dieser Seite findet man ein Tutorial was mir aber leider nicht weiter hilft.