我正在使用通过Eclipse开发的GUI制作一个简单的Grading系统。如何在另一个JFrame上输出average
和Name
?
我想将计算出的平均值从第一个JFrame移动到第二个JFrame,我还想在第二个JFrame上输出一条消息:
"Hi," + the name entered + your average grade is: .
所以第二个JFrame的输出基本上是这样的:
Hi Bahramdun Adil! Your Average is: 99%
您可以运行我创建的代码来帮助您理解。
这是代码:`package gradesystem;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.TextField;
import java.awt.BorderLayout;
import java.awt.Label;
import java.awt.TextArea;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Frame1 {
public JFrame frame;
public JTextField textField;
public JTextField prelim;
public JTextField midterm;
public JTextField prefinal;
public JTextField pinal;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 495, 311);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBackground(Color.WHITE);
textField.setBounds(136, 58, 256, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("Enter Name");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
lblNewLabel.setBounds(25, 60, 101, 17);
frame.getContentPane().add(lblNewLabel);
JLabel lblEnterPrelimGrade = new JLabel("Enter Prelim Grade");
lblEnterPrelimGrade.setFont(new Font("Tahoma", Font.BOLD, 11));
lblEnterPrelimGrade.setBounds(25, 119, 101, 17);
frame.getContentPane().add(lblEnterPrelimGrade);
prelim = new JTextField();
prelim.setColumns(10);
prelim.setBounds(136, 117, 55, 20);
frame.getContentPane().add(prelim);
midterm = new JTextField();
midterm.setColumns(10);
midterm.setBounds(136, 148, 55, 20);
frame.getContentPane().add(midterm);
prefinal = new JTextField();
prefinal.setColumns(10);
prefinal.setBounds(136, 179, 55, 20);
frame.getContentPane().add(prefinal);
pinal = new JTextField();
pinal.setColumns(10);
pinal.setBounds(136, 210, 55, 20);
frame.getContentPane().add(pinal);
JLabel lblEnterMidtermGrade = new JLabel("Enter Midterm Grade");
lblEnterMidtermGrade.setFont(new Font("Tahoma", Font.BOLD, 11));
lblEnterMidtermGrade.setBounds(25, 150, 101, 17);
frame.getContentPane().add(lblEnterMidtermGrade);
JLabel lblEnterPrefinalGrade = new JLabel("Enter Pre-final Grade");
lblEnterPrefinalGrade.setFont(new Font("Tahoma", Font.BOLD, 11));
lblEnterPrefinalGrade.setBounds(25, 181, 101, 17);
frame.getContentPane().add(lblEnterPrefinalGrade);
JLabel lblEnterFinalGrade = new JLabel("Enter Final Grade");
lblEnterFinalGrade.setFont(new Font("Tahoma", Font.BOLD, 11));
lblEnterFinalGrade.setBounds(25, 212, 101, 17);
frame.getContentPane().add(lblEnterFinalGrade);
JLabel label = new JLabel("20%");
label.setBounds(198, 119, 31, 17);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("20%");
label_1.setBounds(198, 150, 31, 17);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("20%");
label_2.setBounds(198, 181, 31, 17);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("40%");
label_3.setBounds(198, 212, 31, 17);
frame.getContentPane().add(label_3);
JButton btnNewButton = new JButton("Click to get total Average!");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int first;
int second;
int third;
int fourth;
double grade;
first=Integer.parseInt(prelim.getText());
second=Integer.parseInt(midterm.getText());
third=Integer.parseInt(prefinal.getText());
fourth=Integer.parseInt(pinal.getText());
grade=first*.2+second*.2+third*.2+fourth*.4;
NumberFormat formatter = new DecimalFormat("#.##");
String grado=formatter.format(grade);
Frame2 s2= new Frame2();
JTextField lblgrade = new JTextField(grado);
lblgrade.setEditable(false);
lblgrade.setBounds(302, 204, 30, 26);
frame.getContentPane().add(lblgrade);
lblgrade.setColumns(10);
}
});
btnNewButton.setBackground(Color.LIGHT_GRAY);
btnNewButton.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
btnNewButton.setBounds(255, 119, 184, 51);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel_1 = new JLabel("GRADING SYSTEM");
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 18));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setBounds(26, 11, 397, 36);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblAverage = new JLabel("Average:");
lblAverage.setFont(new Font("Tahoma", Font.BOLD, 11));
lblAverage.setBounds(293, 182, 80, 14);
frame.getContentPane().add(lblAverage);
JLabel label_4 = new JLabel("%");
label_4.setBounds(342, 210, 27, 14);
frame.getContentPane().add(label_4);
}
}
`
答案 0 :(得分:2)
虽然您的问题不清楚您要对第二个JFrame
做什么,但是您想要打开第二个JFrame
,在这种情况下,Frame1
就像您的例子中一样代码,然后在下面添加您想要打开第二个JFrame
private static void luanch() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
在主method
public static void main(String[] args) {
luanch();
}
还有你要打开第二个JFrame
grade=first*.2+second*.2+third*.2+fourth*.4;
NumberFormat formatter = new DecimalFormat("#.##");
String grado=formatter.format(grade);
//Frame2 s2= new Frame2();
luanch(); // here
JTextField lblgrade = new JTextField(grado);
注意:如果您只想显示消息,那么您可以使用消息对话框,如下所示:
JOptionPane.showMessageDialog(frame, "Hi! "+textField.getText()+" Your average is "+grado+"%");