두 개 이상의 클래스에서 조건부로 할당 된 값을 사용하는 데 문제가 있습니다. 조건이 충족되는 경우에만 한 클래스의 값 사용을 적용한 다음 일부 다른 클래스에서 값을 합하여 총 점수를 합산합니다. 이건 내 코드 야!다른 클래스에서 조건부로 할당 된 값을 얻는 방법
@SuppressWarnings("serial")
public class Beginner extends JPanel {
static JButton quest;
Random rand = new Random();
int n = 10;
static List <Point> points = new ArrayList<Point>();
int scores;
int fails;
public Beginner() {
int radius = 200;
Point center = new Point (250, 250);
double angle = Math.toRadians(360/n);
points.add(center);
for (int i = 0; i < n; i++) {
double theta = i * angle;
int dx = (int) (radius * Math.sin(theta));
int dy = (int) (radius * Math.cos(theta));
Point p = new Point (center.x + dx , center.y + dy);
points.add(p);
}
draw (points);
}
public void draw (List<Point> points) {
JPanel panels = new JPanel();
SpringLayout spring = new SpringLayout();
int count = 1;
for (Point point: points) {
quest = new JButton("Question " + count);
quest.setForeground(Color.BLACK);
Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
quest.setFont(fonte);
add (quest);
count++;
spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels);
spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels);
setLayout(spring);
panels.setOpaque(false);
panels.setVisible(true);
panels.setLocation(5,5);
add(panels);
quest.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed (ActionEvent p) {
if (point.equals(points.get(0))) {
new Quest1();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(1))) {
new Quest2();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(2))) {
new Quest3();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(3))) {
new Quest4();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(4))) {
new Quest5();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
이제이 (Quest1 이름) 처음 호출 된 클래스는
public class Quest1 {
//sound files to be shuffled and played!
String [] word = { "C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/audio.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/bomb.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/baby.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/gym.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/hearing.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/goal.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/manifest.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/mountain.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/market.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/debate.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/election.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/number.wav",
};
//matching strings for each file
String [] words = { "Audio", "Bomb", "Baby", "Gym", "Hearing", "Goal", "Manifest","Mountain", "Market", "Debate", "Election", "Number" };
Random rand = new Random();
int random = rand.nextInt(word.length);
String temp = word[random];
int scores;
int fails;
public Quest1() {
//announcing the JComponents to be used
JFrame frame = new JFrame();
JButton click = new JButton("Play");
JTextField type = new JTextField(15);
JLabel pic = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\Sample pics\\1.png"));
JButton score = new JButton ("Check My Answer");
JPanel cont = new JPanel();
//set up frame properties
frame.getContentPane().setBackground(Color.WHITE);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setResizable(false);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.add(cont, BorderLayout.CENTER);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
frame.setVisible(true);
//setting layout of JPanel
cont.setLayout(new GridBagLayout());
cont.setOpaque(false);
cont.setPreferredSize(new Dimension(500,500));
cont.setLocation(100, 50);
GridBagConstraints g = new GridBagConstraints();
g.anchor = GridBagConstraints.WEST;
g.gridx = 4;
g.gridy = 2;
g.gridwidth = 2;
g.insets = new Insets (70, 2, 2, 2);
cont.add(pic , g);
g.anchor = GridBagConstraints.WEST;
g.gridx = 4;
g.gridy = 5;
g.gridwidth = 2;
g.insets = new Insets (50, 2, 2, 2);
cont.add(click, g);
click.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e) {
//sound file shuffled and played
}
});
g.anchor = GridBagConstraints.EAST;
g.gridx = 10;
g.gridy = 4;
g.gridwidth = 5;
g.insets = new Insets(30, 2, 2, 10);
Font fonty = new Font("Lucida", Font.PLAIN, 15);
type.setFont(fonty);
cont.add(type, g);
g.anchor = GridBagConstraints.SOUTH;
g.gridx = 9;
g.gridy = 8;
g.gridwidth = 2;
g.insets = new Insets(2, 2, 2, 50);
cont.add(score, g);
score.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent tx) {
if (type.getText().equals(words[random])) {
//sound file played
scores+= 3;
String a = "Correct!..... The answer is " + words[random];
JOptionPane.showMessageDialog(null, a);
frame.dispose();
return;
}
else {
//sound file played again
fails+=0;
// This is where my problem is: The other 10 classes are like
//this, I want to get this value and that of scores (depending on the
//condition that is met), and add them up in the class beginner!)
String a = "Sorry, The Answer is " + words[random];
JOptionPane.showMessageDialog(null, a);
frame.dispose();
}
}
});
//sound file played
}
}
나는 그것에 약간의 코드가 필요하다! –
코드 논리는 괜찮은 것 같지만 .... 적용하는데 어려움이 있습니다! .... 세 번째 코드 조각은 otherclass.process (점수, 실패)를 읽습니다. 적용 할 때 오류가 발생합니다! 나는 이미 점수를 만들었고 각 반에서 실패했지만, 효과가 없습니다! –
네가 정보를 전달하고자하는 수업에서 내가 가진 이름과 방법을 모르겠다. "otherclass"를 정보를 전송하려는 클래스로 바꿔야하며, "처리"는 점수 및 실패 값을 사용하여 전달할 방법으로 대체 할 수 있습니다. – Brion