【发布时间】:2012-02-08 00:52:13
【问题描述】:
我需要一些帮助。我正在尝试创建一个 JFrame 来包含选项卡,并且每个选项卡都将显示一个面板。每个面板都包含按钮和Textfields。面板应该在每个选项卡上显示一个单独的类,但面板显示时没有按钮和Textfields。任何帮助将不胜感激。
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();
}
}
【问题讨论】:
-
为什么有两个
JFrames?你已经在扩展它了。 -
几件事:1) 几乎总是使用 Swing 覆盖
paintComponent,而不是paint。 2) 您正在循环中重新分配常量值(用于文本的前导、上升和高度)。 3) 您正在扩展组件,但不是使用this,而是创建新组件并将它们设置为可见而不是添加它们。我会发布一个答案。
标签: java swing jframe jpanel jtabbedpane