收藏 Java初学者,下周六程序设计答辩,窗体设计,在网上找的程序,想自己加个菜单,帮助菜单项如何实现??望前辈们指导一下,很急!!谢啦! [问题点数:40
我是想在这个游戏中加一个帮助菜单,然后下拉菜单项是游戏说明,可是点了说明以后怎么出来那个文本框啊?各位大神帮帮忙吧,之前没好好听课,现在后悔了,我是真的很想明白!最好能给解释一下行吗?谢谢啦!!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class Pintu extends JFrame implements MouseListener,ItemListener,KeyListener{
static final long serialVersionUID = 1L;
JList listResult;
JPanel panMain;
JPanel panGame;
JPanel panRight;
DefaultComboBoxModel dcbm;
JMenuBar mbar;//创建菜单栏
JMenu help;//创建菜单
JMenuItem shuoming;//创建菜单项
JTextField txtCount;
JTextField txtLevel;
JLabel [][] labels;
final int MAX_LEVEL = 10;
int[][] nums;
int level;
long clickCount = 0L;
/**
* Create the frame
*/
public Pintu() {
super();
addComponentListener(new ComponentAdapter() {
public void componentResized(final ComponentEvent e) {
JFrame jf = (JFrame)e.getSource();
if(jf.getHeight()<400) jf.setSize(jf.getWidth(),400);
if(jf.getWidth()<600) jf.setSize(600,jf.getHeight());
panRight.setBounds(jf.getWidth()-panRight.getWidth()-10,10,panRight.getWidth(),jf.getContentPane().getHeight()-20);
panGame.setBounds(10,10,jf.getWidth()-panRight.getWidth()-30,jf.getContentPane().getHeight()-20);
listResult.setSize(listResult.getWidth(), panRight.getHeight()-listResult.getY()-10);
}
});
setName("frmMain");
setTitle("拼图");
setBounds(100, 100, 579, 438);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mbar = new JMenuBar();
this.setJMenuBar(mbar);//把菜单栏显示在窗口中
help = new JMenu("帮助");
mbar.add(help);//把菜单加入菜单栏中
shuoming= new JMenuItem("说明");
help.add("说明");//把菜单项加入菜单中
panMain = new JPanel();
panMain.setLayout(null);
getContentPane().add(panMain, BorderLayout.CENTER);
panGame = new JPanel();
panGame.setLayout(null);
panGame.setBounds(10, 10, 358, 385);
panMain.add(panGame);
panRight = new JPanel();
panRight.setBackground(Color.CYAN);
panRight.setLayout(null);
panRight.setBounds(374, 10, 187, 385);
panMain.add(panRight);
txtLevel = new JTextField();
txtLevel.setText("3");
txtLevel.setBounds(62, 43, 87, 22);
panRight.add(txtLevel);
final JLabel label = new JLabel();
label.setText("行数:");
label.setBounds(10, 45, 39, 18);
panRight.add(label);
final JButton btnStart = new JButton();
btnStart.addMouseListener(new MouseAdapter() {
public void mouseReleased(final MouseEvent e) {
newGame();
}
});
btnStart.setText("开始");
btnStart.setBounds(10, 95, 73, 22);
panRight.add(btnStart);
final JLabel labCount = new JLabel();
labCount.setText("步数:");
labCount.setBounds(10, 176, 39, 18);
panRight.add(labCount);
txtCount = new JTextField();
txtCount.setBounds(62, 174, 87, 22);
panRight.add(txtCount);
dcbm = new DefaultComboBoxModel();
listResult = new JList();
listResult.setModel(dcbm);
listResult.setBounds(13, 245, 164, 130);
panRight.add(listResult);
final JLabel labResult = new JLabel();
labResult.setText("结果:(级别 每局步数)");
labResult.setBounds(10, 221, 167, 18);
panRight.add(labResult);
newGame();
//
}
public boolean newGame(){
boolean rs = false;
try{
level = Integer.parseInt(txtLevel.getText());
if(level < 2 || level >MAX_LEVEL){
JOptionPane.showMessageDialog(this, "请输入大于2且小于" + MAX_LEVEL + "的数值.");
rs=false;
}
nums = new int[level][level];
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
nums[i][j] = i*level+j +1;
}
nums[level-1][level-1] = 0;
randomNums();
creatNewLabel();
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "请输入整数!");
rs = false;
}
return rs;
}
public void creatNewLabel() {
panGame.removeAll();
panGame.repaint();
int len =( panGame.getWidth()>panGame.getHeight() ? panGame.getHeight() : panGame.getWidth()) / level;
labels =new JLabel[level][level];
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
labels[i][j] = new JLabel();
labels[i][j].setFont(new Font("Arial", Font.BOLD, len/2));
labels[i][j].setHorizontalTextPosition(SwingConstants.CENTER);
labels[i][j].setHorizontalAlignment(SwingConstants.CENTER);
labels[i][j].setBorder(new LineBorder(Color.black, 1, false));
labels[i][j].setBounds(j*len, i*len, len, len);
if(nums[i][j] != 0)labels[i][j].setText(""+nums[i][j]);
else labels[i][j].setText("");
labels[i][j].addMouseListener(this);
labels[i][j].setOpaque(true);
labels[i][j].setBackground(Color.lightGray);
panGame.add(labels[i][j]);
}
clickCount = 0L;
txtCount.setText("0");
}
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pintu frame = new Pintu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
((JLabel) e.getSource()).setBackground(Color.orange);
}
}
public void mouseExited(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
((JLabel) e.getSource()).setBackground(Color.lightGray);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
JLabel lab =(JLabel) e.getSource();
int i = lab.getY() / lab.getWidth();
int j = lab.getX() / lab.getWidth();
if(i>0 && nums[i-1][j]==0){
labels[i-1][j].setText(lab.getText());
labels[i][j].setText("");
nums[i-1][j] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(i<level-1 && nums[i+1][j]==0){
labels[i+1][j].setText(lab.getText());
labels[i][j].setText("");
nums[i+1][j] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(j>0 && nums[i][j-1]==0){
labels[i][j-1].setText(lab.getText());
labels[i][j].setText("");
nums[i][j-1] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(j<level-1 && nums[i][j+1]==0){
labels[i][j+1].setText(lab.getText());
labels[i][j].setText("");
nums[i][j+1] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
if( checkNums()){
JOptionPane.showMessageDialog(this, "恭喜你过关!\r\n你点击次数是:" + clickCount);
dcbm.addElement(""+level+" " + clickCount);
//(DefaultListModel)listResult.getModel();//.add(new JLabel(""+level+" " + clickCount));
newGame();
}
txtCount.setText(""+clickCount);
}
}
private boolean checkNums() {
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
if( (i*level+j != level*level-1) && nums[i][j] != i*level+j +1 ){
return false;
}
}
return true;
}
public void randomNums(){
int x1=0,y1=0, x2=0,y2=0,tem=0;
for(int i = 0; i<level*level;i++){
x1 =(int)(Math.random()*level);
y1 =(int)(Math.random()*level);
x2 =(int)(Math.random()*level);
y2 =(int)(Math.random()*level);
tem = nums[x1][y1];
nums[x1][y1] = nums[x2][y2];
nums[x2][y2] = tem;
}
}
public void itemStateChanged(ItemEvent e){
}
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
System.out.println(e.getKeyCode());
}
}
[解决办法]
看的我好累啊
help.add("说明");//把菜单项加入菜单中
你加的只是一个字符串,应该加JMenuItem
也就是shuoming
import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ItemListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
/**
* http://bbs.csdn.net/topics/390333065
* @author cecil
*
*/
public class Pintu extends JFrame implements MouseListener,ItemListener,KeyListener{
static final long serialVersionUID = 1L;
JList listResult;
JPanel panMain;
JPanel panGame;
JPanel panRight;
DefaultComboBoxModel dcbm;
JMenuBar mbar;//创建菜单栏
JMenu help;//创建菜单
JMenuItem shuoming;//创建菜单项
JTextField txtCount;
JTextField txtLevel;
JLabel [][] labels;
final int MAX_LEVEL = 10;
int[][] nums;
int level;
long clickCount = 0L;
/**
* Create the frame
*/
public Pintu() {
super();
addComponentListener(new ComponentAdapter() {
public void componentResized(final ComponentEvent e) {
JFrame jf = (JFrame)e.getSource();
if(jf.getHeight()<400) jf.setSize(jf.getWidth(),400);
if(jf.getWidth()<600) jf.setSize(600,jf.getHeight());
panRight.setBounds(jf.getWidth()-panRight.getWidth()-10,10,panRight.getWidth(),jf.getContentPane().getHeight()-20);
panGame.setBounds(10,10,jf.getWidth()-panRight.getWidth()-30,jf.getContentPane().getHeight()-20);
listResult.setSize(listResult.getWidth(), panRight.getHeight()-listResult.getY()-10);
}
});
setName("frmMain");
setTitle("拼图");
setBounds(100, 100, 579, 438);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mbar = new JMenuBar();
this.setJMenuBar(mbar);//把菜单栏显示在窗口中
help = new JMenu("帮助");
mbar.add(help);//把菜单加入菜单栏中
shuoming= new JMenuItem("说明");
help.add(shuoming);//把菜单项加入菜单中
shuoming.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JDialog j=new JDialog(Pintu.this,"help",true);
j.add(new JLabel("输入帮助文字"));
j.setSize(300, 300);
j.setVisible(true);
}
});
panMain = new JPanel();
panMain.setLayout(null);
getContentPane().add(panMain, BorderLayout.CENTER);
panGame = new JPanel();
panGame.setLayout(null);
panGame.setBounds(10, 10, 358, 385);
panMain.add(panGame);
panRight = new JPanel();
panRight.setBackground(Color.CYAN);
panRight.setLayout(null);
panRight.setBounds(374, 10, 187, 385);
panMain.add(panRight);
txtLevel = new JTextField();
txtLevel.setText("3");
txtLevel.setBounds(62, 43, 87, 22);
panRight.add(txtLevel);
final JLabel label = new JLabel();
label.setText("行数:");
label.setBounds(10, 45, 39, 18);
panRight.add(label);
final JButton btnStart = new JButton();
btnStart.addMouseListener(new MouseAdapter() {
public void mouseReleased(final MouseEvent e) {
newGame();
}
});
btnStart.setText("开始");
btnStart.setBounds(10, 95, 73, 22);
panRight.add(btnStart);
final JLabel labCount = new JLabel();
labCount.setText("步数:");
labCount.setBounds(10, 176, 39, 18);
panRight.add(labCount);
txtCount = new JTextField();
txtCount.setBounds(62, 174, 87, 22);
panRight.add(txtCount);
dcbm = new DefaultComboBoxModel();
listResult = new JList();
listResult.setModel(dcbm);
listResult.setBounds(13, 245, 164, 130);
panRight.add(listResult);
final JLabel labResult = new JLabel();
labResult.setText("结果:(级别 每局步数)");
labResult.setBounds(10, 221, 167, 18);
panRight.add(labResult);
newGame();
//
}
public boolean newGame(){
boolean rs = false;
try{
level = Integer.parseInt(txtLevel.getText());
if(level < 2
[解决办法]
level >MAX_LEVEL){
JOptionPane.showMessageDialog(this, "请输入大于2且小于" + MAX_LEVEL + "的数值.");
rs=false;
}
nums = new int[level][level];
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
nums[i][j] = i*level+j +1;
}
nums[level-1][level-1] = 0;
randomNums();
creatNewLabel();
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "请输入整数!");
rs = false;
}
return rs;
}
public void creatNewLabel() {
panGame.removeAll();
panGame.repaint();
int len =( panGame.getWidth()>panGame.getHeight() ? panGame.getHeight() : panGame.getWidth()) / level;
labels =new JLabel[level][level];
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
labels[i][j] = new JLabel();
labels[i][j].setFont(new Font("Arial", Font.BOLD, len/2));
labels[i][j].setHorizontalTextPosition(SwingConstants.CENTER);
labels[i][j].setHorizontalAlignment(SwingConstants.CENTER);
labels[i][j].setBorder(new LineBorder(Color.black, 1, false));
labels[i][j].setBounds(j*len, i*len, len, len);
if(nums[i][j] != 0)labels[i][j].setText(""+nums[i][j]);
else labels[i][j].setText("");
labels[i][j].addMouseListener(this);
labels[i][j].setOpaque(true);
labels[i][j].setBackground(Color.lightGray);
panGame.add(labels[i][j]);
}
clickCount = 0L;
txtCount.setText("0");
}
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pintu frame = new Pintu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
((JLabel) e.getSource()).setBackground(Color.orange);
}
}
public void mouseExited(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
((JLabel) e.getSource()).setBackground(Color.lightGray);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if(e.getSource().getClass().equals(labels[0][0].getClass())){
JLabel lab =(JLabel) e.getSource();
int i = lab.getY() / lab.getWidth();
int j = lab.getX() / lab.getWidth();
if(i>0 && nums[i-1][j]==0){
labels[i-1][j].setText(lab.getText());
labels[i][j].setText("");
nums[i-1][j] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(i<level-1 && nums[i+1][j]==0){
labels[i+1][j].setText(lab.getText());
labels[i][j].setText("");
nums[i+1][j] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(j>0 && nums[i][j-1]==0){
labels[i][j-1].setText(lab.getText());
labels[i][j].setText("");
nums[i][j-1] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
else if(j<level-1 && nums[i][j+1]==0){
labels[i][j+1].setText(lab.getText());
labels[i][j].setText("");
nums[i][j+1] = nums[i][j];
nums[i][j] = 0;
clickCount++;
}
if( checkNums()){
JOptionPane.showMessageDialog(this, "恭喜你过关!\r\n你点击次数是:" + clickCount);
dcbm.addElement(""+level+" " + clickCount);
//(DefaultListModel)listResult.getModel();//.add(new JLabel(""+level+" " + clickCount));
newGame();
}
txtCount.setText(""+clickCount);
}
}
private boolean checkNums() {
for(int i = 0;i<level;i++)
for(int j =0;j<level;j++){
if( (i*level+j != level*level-1) && nums[i][j] != i*level+j +1 ){
return false;
}
}
return true;
}
public void randomNums(){
int x1=0,y1=0, x2=0,y2=0,tem=0;
for(int i = 0; i<level*level;i++){
x1 =(int)(Math.random()*level);
y1 =(int)(Math.random()*level);
x2 =(int)(Math.random()*level);
y2 =(int)(Math.random()*level);
tem = nums[x1][y1];
nums[x1][y1] = nums[x2][y2];
nums[x2][y2] = tem;
}
}
public void itemStateChanged(ItemEvent e){
}
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
System.out.println(e.getKeyCode());
}
}