【发布时间】:2015-12-06 02:17:45
【问题描述】:
我正在使用 netbeans 构建一个 Java 应用程序。我创建了一个 jbutton,并希望通过使用它来运行一个单独的 jframe。问题在于点击 jbutton 时 jframe 即将到来,但方法未执行。
jframe处的代码是:
public class DroneJFrame extends javax.swing.JFrame {
private int x;
private int y;
private String text;
private int a;
private int b;
private String text1;
private int c;
private int d;
private String text2;
public DroneJFrame() {
initComponents();
x= 10;
y=150;
a=10;
b=150;
c=1000;
d=150;
text="Drone";
text1="Factory";
text2="Hospital";
setSize(1070,300);
this.getContentPane().add(jPanel1);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, 1070, 300);
g.setColor(Color.BLACK);
g.drawString(text, x, y);
System.out.println(x + "" + y);
g.drawString(text1, a, b);
g.drawString(text2, c, d);
}
public void factory() throws InterruptedException{
while(true){
while(a<=getWidth()){
b=getHeight()/2;
repaint();
Thread.sleep(1000000);
}
}
}
public void Hospital() throws InterruptedException{
while(true){
while(c<=getWidth()){
d=getHeight()/2;
repaint();
Thread.sleep(1000000);
}
}
}
public void start() throws InterruptedException{
while(true){
while(x <= getWidth()-50){
x++;
y = getHeight()/2;
repaint();
Thread.sleep(50);
}
JOptionPane.showMessageDialog(null, "Drone Has Been Reached To Hospital");
while(x >= 0){
x--;
y = getHeight()/2;
repaint();
Thread.sleep(50);
}
break;
}
}
public static void main(String args[]) throws InterruptedException {
DroneJFrame drone = new DroneJFrame();
drone.start();
drone.factory();
drone.Hospital();
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(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
}
});
}
private javax.swing.JPanel jPanel1;
}
jbutton处的代码是:
private void droneActionPerformed(java.awt.event.ActionEvent evt) {
DroneJFrame drone = new DroneJFrame();
drone.setVisible(true);
drone.setLocation(180,200);
drone.setDefaultCloseOperation(DroneJFrame.HIDE_ON_CLOSE);
}
我不明白我的错误在哪里?任何人都可以弄清楚。提前致谢
【问题讨论】:
-
如果您可以创建并发布minimal reproducible example(请阅读链接)、此处发布的代码(不在链接中)、我们可以直接复制并粘贴到我们的 IDE 中并自行测试的代码,那总是最好的.这不是完整的代码转储,而是可以直接向我们展示您的问题的最小可编译和可运行程序。
-
另外,一些所谓的“侧面”问题,与您手头的问题无关但仍然很重要的问题:您可能真的不想显示多个 JFrame。请参阅The Use of Multiple JFrames, Good/Bad Practice? 了解原因以及可以使用的替代方案。此外,您永远不想像您正在做的那样直接在 JFrame 的paint 方法中进行绘制,因为这可能会产生令人讨厌和意想不到的副作用,尤其是因为您从未调用过 super 的 paint 方法。编辑:正如@AndrewThompson 上面所说的那样!
-
不要从
JFrame扩展,它会将您锁定在一个用例中。不要覆盖顶级容器的paint,如JFrame,还有更多关于如何实际绘制顶级容器的内容,然后再处理。在这两种情况下,都以JPanel开头,如果您需要进行自定义绘画,请覆盖paintComponent -
顺便说一句-从您的个人资料中,我注意到在之前的问题中,只有一个没有答案,但其余的都没有accepted answer!这是为什么?如果 SO 对我来说效果很差,以至于我从来没有得到正确的答案,我会寻找另一个可能的答案来源..
-
你的
while (true)循环肯定会出现问题,请查看Concurrency in Swing 了解原因
标签: java swing netbeans methods jframe