嗨我正在做一个程序,首先打开图片中的设置菜单 enter image description here
首先我从jcombox和jdialog中选择我想要的游戏,打开窗口设置名称。
这是它的代码:
public class SettingMenu extends JFrame {
boolean is3players = false, is4players = false;
BufferedImage settingImage;
String[] playersChoises = { "2", "3", "4" };
String[] sizeChoises = { "30", "50", "100" };
JComboBox comboBoxPlayers;
JComboBox comboBoxSizes;
static JButton startGame, writeNames;
public SettingMenu() {
JLabel howManyPlayersText = new JLabel("How many players ?");
comboBoxPlayers = new JComboBox(playersChoises);
if (comboBoxPlayers.getSelectedItem().equals("3")) {
is3players = true;
}
if (comboBoxPlayers.getSelectedItem().equals("4")) {
is3players = true;
is4players = true;
}
JLabel writeNamesText = new JLabel("Set names of playes");
writeNames = new JButton("set names");
JLabel sizeOfBoredText = new JLabel("What the size of the bored ?");
comboBoxSizes = new JComboBox(sizeChoises);
startGame = new JButton("Click to start the game!");
howManyPlayersText.setBounds(177, 200, 270, 100);
comboBoxPlayers.setBounds(230, 270, 100, 30);
writeNamesText.setBounds(230, 210, 380, 250);
writeNames.setBounds(240, 350, 100, 36);
sizeOfBoredText.setBounds(177, 376, 300, 100);
comboBoxSizes.setBounds(230, 450, 100, 30);
startGame.setBounds(200, 500, 200, 44);
add(howManyPlayersText);
add(comboBoxPlayers);
add(writeNamesText);
add(writeNames);
add(sizeOfBoredText);
add(comboBoxSizes);
add(startGame);
setVisible(true);
}
public static void main(String[] args) {
// open this class
new SettingMenu();
// when i click on startgame bottun open the class of the game
startGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new LeadersAndSnake_Project201();
}
});
//当我点击writeNames时,打开对话框
writeNames.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetNames();
}
});
}
}
这是Set Names类,用于打开set names窗口 enter image description here
class SetNames extends JDialog {
public JTextField setNamePlayer1, setNamePlayer2, setNamePlayer3, setNamePlayer4;
public SetNames() {
this.setSize(280, 151);
this.setLocationRelativeTo(null);
JLabel name1 = new JLabel("Set Player's 1 name : ");
setNamePlayer1 = new JTextField(7);
setNamePlayer1.setText("Player 1");
JLabel name2 = new JLabel("Set Player's 2 name : ");
setNamePlayer2 = new JTextField(7);
setNamePlayer2.setText("Player 2");
JPanel panelOfDialog_1 = new JPanel();
panelOfDialog_1.add(name1);
panelOfDialog_1.add(setNamePlayer1);
JPanel panelOfDialog_2 = new JPanel();
panelOfDialog_2.add(name2);
panelOfDialog_2.add(setNamePlayer2);
JPanel panelOfDialog_3 = new JPanel();
JButton okBotton = new JButton("OK");
add(okBotton);
okBotton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setNamePlayer1 = new JTextField(setNamePlayer1.getText());
setNamePlayer2 = new JTextField(setNamePlayer2.getText());
setVisible(false);
}
});
panelOfDialog_3.add(okBotton);
add(panelOfDialog_1, BorderLayout.NORTH);
add(panelOfDialog_2, BorderLayout.CENTER);
add(panelOfDialog_3, BorderLayout.AFTER_LAST_LINE);
this.setVisible(true);
}
}
这是游戏的大型课程,但我只是把重要的东西放在这里:
class LeadersAndSnake_Project201 extends JFrame implements ActionListener{
// Here I made an object of Setting Menu class for use the varible that it has
SettingMenu obj1 = new SettingMenu();
// Here I made an object of Set Names class to take the name inside the textfiled
SetNames obj2 = new SetNames();
public LeadersAndSnake_Project201() {
// -----------------left panel------------------------------
JPanel leftPanel = new JPanel();
GroupLayout groupLayout = new GroupLayout(leftPanel);
leftPanel.setLayout(groupLayout);
//---------------------------panel 1 for information player 1 -------------------------------------
JPanel panel1 = new JPanel();
ImageIcon imageMan1 = new ImageIcon("man1.png");
JLabel imageMan11 = new JLabel("", imageMan1, JLabel.CENTER);
JLabel player1text = new JLabel(obj2.setNamePlayer1.getText());
panel1.add(imageMan11);
panel1.add(player1text);
//---------------------------panel 2 for information player 2------------------------------------
JPanel panel2 = new JPanel();
ImageIcon imageMan2 = new ImageIcon("man2.png");
JLabel imageMan22 = new JLabel("", imageMan2, JLabel.CENTER);
JLabel player2text = new JLabel(obj2.setNamePlayer2.getText());
panel2.add(imageMan22);
panel2.add(player2text);
//---------------------------panel 3 for information player 3-------------------------------------
JPanel panel3 = new JPanel();
if(obj.is3 == true){
ImageIcon imageMan3 = new ImageIcon("man3.png");
JLabel imageMan33 = new JLabel("", imageMan3, JLabel.CENTER);
JLabel player3text = new JLabel("Player 3");
player2text.setFont(fontText);
panel3.add(imageMan33);
panel3.add(player3text);
}
}
}
左边的面板是我的问题 即使我试图在文本文件中更改它们,播放器1和播放器2的jlable也没有变化,第二个问题是即使我从combox选择了chois“3”,播放器3的面板也没有显示。
答案 0 :(得分:0)
尝试在更改元素后调用Frame上的.pack()。
修改
您没有更改标签的代码。你需要的是将标签存储在变量中,就像你在其他组件中一样,并在SetNames()中的okButton上的ActionListener中,而不是对那里的TextFields做什么(看起来不对),更新Label文本。
所以,例如
class SetNames extends JDialog {
[...]
public JButton okButton; // save okButton so others can add listeners
}
class LeadersAndSnake_Project201 extends JFrame implements ActionListener{
// Here I made an object of Set Names class to take the name inside the textfiled
SetNames obj2 = new SetNames();
public LeadersAndSnake_Project201() {
[...]
obj2.okButton.addActionListener(new ActionListener(){/*update labels*/});
}}