所以我必须要做一个类项目来制作这个程序,但是我想让它在JFrame
中工作,所以它有自己的GUI。以下是我对该计划的看法。
package college.rhys.test;
import java.util.Scanner;
public class trial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Grade format is ppp,mpp,mmm,dmm,ddd:");
System.out.println("Please enter Btec Grade:");
String Kboard = input.nextLine();
if(Kboard.equals("ppp")){
System.out.println("You have 120 UCAS points");
} else if(Kboard.equals("mpp")){
System.out.println("You have 160 UCAS points!");
} else if(Kboard.equals("mmm")){
System.out.println("You have 240 UCAS points!");
} else if(Kboard.equals("dmm")){
System.out.println("You have 280 UCAS points!");
} else if(Kboard.equals("ddd")){
System.out.println("You have 360 UCAS points!");
} }
}
答案 0 :(得分:0)
从文本框中获取成绩,然后将其存储在字符串变量
中如果您的String等于定义的字符,请显示带有点的消息框。
首先,你需要一个框架,然后添加文本框,按钮。(你也可以使用面板)
点击按钮,您需要获取文本框值并进行比较。
根据比较结果,显示您的消息框。
Here您可以找到使用带有文本框和按钮的框架的基本教程
答案 1 :(得分:0)
这是我最终的结果
公共类Gui扩展了JFrame {
private JTextField gradeanswer;
private JButton ppp, mpp, mmm, dmm, ddd, reset;
private String sanswer;
private double answer = 00;
private JPanel contentpanel;
boolean opchosen = false;
public Gui(){
super("grade calculator");
gradeanswer = new JTextField(null, 20);
ppp = new JButton("ppp");
mpp = new JButton("mpp");
mmm = new JButton("mmm");
dmm = new JButton("dmm");
ddd = new JButton("ddd");
reset = new JButton("Reset");
Dimension dim = new Dimension(75,75);
ppp.setPreferredSize(dim); mpp.setPreferredSize(dim); mmm.setPreferredSize(dim); dmm.setPreferredSize(dim);
ddd.setPreferredSize(dim);reset.setPreferredSize(dim);
grade g = new grade();
ppp.addActionListener(g); dmm.addActionListener(g);
mpp.addActionListener(g); ddd.addActionListener(g);
mmm.addActionListener(g); reset.addActionListener(g);
contentpanel = new JPanel();
contentpanel.setBackground(Color.white);
contentpanel.setLayout(new FlowLayout());
contentpanel.add(gradeanswer, BorderLayout.NORTH);
gradeanswer.setEditable(false);
contentpanel.add(ppp); contentpanel.add(mpp); contentpanel.add(mmm);
contentpanel.add(dmm); contentpanel.add(ddd); contentpanel.add(reset);
JLabel label1 = new JLabel("Btec To Ucas Converer");
contentpanel.add(label1, BorderLayout.SOUTH);
label1.setPreferredSize(new Dimension(150, 100));
this.setContentPane(contentpanel);
}
private class grade implements ActionListener{
public void actionPerformed(ActionEvent Event) {
JButton src = (JButton) Event.getSource();
if (src.equals(ppp)){
if(opchosen == false){
if (sanswer== null){
sanswer = "You have 120 UCAS points!";
}}}
if (src.equals(mpp)){
if(opchosen == false){
if (sanswer== null){
sanswer = "You have 160 UCAS points!";
}}}
if (src.equals(mmm)){
if(opchosen == false){
if (sanswer== null){
sanswer = "You have 240 UCAS points!";
}}}
if (src.equals(dmm)){
if(opchosen == false){
if (sanswer== null){
sanswer = "You have 280 UCAS points!";
}}}
if (src.equals(ddd)){
if(opchosen == false){
if (sanswer== null){
sanswer = "You have 360 UCAS points!";
}}}
if (src.equals(reset)){
if(opchosen == false){
if (sanswer!= null){
gradeanswer.setText("");
}}}
if (opchosen == false){
gradeanswer.setText(sanswer);
}
if (src.equals(reset)){
if(opchosen == false){
if (sanswer!= null){
gradeanswer.setText("");
sanswer = null;
}}}
}
}
}