[英]How to make a JButton that will create other buttons in java Swing?
[英]How can I make certain buttons do different actions? Java Swing JButton
我正在制作一个可以计算GPA并返回当前类所需的GPA的GUI,以获得目标GPA。 我让用户输入他们所采用的所有先前/当前/将来的类,并且我有几个按钮可以将这些信息输入到单独的变量中,稍后我将进行计算。 但是,我的按钮都没有执行任何操作。 有人可以向我解释我的代码有什么问题吗? 我尝试了两种不同的方法(均在示例中进行了介绍),但两种方法均无效。
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GUI extends JFrame implements ActionListener {
private FlowLayout flowlayout = new FlowLayout();
private GridLayout gridlayout = new GridLayout(6, 6);
private JLabel instructions;
private JLabel enteredclasses;
private JLabel status1, status2, status3, status4, status5, status6;
private JTextField classinput1, classinput2, classinput3, classinput4, classinput5, classinput6;
private JTextField hoursinput1, hoursinput2, hoursinput3, hoursinput4, hoursinput5, hoursinput6;
private JComboBox<String> gradeinput1, gradeinput2, gradeinput3, gradeinput4, gradeinput5, gradeinput6;
private JPanel panel2 = new JPanel();
private JButton add1, add2, add3, add4, add5, add6;
private JButton remove1, remove2, remove3, remove4, remove5, remove6;
private String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
private double gpa1, gpa2, gpa3, gpa4, gpa5, gpa6;
private ArrayList<Array> matrix;
private String[] row1 = new String[3];
private String[] row2 = new String[3];
private String[] row3 = new String[3];
private String[] row4 = new String[3];
private String[] row5 = new String[3];
private String[] row6 = new String[3];
public GUI(String name) {
super(name);
}
public void addComponentsToPanel(Container pane) {
JPanel panel1 = new JPanel();
panel1.setLayout(flowlayout);
flowlayout.setAlignment(FlowLayout.LEADING);
instructions = new JLabel("Input the correct information below.");
panel1.add(instructions);
panel2.setLayout(gridlayout);
//creates row 1
addUserInput(classinput1, hoursinput1);
addGrades(gradeinput1);
addButton(add1, "first");
removeButton(remove1, "second");
addStatus(status1);
//creates row 2
addUserInput(classinput2, hoursinput2);
addGrades(gradeinput2);
addButton(add2, "third");
removeButton(remove2, "fourth");
addStatus(status2);
//creates row 3
addUserInput(classinput3, hoursinput3);
addGrades(gradeinput3);
addButton(add3, "fifth");
removeButton(remove3, "sixth");
addStatus(status3);
.........
private static void createAndShowGUI() {
GUI frame = new GUI("GPA Calculator and Planner Design");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPanel(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public void addUserInput(JTextField name, JTextField credithours) {
name = new JTextField("Course Name (optional)");
credithours = new JTextField("Credit Hours");
panel2.add(name);
panel2.add(credithours);
}
public void addGrades(JComboBox<String> choices) {
choices = new JComboBox<String>(grades);
panel2.add(choices);
}
public double convertGrade(String grade) {
String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"};
int index = 0;
for (int i = 0; i < 13; i++) {
if (grade.equals(grades[i])) {
index = i;
}
}
double[] gradeconversions = {0.0 , 4.0, 3.67, 3.3, 3.0, 2.67, 2.33, 2.0, 1.67, 1.33, 1.0, .67, 0.0};
return gradeconversions[index];
}
public void addButton(JButton b, String s) {
b = new JButton("ADD");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
public void removeButton(JButton b, String s) {
b = new JButton("REMOVE");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
public void addStatus(JLabel status) {
status = new JLabel("Status: -");
panel2.add(status);
}
public void addClasses() {
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == add1) {
row1[0] = classinput1.getText();
row1[1] = hoursinput1.getText();
row1[2] = (String) gradeinput1.getSelectedItem();
status1.setText("Status: Added");
}
if (e.getActionCommand().equals("third")) {
row2[0] = classinput1.getText();
row2[1] = hoursinput1.getText();
row2[2] = (String) gradeinput1.getSelectedItem();
status2.setText("Status: Added");
}
.....
您的actionPerformed
方法if (e.getSource() == add1)
永远不会为true
因为JButton add1
始终为null
,因为addButton()
方法从未初始化变量add1
因此,您必须声明JButton add1 = new JButton();
作为类变量,并如下更改addButton
方法
public void addButton(JButton b, String s) {
b.setText("ADD");
b.setActionCommand(s);
b.addActionListener(this);
panel2.add(b);
}
也对removeButton
方法应用相同的更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.