2012-02-08 5 views
1

私はいくつかの助けが必要です。タブを含むようにJFrameを作成しようとしています。各タブには1つのパネルが表示されます。各パネルにはボタンとTextfieldsが含まれています。パネルには各タブに別のクラスが表示されますが、パネルにはボタンとTextfieldsが表示されません。どんな助けもありがとう。JFrame内のタブにJPanelsとそのコンポーネントを個別のクラスとして表示する

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.JFrame; 

public class CELTool extends JFrame implements ActionListener { 

    JTabbedPane tab; 

    public CELTool() { 
     JFrame frame = new JFrame("CELTool"); 
     tab = new JTabbedPane(); 
     frame.add(tab, BorderLayout.CENTER); 

     Illustration etool = new Illustration(); 
     tab.add("Illustration", etool); 

     Encrypt crypt = new Encrypt(); 
     tab.add("Crypt", crypt); 

     Decrypt decrypt = new Decrypt(); 

     tab.add("Decrypt", decrypt); 

     frame.setSize(500, 750); 
     frame.pack(); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 

    } 

    public void actionPerformed(ActionEvent ae) { 
    } 

    public static void main(String[] args) { 
     CELTool clt = new CELTool();//   
     clt.setSize(400,500);  
     clt.setVisible(true); 
    } 
} 

class Illustration extends JPanel { 

    static String[] strPlaintext = {"Original Input", "InitialPermutation", "First RIound ", "Second Round", 
            "Third Round", "Fourth Round", "Fifth Round", "Sixth Round", "Seventh Round", 
            "Eight Round", "Ninth Round", "Tenth Round", "Eleventh Round", "Twelveth Round", 
            "Thirteenth Round", "Fourtheenth Round", "Fithteenth Round", "Sixtheenth Round", 
            "Final Round", "Ciphertext"}; 
    static String[] str2 = {"Key", "Round Sixteen", "Round Fithteen", "Round Fourteen", "Round Thirteen", "Round Twelve", 
          "Round Eleven", "Round Ten",}; 
    int leading, ascent, height, width; 
    int leading2, ascent2, height2, width2; 
    int xcoord = 40, ycoord = 100, xcoord2 = 600, ycoord2 = 60; 
    static final int BORDER = 5; 

    public void paint(Graphics gr) { 
     super.paint(gr); 
     int i = 0, xcoord = 40, ycoord = 100, xcoord2 = 600, ycoord2 = 60; 


     leading = gr.getFontMetrics().getLeading(); 
     ascent = gr.getFontMetrics().getAscent(); 
     height = gr.getFontMetrics().getHeight(); 
     width = gr.getFontMetrics().stringWidth(strPlaintext[i]); 

     for (i = 0; i < strPlaintext.length; i++) { 
     gr.drawString(strPlaintext[i], xcoord, ycoord); 
     leading = gr.getFontMetrics().getLeading(); 
     ascent = gr.getFontMetrics().getAscent(); 
     height = gr.getFontMetrics().getHeight(); 
     width = gr.getFontMetrics().stringWidth(strPlaintext[i]); 
     gr.drawRect(xcoord - BORDER, ycoord - (ascent + leading + BORDER), width + 2 * BORDER, height + 2 * BORDER); 
     ycoord += 40; 

     } 

    } 
} 

class Encrypt extends JPanel { 

    public Encrypt() { 
     JPanel panel5 = new JPanel(); 
     panel5.setLayout(new GridBagLayout()); //panel5.setLayout((LayoutManager) new GridBagConstraints()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     panel5.add(new JLabel("Input plaintext text"), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     panel5.add(new JTextField("ABCDEF", 20), gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 2; 
     panel5.add(new JLabel("Input text in Hex"), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 2; 
     panel5.add(new JTextField("ABCDEF", 20), gbc); 

     panel5.setSize(200, 300); 
     panel5.setVisible(true); 
    } 
} 

class Decrypt extends JPanel { 

    JPanel decPanel; 
    JTextField dectxt; 

    public Decrypt() { 
     decPanel = new JPanel(); 
     decPanel.add(new JLabel("Cipher")); 
     decPanel.add(new JTextField()); 
     decPanel.add(new JLabel("key")); 
     decPanel.add(new JTextField()); 
     decPanel.add(new JLabel("Plaintext")); 
     decPanel.add(new JTextField()); 
     decPanel.add(new JButton("Decrypt")); 
     decPanel.add(new JButton("Reset")); 
     decPanel.validate(); 

    } 
} 
+0

「JFrames」が2つあるのはなぜですか?あなたはすでにそれを拡張しています。 – rtheunissen

+1

ほとんどのこと:1)ほとんどの場合、 'paint'ではなくSwingを使って' paintComponent'をオーバーライドします。 2)ループの定数値を再割り当てします(テキストの先頭、高さ、高さ)。 3)コンポーネントを拡張していますが、 'this'を使用する代わりに、新しいコンポーネントを作成し、追加する代わりに可視に設定しています。私は答えを投稿します。 – rtheunissen

答えて

1

これはまったく修正されているわけではありませんが、少なくとも正しい方向に進むためのものです。違いを確認するには、コードと慎重に比較してください。お役に立てれば。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.JFrame; 

public class CELTool extends JFrame implements ActionListener { 

    JTabbedPane tab; 
    Illustration etool; 
    Encrypt encrypt; 
    Decrypt decrypt; 

    private CELTool() { 
     super("CELTool"); 

     tab = new JTabbedPane(); 
     etool = new Illustration(); 
     encrypt = new Encrypt(); 
     decrypt = new Decrypt(); 

     tab.add("Illustration", etool); 
     tab.add("Crypt", encrypt); 
     tab.add("Decrypt", decrypt); 
     this.add(tab, BorderLayout.CENTER); 

     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setSize(500, 750); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     // why blank? 
    } 

    public static void main(String[] args) { 
     new CELTool();  
    } 


private class Illustration extends JPanel { 

    public Illustration(){ 

    } 

    private final String[] strPlaintext = {"Original Input", "InitialPermutation", "First RIound ", "Second Round", 
            "Third Round", "Fourth Round", "Fifth Round", "Sixth Round", "Seventh Round", 
            "Eight Round", "Ninth Round", "Tenth Round", "Eleventh Round", "Twelveth Round", 
            "Thirteenth Round", "Fourtheenth Round", "Fithteenth Round", "Sixtheenth Round", 
            "Final Round", "Ciphertext"}; 
    private final String[] str2 = {"Key", "Round Sixteen", "Round Fithteen", "Round Fourteen", "Round Thirteen", "Round Twelve", 
          "Round Eleven", "Round Ten",}; 
    int leading, ascent, height, width; 
    int xcoord = 40, ycoord = 100, xcoord2 = 600, ycoord2 = 60; 
    private final int BORDER = 5; 

    public void paintComponent(Graphics gr) { 
     super.paintComponent(gr); 
     int i = 0, xcoord = 40, ycoord = 100, xcoord2 = 600, ycoord2 = 60; 

     leading = gr.getFontMetrics().getLeading(); 
     ascent = gr.getFontMetrics().getAscent(); 
     height = gr.getFontMetrics().getHeight(); 

     for (i = 0; i < strPlaintext.length; i++) { 
     gr.drawString(strPlaintext[i], xcoord, ycoord); 
     width = gr.getFontMetrics().stringWidth(strPlaintext[i]); 
     gr.drawRect(xcoord - BORDER, ycoord - (ascent + leading + BORDER), width + 2 * BORDER, height + 2 * BORDER); 
     ycoord += 40; 
     } 
    } 
} 

private class Encrypt extends JPanel { 

    public Encrypt() { 
     this.setLayout(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     this.add(new JLabel("Input plaintext text"), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     this.add(new JTextField("ABCDEF", 20), gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 2; 
     this.add(new JLabel("Input text in Hex"), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 2; 
     this.add(new JTextField("ABCDEF", 20), gbc); 

     this.setPreferredSize(new Dimension(200, 300)); 
    } 
} 

private class Decrypt extends JPanel { 

    public Decrypt() { 
     this.setLayout(new GridLayout(0,1)); 
     this.add(new JLabel("Cipher")); 
     this.add(new JTextField()); 
     this.add(new JLabel("key")); 
     this.add(new JTextField()); 
     this.add(new JLabel("Plaintext")); 
     this.add(new JTextField()); 
     this.add(new JButton("Decrypt")); 
     this.add(new JButton("Reset")); 
     this.validate(); 
    } 
} 
} 
+0

ありがとう、paranoid-androidありがとうございます。私は、私はクラスのインスタンスを使用することができます私は作成し、JFrameから拡張することができなかった理由は、javaに新しいです。私はそのJFrameを削除しようとし、JFrameを再定義せずにどのように管理できるかを見ていきます。ほとんどのJavaサンプルコードでは、常にjava.awtとjavax.swingが読み込まれているので、なぜ私はそれらを読んでいるのかわかりません。私はpaintComponentを使用するか、またはペイントするだけで、違いはありますか? – Iyemwen

+0

1)たとえば 'JFrame'を拡張している場合、そのクラスは' JFrame'なので、 'new JFrame()'と同じように扱うことができます。 AWTとSwingは非常に異なっており、違いを学び、それらを混在させないようにするには時間がかかるかもしれません。 2)私はチュートリアルを読むことがあなたにはるかに良い理解を与えると思います。 Javaチュートリアル:http://goo.gl/1zkR9 – rtheunissen

関連する問題