2012-04-20 7 views
1

私は非常に簡単な個人的なプロジェクトに取り組んでいますが、これはもっと大きなプロジェクトの一部です。どんな助けもありがとうございます。これは私のコードと出力が間違っている、私はそれを実行すると、それは1つの大きなウィンドウと時間を表示する別の小さなウィンドウを表示します。正しい方法は、JLabelの大きなフレームに時間を表示することです。GUIの表示時間

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
//package Employees; 

import java.text.SimpleDateFormat; 
import java.util.Date; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


public class DateShowAPII extends javax.swing.JFrame { 

/** 
* Creates new form DateShowAPI 
*/ 
public DateShowAPII() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    JLabel = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel2.setText("Time"); 

    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(104, 104, 104) 
      .addComponent(jLabel2) 
      .addGap(28, 28, 28) 
      .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(179, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap(139, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
       .addComponent(JLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jLabel2)) 
      .addGap(132, 132, 132)) 
    ); 

    pack(); 
}// </editor-fold> 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 

    JFrame frame = new JFrame(); 

    String date = new SimpleDateFormat("hh:mm:ss").format(new Date()); 
    JLabel l = new JLabel(date); 

    frame.getContentPane().add(l); 

    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      new DateShowAPII().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JLabel JLabel; 
private javax.swing.JLabel jLabel2; 
// End of variables declaration 
} 
+0

あなたの質問は何ですか? – ControlAltDel

+0

"これは私のコードです。出力が間違っています。実行すると大きなウィンドウと時間が表示される別の小さなウィンドウが表示されます。正しい方法はJLabelの大きなフレームに時間を表示する必要があります。彼が書いたことを読む。 – Adam

+0

質問:大きなFRAMEに時間が表示されないので、私のコードに何が間違っているのか分かりません。 –

答えて

2

DateShowAPII(それは大きなものだ)とメインに、あなたは別のものを作成し(と、それをパック)フレームである - それは、時間の小さな一つです。

代わりに、別のフレームを作成せずに、作成した新しいDateShowAPIIにJLabelを追加しないでください。

+0

よろしくお願いいたします。 –

2

1)あなたはDateShowAPIIはjavax.swing.JFrameを拡張2 JFrames

を宣言

JFrameのフレーム=新しいJFrameの()。

同じ内容

2と同様のように見える)Swing Timerを使用してSwing ActionJLabel#setText(date)

3からは)

を意味し、変数やクラス名としてresersed Javaメソッド名を使用しないでください
JLabel = new javax.swing.JLabel(); 

myJLabel = new javax.swing.JLabel(); 
+0

よかった、ありがとう –

関連する問題