Java老师您好,我是一名JAVA初学者。现编写了一个小程序。求老师指点。

2024-12-22 14:22:45
推荐回答(1个)
回答1:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Window extends JFrame implements ActionListener{
private int number;
private JLabel L1, L2, L3, L4, L5, L6, L7, L8;
private JTextField T1, T2, T3, T4, T5, T6, T7, T8;
private JButton buttonEnter;
Window(String title){
super(title);
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

L1 = new JLabel("L", JLabel.CENTER); L1.setBackground(Color.cyan);L1.setPreferredSize(new Dimension(60, 30));
T1 = new JTextField("2", 38);
JPanel pnl1=new JPanel();
pnl1.setLayout(new BorderLayout());
pnl1.add(L1, BorderLayout.WEST);
pnl1.add(T1, BorderLayout.EAST);
add(pnl1);

L2 = new JLabel("L", JLabel.CENTER); L2.setBackground(Color.cyan);L2.setPreferredSize(new Dimension(60, 30));
T2 = new JTextField("2", 38);
JPanel pnl2=new JPanel();
pnl2.setLayout(new BorderLayout());
pnl2.add(L2, BorderLayout.WEST);
pnl2.add(T2, BorderLayout.EAST);
add(pnl2);

L3 = new JLabel("L", JLabel.CENTER); L3.setBackground(Color.cyan);L3.setPreferredSize(new Dimension(60, 30));
T3 = new JTextField("2", 38);
JPanel pnl3=new JPanel();
pnl3.setLayout(new BorderLayout());
pnl3.add(L3, BorderLayout.WEST);
pnl3.add(T3, BorderLayout.EAST);
add(pnl3);

L4 = new JLabel("L", JLabel.CENTER); L4.setBackground(Color.cyan);L4.setPreferredSize(new Dimension(60, 30));
T4 = new JTextField("2", 38);
JPanel pnl4=new JPanel();
pnl4.setLayout(new BorderLayout());
pnl4.add(L4, BorderLayout.WEST);
pnl4.add(T4, BorderLayout.EAST);
add(pnl4);

L5 = new JLabel("L", JLabel.CENTER); L5.setBackground(Color.cyan);L5.setPreferredSize(new Dimension(60, 30));
T5 = new JTextField("2", 38);
JPanel pnl5=new JPanel();
pnl5.setLayout(new BorderLayout());
pnl5.add(L5, BorderLayout.WEST);
pnl5.add(T5, BorderLayout.EAST);
add(pnl5);

L6 = new JLabel("L", JLabel.CENTER); L6.setBackground(Color.cyan);L6.setPreferredSize(new Dimension(60, 30));
T6 = new JTextField("2", 38);
JPanel pnl6=new JPanel();
pnl6.setLayout(new BorderLayout());
pnl6.add(L6, BorderLayout.WEST);
pnl6.add(T6, BorderLayout.EAST);
add(pnl6);

L7 = new JLabel("L", JLabel.CENTER); L7.setBackground(Color.cyan);L7.setPreferredSize(new Dimension(60, 30));
T7 = new JTextField("2", 38);
JPanel pnl7=new JPanel();
pnl7.setLayout(new BorderLayout());
pnl7.add(L7, BorderLayout.WEST);
pnl7.add(T7, BorderLayout.EAST);
add(pnl7);

buttonEnter = new JButton("Count");
JPanel pnlBtn=new JPanel();
pnlBtn.setLayout(new BorderLayout());
pnlBtn.add(buttonEnter);
add(pnlBtn);
buttonEnter.addActionListener(this);
setBounds(0, 0, 500, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}

private float C,g1,g2,g11,g22,t1,t2;
private int s1, s2, y1, y2, I,A, l;
public void actionPerformed(ActionEvent e){
if(e.getSource()==buttonEnter){

rull();

if (y1 * 3600 / s1 + y2 * 3600 / s2 > 0.9){
L1.setText("Q>0.9");
L2.setText("Q>0.9");
L3.setText("Q>0.9");
L4.setText("Q>0.9");
L5.setText("Q>0.9");
L6.setText("Q>0.9");
L7.setText("Q>0.9");
}else{
L1.setText("" + C);
L2.setText("" + g1);
L3.setText("" + g2);
L4.setText("" + g11);
L5.setText("" + g22);
L6.setText("" + t1);
L7.setText("" + t2);
}
}
}

private void rull(){
int y1 = Integer.parseInt(T1.getText());
s1 = Integer.parseInt(T2.getText());
y2 = Integer.parseInt(T3.getText());
s2 = Integer.parseInt(T4.getText());
I = Integer.parseInt(T5.getText());
A = Integer.parseInt(T6.getText());
l = Integer.parseInt(T7.getText());

C = (3 * (l + I - A) + 5) / (1 - y1 * 3600 / s1 - y2 * 3600 / s2);
g1 = (C - 2 * (l + I - A)) * (y1 * 3600 / s1 / (y1 * 3600 / s1 + y2 * 3600 / s2)) + l - A;
g2 = (C - 2 * (l + I - A)) * (y2 * 3600 / s2 / (y1 * 3600 / s1 + y2 * 3600 / s2)) + l - A;
g11 = (g1 - l + A) / C;
g22 = (g2 - l + A) / C;
t1 = C*(1 - g11) * (1 - g11) / 2 * (1 - y1 * 3600 / s1);
t2 = C*(1 - g22) * (1 - g22) / 2 * (1 - y2 * 3600 / s2);
}
}

public class Cont {
public static void main(String args[]){
Window win=new Window("TY");
}
}