我正在使用通过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);
}
}
`
最佳答案
尽管您的问题不清楚您要如何处理第二个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+"%");