ためEvents
を処理する必要があります。 ..
public class Hasher extends JFrame implements ActionListener {
//...
private JFrame contentPane;
//..
これはすぐに質問が表示されます。質問は実際には画面に表示され、実際にはどの質問と実際にやり取りしていますか?これはあなたの問題に直接関係しないかもしれませんが、混乱します。
大まかに言えば、JFrame
のようなトップレベルのコンテナからは伸びず、1つのユースケースに固定され、混乱を招くことはありません。代わりに、JPanel
で始まり、JFrame
などのインスタンスに追加してください。
"主な原因"の問題は、スイングが怠惰だということです。あなたが指示するかUIのサイズが変更されるまで、UIは更新されません。コンポーネントの追加と削除、アップデートをトリガするために容器上にrevalidate
とrepaint
を呼び出す必要が
- これはそれがprivate JFrame contentPane;
とないのcontentPane
であることにより、実際に、あなたはcontentPane
を参照している、あなたのコードで間違って何が起こっているのですJFrame
からHasher
が伸びています。見て、混乱します。
あなたのコードの周りを少しでも掘り下げれば、何度も繰り返される操作が繰り返されていることは明らかです。唯一の違いは、テキストをハッシュするアルゴリズムです。
だから、そのことを念頭に置いて、ほとんどの作業を行うための基本クラスをいくつか作成することができます。たとえば、...
public class HashPane extends JPanel {
private JLabel hashedLabel;
private JLabel promptLabel;
private HashAlgorithim algorithim;
public HashPane(String labelText, HashAlgorithim algorithim) {
setOpaque(false);
this.algorithim = algorithim;
setLayout(new FlowLayout(FlowLayout.LEFT));
promptLabel = new JLabel(labelText);
add(promptLabel);
hashedLabel = new JLabel("??");
add(hashedLabel);
}
public void setText(String text) {
hashedLabel.setText(algorithim.generateHash(text));
}
}
これは2つのラベルで、プロンプトと結果を示しています。結果は付属のテキスト
アルゴリズム自体のハッシュを生成するために使用されるプラグアルゴリズムを介して生成されることは...ちょうどあなたが、その後に必要となる
public interface HashAlgorithm {
public String generateHash(String from);
}
基本契約を定義するinterface
ですあなたは今
を使用していた各アルゴリズムの実装を作成し、目に見えない/見えパネルを作ることは、単純に、簡単なMap
によって達成することができますHashPane
でJCheckBox
を関連付けるの問題になり...
あなたは、その後持つすべての
JCheckBox
の
public class Hasher extends JPanel {
//...
private Map<JCheckBox, HashPane> mapPanes;
public Hasher() {
//...
ActionHandler listener = new ActionHandler();
for (Entry<JCheckBox, HashPane> entry : mapPanes.entrySet()) {
entry.getKey().addActionListener(listener);
entry.getValue().setVisible(false);
}
}
protected class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
HashPane hashPane = mapPanes.get(cb);
hashPane.setVisible(cb.isSelected());
revalidate();
repaint();
}
}
そして、私はかなりあなたのコードを虐殺ので...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Hasher extends JPanel {
String UserInput;
private JTextField textInputField;
private static JCheckBox MD5Check, SHA1Check, SHA256Check, FileCheck;
private Map<JCheckBox, HashPane> mapPanes;
public Hasher() {
setLayout(new BorderLayout());
setBackground(new Color(88, 148, 202));
//CheckBoxes
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel checkBoxPanel = new JPanel();
MD5Check = new JCheckBox("MD5");
checkBoxPanel.add(MD5Check);
SHA1Check = new JCheckBox("SHA-1");
checkBoxPanel.add(SHA1Check);
SHA256Check = new JCheckBox("SHA-256");
checkBoxPanel.add(SHA256Check);
FileCheck = new JCheckBox("File Hashing");
checkBoxPanel.add(FileCheck);
mainPanel.add(checkBoxPanel);
add(mainPanel, BorderLayout.NORTH);
JPanel centerPane = new JPanel(new BorderLayout());
centerPane.setOpaque(false);
JPanel inputPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
inputPane.setOpaque(false);
//Entered data to perform hash on
inputPane.add(new JLabel("Enter text to hash: "));
textInputField = new JTextField(20);
inputPane.add(textInputField);
centerPane.add(inputPane, BorderLayout.NORTH);
JPanel output = new JPanel(new GridBagLayout());
output.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
HashPane md5Pane = new HashPane("MD5 hash = ", new NotAMD5Alorithim());
output.add(md5Pane, gbc);
gbc.gridx = 0;
gbc.gridy++;
HashPane sha1Pane = new HashPane("SHA-1 hash = ", new NotAMSHA1Alorithim());
output.add(sha1Pane, gbc);
// last pane
gbc.gridy++;
gbc.weighty = 1;
output.add(new JLabel(), gbc);
centerPane.add(output);
add(centerPane);
mapPanes = new HashMap<>(25);
mapPanes.put(MD5Check, md5Pane);
mapPanes.put(SHA1Check, sha1Pane);
//...
ActionHandler listener = new ActionHandler();
for (Entry<JCheckBox, HashPane> entry : mapPanes.entrySet()) {
entry.getKey().addActionListener(listener);
entry.getValue().setVisible(false);
}
}
protected class ActionHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
HashPane hashPane = mapPanes.get(cb);
hashPane.setVisible(cb.isSelected());
revalidate();
repaint();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Hasher());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public interface HashAlgorithm {
public String generateHash(String from);
}
public class NotAMD5Alorithim implements HashAlgorithm {
@Override
public String generateHash(String from) {
return "bananas";
}
}
public class NotAMSHA1Alorithim implements HashAlgorithm {
@Override
public String generateHash(String from) {
return "bananas";
}
}
public class HashPane extends JPanel {
private JLabel hashedLabel;
private JLabel promptLabel;
private HashAlgorithm algorithim;
public HashPane(String labelText, HashAlgorithm algorithim) {
setOpaque(false);
this.algorithim = algorithim;
setLayout(new FlowLayout(FlowLayout.LEFT));
promptLabel = new JLabel(labelText);
add(promptLabel);
hashedLabel = new JLabel("??");
add(hashedLabel);
}
public void setText(String text) {
hashedLabel.setText(algorithim.generateHash(text));
}
}
}
を管理するための単一ActionListener
を使用することができます
public class Hasher extends JPanel {
//...
private Map<JCheckBox, HashPane> mapPanes;
public Hasher() {
//...
HashPane md5Pane = new HashPane("MD5 hash = ", new NotAMD5Alorithim());
//...
HashPane sha1Pane = new HashPane("SHA-1 hash = ", new NotAMSHA1Alorithim());
//..
mapPanes = new HashMap<>(25);
mapPanes.put(MD5Check, md5Pane);
mapPanes.put(SHA1Check, sha1Pane);
//...
そのすべてを言った、私はにJCheckBox
を持っていると誘惑されるかもしれない13230と常にHashPane
が表示され、単に出力テキストを無効にする...または単に気にせずに常にすべてのアルゴリズムを利用できるようにする
*「Jlabelをビューから追加/削除することはできますか? "*もちろん。アイコンをヌルに、テキストを '' ''(空の文字列)に設定します。 Voila!それは不可視です。 –