使用每个选项卡作为单独的类,在 JFrame 内的选项卡中显示 JPanel 及其组件

发布于 2025-01-03 15:46:05 字数 4033 浏览 0 评论 0原文

请我需要一些帮助。我正在尝试创建一个 JFrame 来包含选项卡,每个选项卡将显示一个面板。每个面板都包含按钮和文本字段。面板应在每个选项卡上显示单独的类,但面板不显示按钮和文本字段。任何帮助将不胜感激。

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("0123456789ABCDEF", 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("0123456789ABCDEF", 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();

   }
}

Please I need some help. I am trying to create a JFrame to contain Tabs, and each tab will show one panel. Each panel contains buttons and Textfields. The panels are should show a separate class on each tab but the panels show without the buttons and the Textfields. Any help will be appreciated.

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("0123456789ABCDEF", 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("0123456789ABCDEF", 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();

   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2025-01-10 15:46:05

这根本不是一个完整的修复,但至少可以让您朝着正确的方向前进。仔细将其与您的代码进行比较以查看差异。希望这有帮助。

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("0123456789ABCDEF", 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("0123456789ABCDEF", 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();
   }
 }
}

This is not a complete fix at all, but at least something to get you going in the right direction. Compare it carefully with your code to see the differences. Hope this helps.

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("0123456789ABCDEF", 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("0123456789ABCDEF", 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();
   }
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文