这是我的图形表示的代码,但问题是lpane调用一次但是当我再次开始重新启动并且它不是娱乐场呼叫的构造函数但是我已经检查了控制台程序也有效但是lpane不会更改卡... ......
package test;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends javax.swing.JFrame {
public Test() {
initComponents();
start.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
new Casino();
new JLayeredPane();
System.out.println(Casino.p[0].getpHand()[0].getValue());
}
});
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
start = new javax.swing.JButton();
lpane = new javax.swing.JLayeredPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
start.setText("Start");
javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(lpane);
lpane.setLayout(jLayeredPane1Layout);
jLayeredPane1Layout.setHorizontalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 691, Short.MAX_VALUE)
);
jLayeredPane1Layout.setVerticalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 475, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(start)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(lpane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(114, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(lpane)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(start)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
public static JLayeredPane getLPane(){
return lpane;
}
private static javax.swing.JLayeredPane lpane;
private static javax.swing.JPanel jPanel1;
private static javax.swing.JButton start;
}
答案 0 :(得分:1)
退一步,查看您的mouseClicked
活动......
public void mouseClicked(MouseEvent e){
new Casino();
new JLayeredPane();
System.out.println(Casino.p[0].getpHand()[0].getValue());
}
您创建了Casino
和JLayeredPane
的新实例并对其执行了操作,甚至没有将它们分配给某种引用......
不要依赖static
引用,它们是设计糟糕的明显标志......
不要在按钮上使用MouseListener
,您应该使用ActionListener
远离表单设计师,严肃地说,它会让你受到伤害。花点时间阅读Creating a GUI With JFC/Swing,了解如何手动构建UI以及生成复杂界面所需的技巧,然后才能了解表单设计者可以为您做什么以及如何使用它。