swing Java JLabel不工作
我对爪哇很陌生。我决定将tic-tac-toe编码为练习(从头开始)
不管怎样,我试图在单击时更改“JLabel标签”;从1开始,然后是2,等等。最终它会变成数字以外的东西。但是,就目前而言,它适用于测试
“系统”。出来println(“点击次数:“+mouseIn”);'运行良好,并产生我想要的输出。控制台和JFrame/JPanel/JLabel的图片可以在这里看到:
(JFrame中的小1是JLabel。我想匹配控制台中的输出
我已经在谷歌上搜索过,尝试过我知道的一切(其实不多!)我不能让这该死的东西工作。。。我只是想得到一些指导。主要方法仅包括建造框架/面板
代码如下:
从名为(namone.java[出于我自己的原因将其命名])的主类中:
public void run(JPanel p) //Takes panel from main method {
for(int i = 0; i < ticList.length; i++){
ticList[i] = new JButton(buttonText); //For every position in
//ticList[], create a JButton object
p.add(ticList[i]);
ticList[i].setPreferredSize(new Dimension(140,140));
ticList[i].addMouseListener(mouse); //Load mouseListner
}
//Set mouseIn to click value in MouseEvents Class
int mouseIn = mouse.getClicks();
//Set text value to text value in MouseEvents class
text = mouse.getText();
//Output...
System.out.println("Number of clicks: " + mouseIn); //For testing
String mouse = Integer.toString(mouseIn); //Convert mouseIn value (from MouseEvents.java) to mouse
JLabel label = new JLabel(); //New JLabel
p.add(label); //Add label to screen
label.setText(mouse); //Set the text of the label to value of mouse
System.out.println(mouse); //For testing
//So I can see if it's working (clicks wise)
}
然后是我鼠标事件中的代码。java类:
public class MouseEvents extends namone implements MouseListener {
int clicks = 1;
String text = "first"; //For testing purposes
public namone game = new namone();
public int getClicks(){
return clicks;
}
public String getText(){
return text;
}
public int intToString(){
Integer.toString(clicks);
return clicks;
}
@Override
public void mouseClicked(MouseEvent e) {
clicks++;
intToString();
JPanel p = new JPanel();
text = "" + clicks;
game.run(p);
}
就像我说的。对Java非常陌生;我在努力学习。我相信这是我自己的无知造成的
谢谢
共 (0) 个答案