私はJavaの初心者コーダーで、GUIのコーディング方法を学んでいます。私の懸念は、入力関数に応じてさまざまな論理ゲート (AND、OR など) を表示できる部分が得られないことです。Boolean Simplification のコードを書きました。これをロジック ダイアグラムで表示するために必要な作業はすべて完了です。しかし、私はGUIが初めてなので、そのために何をすべきか本当にわかりません。これに関する指示/リンク/チュートリアルは非常に役立ちます。よろしくお願いします。
3 に答える
この例を見てください。何を行うかは、ロジックを GUI から分離することです。BooleanSimplification
メソッドが 1 つしかないクラスの簡単な例を使用しました。このメソッドはすべて、2 つの数値を加算して結果を返します。
興味深いのはBooleanExpresson
、GUI でクラスのインスタンスを作成したことだけであり、そのメソッドを呼び出して論理演算メソッドを取得する方法です。GUI をより管理しやすくするために、これが目的の方法です。データとロジックを GUI から分離します。
BooleanSimplification
まずはクラスをご覧ください
class BooleanSimplification {
public BooleanSimplification() {
}
public double add(double num1, double num2) {
return num1 + num2;
}
}
それから私のGUIクラス。それは、2 つの入力フィールドと結果入力フィールドと計算するためのボタンを備えた単純な電卓です。
次のコードをコピーして貼り付けることができます。コード内のコメントを試してみてください
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TestRun extends JPanel {
JLabel jlblnum1 = new JLabel("Number1"); // number 1 label
JLabel jlblnum2 = new JLabel("Number2"); // number 2 label
JLabel jlblresult= new JLabel("result"); // result label
JTextField jtfnum1 = new JTextField(10); // number1 text field
JTextField jtfnum2 = new JTextField(10); // number 2 text field
JTextField jtfresult = new JTextField(10); // result text field
JButton getResult = new JButton("Get Result"); // get result button
// instance of class here
BooleanSimplification bs = new BooleanSimplification(); // the class
public TestRun() {
JPanel panel = new JPanel(new GridLayout(3, 2)); // panel to hold components
panel.add(jlblnum1);
panel.add(jtfnum1);
panel.add(jlblnum2);
panel.add(jtfnum2);
panel.add(jlblresult);
panel.add(jtfresult);
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
add(getResult, BorderLayout.SOUTH);
// here is where everything goes on.
getResult.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
double num1 = Double.parseDouble(jtfnum1.getText()); // get number from text field
double num2 = Double.parseDouble(jtfnum2.getText()); // get number from text field
double result = bs.add(num1, num2); // call the method from the BooleanSimplification class to handle my logic
jtfresult.setText(String.valueOf(result)); // set the text of result with the result from the method
}
});
}
public static void createAndShowGui() {
JFrame frame = new JFrame();
frame.add(new TestRun());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class BooleanSimplification {
public BooleanSimplification() {
}
public double add(double num1, double num2) {
return num1 + num2;
}
}
クラスのインスタンスを作成すると、そのすべてのデータとメソッドを GUI で使用できます。
スイングを使用します。学ぶのは難しくありません。Netbeans にはスイング GUI ビルダーが組み込まれています。あなたはそれを使うことができます。http://docs.oracle.com/javase/tutorial/uiswing/ .