我得到了一个JFrame游戏作为学校的项目,有一个按钮,这导致了第二个JFrame,这是设置框架。我现在希望我的设置应用到我的主JFrame。我该怎么做?
主JFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.math.*;
import java.util.Random;
import java.awt.geom.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.sound.*;
public class SpiceWars4 extends JFrame {
// Anfang Attribute
private JList jList1 = new JList();
private DefaultListModel jList1Model = new DefaultListModel();
private JScrollPane jList1ScrollPane = new JScrollPane(jList1);
private JList jList2 = new JList();
private DefaultListModel jList2Model = new DefaultListModel();
private JScrollPane jList2ScrollPane = new JScrollPane(jList2);
private JList jList3 = new JList();
private DefaultListModel jList3Model = new DefaultListModel();
private JScrollPane jList3ScrollPane = new JScrollPane(jList3);
private JLabel lSchwarzmarkt = new JLabel();
private JLabel lLaderaum = new JLabel();
private JButton bKaufen = new JButton();
private JButton bVerkaufen = new JButton();
private JNumberField jNumberField1 = new JNumberField();
private JButton bWeiterreisen = new JButton();
public int radius;
private JButton jButton1 = new JButton();
private JButton jtest = new JButton();
// Ende Attribute
int [] KostenMin = new int [5];
int [] KostenMax = new int [5];
//**Grunddinger Anfang
int Geld, Laderaum;
String [] Gewürze = new String [5];
int [] aktKosten = new int [5];
int [] EigeneLadung = new int [5];
Random rand = new Random();
//**Grunddinger Ende
public SpiceWars4() {
// Frame-Initialisierung
super();
//String filepath = "song.wav"; //wählt song aus
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 865;
int frameHeight = 698;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setTitle("SpiceWars");
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
cp.setBackground(new Color(0x666666));
// Anfang Komponenten
jList1.setModel(jList1Model);
jList1ScrollPane.setBounds(24, 48, 302, 228);
jList1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jList1_MouseClicked(evt);
}
});
jList1.setBackground(new Color(0x646464));
cp.add(jList1ScrollPane);
jList2.setModel(jList2Model);
jList2ScrollPane.setBounds(504, 48, 278, 260);
jList2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jList2_MouseClicked(evt);
}
});
jList2.setBackground(new Color(0x646464));
cp.add(jList2ScrollPane);
jList3.setModel(jList3Model);
jList3ScrollPane.setBounds(24, 296, 142, 220);
jList3.setBackground(new Color(0x646464));
cp.add(jList3ScrollPane);
lSchwarzmarkt.setBounds(104, 8, 91, 28);
lSchwarzmarkt.setText("Markt ");
lSchwarzmarkt.setForeground(Color.WHITE);
cp.add(lSchwarzmarkt);
lLaderaum.setBounds(614, 8, 62, 28);
lLaderaum.setText("Laderaum ");
lLaderaum.setForeground(Color.WHITE);
cp.add(lLaderaum);
bKaufen.setBounds(368, 88, 83, 33);
bKaufen.setText(">>Kaufen ");
bKaufen.setMargin(new Insets(2, 2, 2, 2));
bKaufen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bKaufen_ActionPerformed(evt);
}
});
bKaufen.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
bKaufen_KeyPressed(evt);
}
});
bKaufen.setBackground(new Color(0x4F9B9B));
cp.add(bKaufen);
bVerkaufen.setBounds(368, 136, 83, 33);
bVerkaufen.setText("<<Verkaufen ");
bVerkaufen.setMargin(new Insets(2, 2, 2, 2));
bVerkaufen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bVerkaufen_ActionPerformed(evt);
}
});
bVerkaufen.setBackground(new Color(0x4F9B9B));
cp.add(bVerkaufen);
jNumberField1.setBounds(368, 48, 83, 28);
jNumberField1.setText("");
jNumberField1.setBackground(Color.GRAY);
cp.add(jNumberField1);
bWeiterreisen.setBounds(126, 570, 83, 33);
bWeiterreisen.setText("Weiterreisen");
bWeiterreisen.setMargin(new Insets(2, 2, 2, 2));
bWeiterreisen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bWeiterreisen(evt);
}
});
bWeiterreisen.setBackground(new Color(0x008080));
cp.add(bWeiterreisen);
bWeiterreisen.setBounds(96, 568, 136, 33);
bWeiterreisen.setBounds(32, 536, 136, 33);
jButton1.setBounds(712, 600, 112, 24);
jButton1.setText("Einstellungen");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
cp.add(jButton1);
jtest.setBounds(736, 536, 80, 24);
jtest.setText("Button");
jtest.setMargin(new Insets(2, 2, 2, 2));
jtest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jtest_ActionPerformed(evt);
}
});
cp.add(jtest);
// Ende Komponenten
//**Eigene Variabelen Anfang
Geld = 10000;
Laderaum = 1000 ;
Gewürze[0] = "Pfeffer";
Gewürze[1] = "Muskat";
Gewürze[2] = "Curry";
Gewürze[3] = "Vanille";
Gewürze[4] = "Zimt";
aktKosten[0] = 1500;
aktKosten[1] = 5000;
aktKosten[2] = 900;
aktKosten[3] = 1400;
aktKosten[4] = 650;
EigeneLadung[0] = 0;
EigeneLadung[1] = 0;
EigeneLadung[2] = 0;
EigeneLadung[3] = 0;
EigeneLadung[4] = 0;
//**Eigene Variablen Ende
//**Preisrandomisen Weiterreisen Anfang
KostenMin[0] = 1300;
KostenMin[1] = 4250;
KostenMin[2] = 725;
KostenMin[3] = 1125;
KostenMin[4] = 400;
KostenMax[0] = 1700;
KostenMax[1] = 6250;
KostenMax[2] = 1150;
KostenMax[3] = 1625;
KostenMax[4] = 800;
//**Preisrandomisen Weiterreisen Ende
Datenanzeigen();
setVisible(true);
} // end of public SpiceWars4
// Anfang Methoden
public static void main(String[] args) {
new SpiceWars4();
} // end of main
//**Kaufenbutton Anfang
public void bKaufen_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
int Anzahl = Integer.parseInt(jNumberField1.getText());
int Nummer = jList1.getSelectedIndex();
if (Anzahl*aktKosten[Nummer] <= Geld) {
Geld = Geld - Anzahl * aktKosten[Nummer];
EigeneLadung[Nummer] = EigeneLadung[Nummer] + Anzahl;
} // end of if
Datenanzeigen();
} // end of bKaufen_ActionPerformed
//**Kaufenbutton Ende
//**Vekaufsbutton Anfang
public void bVerkaufen_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
int Anzahl = Integer.parseInt(jNumberField1.getText());
int Nummer = jList2.getSelectedIndex();
if (Anzahl <= EigeneLadung [Nummer]) {
Geld = Geld + Anzahl * aktKosten[Nummer];
EigeneLadung[Nummer] = EigeneLadung[Nummer]-Anzahl;
} // end of if
Datenanzeigen();
} // end of bVerkaufen_ActionPerformed
//**Vekaufsbutton Ende
public void Datenanzeigen(){
jList1Model.removeAllElements();
jList2Model.removeAllElements();
jList3Model.removeAllElements();
for (int i = 0; i < 5; i++) {
jList1Model.addElement(Gewürze[i] + " kostet: " + aktKosten[i] + " Münzen ");
}
for (int i = 0; i < 5; i++) {
jList2Model.addElement(Gewürze[i] + " geladen: " + EigeneLadung[i] + " Stück ");
}
jList2Model.addElement("----------") ;
jList2Model.addElement("Geld: " + Geld);
jList3Model.addElement("Mendig");
jList3Model.addElement("Mayen");
jList3Model.addElement("Kruft");
jList3Model.addElement("Thür");
jList3Model.addElement("Bell");
}
public void jList1_MouseClicked(MouseEvent evt) {
// TODO hier Quelltext einfügen
int Index = jList1.getSelectedIndex();
jNumberField1.setText(Integer.toString((Geld / aktKosten[Index])));
} // end of jList1_MouseClicked
public void jList2_MouseClicked(MouseEvent evt) {
// TODO hier Quelltext einfügen
int Index = jList1.getSelectedIndex();
jNumberField1.setText(Integer.toString(EigeneLadung[Index]));
} // end of jList2_MouseClicked
//**weiterreisen von Welt zu Welt Anfang
public void bWeiterreisen(ActionEvent evt) {
// TODO hier Quelltext einfügen
for (int i = 0; i < 5; i++) {
aktKosten[i] = KostenMin[i] + rand.nextInt(KostenMax[i] - KostenMin[i]);
}
Datenanzeigen();
} // end of bWeiterreisen_ActionPerformed
//**Weiterreisen von Welt zu Welt Ende
public void bKaufen_KeyPressed(KeyEvent evt) {
// TODO hier Quelltext einfügen
} // end of bKaufen_KeyPressed
public void jButton1_ActionPerformed(ActionEvent evt) {
JFrame2 jf2 = new JFrame2(); //initalisierung neues jframe
jf2.setVisible(true);
dispose(); //Schließt das game
} // end of jButton1_ActionPerformed
public void jtest_ActionPerformed(ActionEvent evt) {
} // end of jtest_ActionPerformed
// Ende Methoden
} // end of class Spielprogrammieren
设置JFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.math.*;
import java.util.Random;
import java.awt.geom.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.sound.*;
public class JFrame2 extends JFrame {
// Anfang Attribute
private JButton jButton1 = new JButton();
private JScrollPane jScrollPane1 = new JScrollPane();
private JButton jmute = new JButton();
private JLabel jLabel1 = new JLabel();
private JButton bDarkMode1 = new JButton();
private JButton bLightMode1 = new JButton();
private JPanel jDifficulty = new JPanel();
private JScrollPane jScrollPane2 = new JScrollPane();
private ButtonGroup jDifficultyBG = new ButtonGroup();
private TitledBorder jDifficultyTB = new TitledBorder("Schwierigkeit");
private JRadioButton jDifficultyRB0 = new JRadioButton("Schwer");
private JRadioButton jDifficultyRB1 = new JRadioButton("Mittel");
private JRadioButton jDifficultyRB2 = new JRadioButton("Leicht");
// Ende Attribute
public JFrame2() {
// Frame-Initialisierung
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 373;
int frameHeight = 517;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setTitle("Einstellungen");
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
cp.setBackground(new Color(0x666666));
// Anfang Komponenten
jButton1.setBounds(104, 440, 160, 24);
jButton1.setText("Zurück");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
jButton1.setBackground(new Color(0x49969E));
cp.add(jButton1);
jScrollPane1.setBounds(696, -16, 1, 17);
cp.add(jScrollPane1);
jmute.setBounds(120, 160, 128, 24);
jmute.setText("Musik aus");
jmute.setMargin(new Insets(2, 2, 2, 2));
jmute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jmute_ActionPerformed(evt);
}
});
jmute.setBackground(new Color(0x49969E));
jmute.setRolloverEnabled(false);
cp.add(jmute);
jLabel1.setBounds(112, 24, 328, 47);
jLabel1.setText("Einstellungen");
jLabel1.setFont(new Font("Dialog", Font.BOLD, 24));
cp.add(jLabel1);
bDarkMode1.setBounds(144, 216, 80, 24);
bDarkMode1.setText("Dark Mode");
bDarkMode1.setMargin(new Insets(2, 2, 2, 2));
bDarkMode1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bDarkMode1_ActionPerformed(evt);
}
});
bDarkMode1.setBackground(Color.BLACK);
bDarkMode1.setForeground(Color.WHITE);
cp.add(bDarkMode1);
bLightMode1.setBounds(144, 272, 80, 24);
bLightMode1.setText("Light Mode");
bLightMode1.setMargin(new Insets(2, 2, 2, 2));
bLightMode1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
bLightMode1_ActionPerformed(evt);
}
});
bLightMode1.setBackground(Color.WHITE);
bLightMode1.setForeground(new Color(0x060606));
cp.add(bLightMode1);
jDifficulty.setLayout(null);
jDifficulty.setBounds(128, 336, 120, 80);
jDifficulty.setBackground(new Color(0x666666));
jDifficulty.setForeground(new Color(0x666666));
jDifficultyRB0.setBounds(7, 18, 106, 20);
jDifficultyRB0.setSelected(true);
jDifficultyBG.add(jDifficultyRB0);
jDifficulty.add(jDifficultyRB0);
jDifficultyRB1.setBounds(7, 37, 106, 19);
jDifficultyBG.add(jDifficultyRB1);
jDifficulty.add(jDifficultyRB1);
jDifficultyRB2.setBounds(7, 56, 106, 19);
jDifficultyBG.add(jDifficultyRB2);
jDifficulty.add(jDifficultyRB2);
jDifficulty.setBorder(jDifficultyTB);
cp.add(jDifficulty);
jScrollPane2.setBounds(352, -16, 1, 17);
cp.add(jScrollPane2);
// Ende Komponenten
setVisible(true);
} // end of public JFrame2
// Anfang Methoden
public static void main(String[] args) {
new JFrame2();
} // end of main
public void jButton1_ActionPerformed(ActionEvent evt) {
SpiceWars4 jf1 = new SpiceWars4(); //initalisierung neues jframe
jf1.setVisible(true);
dispose(); //Schließt die Einstellungen
} // end of jButton1_ActionPerformed
public void jmute_ActionPerformed(ActionEvent evt) {
} // end of jmute_ActionPerformed
public void bDarkMode1_ActionPerformed(ActionEvent evt) {
Container cp = getContentPane();
if (evt.getSource()==bDarkMode1) {
jButton1.setBackground(new Color(0x49969E));
jButton1.setForeground(new Color(0x333333));
cp.setBackground(new Color(0x666666));
jmute.setBackground(new Color(0x49969E));
jDifficulty.setBackground(new Color(0x666666));
jDifficulty.setForeground(new Color(0x666666));
} // end of if
} // end of bDarkMode1_ActionPerformed
public void bLightMode1_ActionPerformed(ActionEvent evt) {
Container cp = getContentPane();
if (evt.getSource()==bLightMode1) {
jButton1.setBackground(Color.WHITE);
jButton1.setForeground(new Color(0x666666));
jDifficulty.setBackground(Color.WHITE);
jmute.setBackground(Color.WHITE);
cp.setBackground(Color.WHITE);
} // end of if
} // end of bLightMode1_ActionPerformed
public String jDifficultyBG_getSelectedButtonGroupLabel() {
for (java.util.Enumeration<AbstractButton> e = jDifficultyBG.getElements(); e.hasMoreElements();) {
AbstractButton b = e.nextElement();
if (b.isSelected()) return b.getText();
}
return "";
}
// Ende Methoden
} // end of class JFrame2
例如,当用户单击LightMode按钮时,它也应该应用于主Jframe,与DarkMode按钮相同。
会很感激你的帮助,因为我还是个初学者。
1条答案
按热度按时间flseospp1#
这听起来像是您需要observer pattern,当某个事件发生在某个地方时,您希望您的第一个JFrame对其作出React。
此解决方案需要Java 7或更高版本
用一个单独的类来维护设置的状态是有意义的。这个实现是一个singleton,它对你的实现来说足够好用了,尽管随着你的学习会有更好的选择。
这个设置对象将允许任何
PropertyChangeListener
订阅它,并在属性更改时得到通知。JFrame 1可以通过实现接口和订阅Settings.addPropertyChangeListener()
方法成为PropertyChangeListener
。然后是JFrame 2中在单击按钮时实际设置属性的代码:
设置屏幕模式将触发一个属性更改事件,该事件将执行所有订阅者(JFrame 1)的
propertyChange(PropertyEvent)
方法,然后可以根据需要处理或忽略该事件。