Java空JFrame
情况如下 我有这门课:Java空JFrame,java,swing,model-view-controller,jframe,Java,Swing,Model View Controller,Jframe,情况如下 我有这门课: package it.polimi.client.view.gui.phases; import it.polimi.client.controller.ControllerClient; import it.polimi.client.exceptions.BadInputException; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent;
package it.polimi.client.view.gui.phases;
import it.polimi.client.controller.ControllerClient;
import it.polimi.client.exceptions.BadInputException;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChoiceGameTypePhase extends GuiPhase {
ControllerClient controllerClient;
public static JFrame frame;
private String typeGame;
public ChoiceGameTypePhase(ControllerClient controllerClient) {
super();
this.controllerClient = controllerClient;
}
@Override
public void exec() {
initialize();
}
private void initialize() {
frame = new JFrame("ciao");
frame.setBounds(100, 100, 345, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JLabel lblChoiceRules = new JLabel("choose which rules to use");
lblChoiceRules.setBounds(38, 44, 152, 14);
frame.getContentPane().add(lblChoiceRules);
/*JRadioButton rdbtnSimple = new JRadioButton("simple");
rdbtnSimple.setBounds(203, 40, 88, 23);
rdbtnSimple.addActionListener(/new RdbtnBase());
frame.getContentPane().add(rdbtnSimple);
JRadioButton rdbtnComplete = new JRadioButton("complete");
rdbtnComplete.setBounds(203, 66, 88, 23);
rdbtnComplete.addActionListener(new RdbtnAdvanced());
frame.getContentPane().add(rdbtnComplete);
JRadioButton rdbtnOldSchool = new JRadioButton("old school");
rdbtnOldSchool.setBounds(203, 92, 88, 23);
rdbtnOldSchool.addActionListener(new RdbtnOldSchool());
frame.getContentPane().add(rdbtnOldSchool);
JRadioButton rdbtnInfection = new JRadioButton("infection");
rdbtnInfection.setBounds(203, 118, 88, 23);
rdbtnInfection.addActionListener(new RdbtnInfection());
frame.getContentPane().add(rdbtnInfection);
JRadioButton rdbtnArena = new JRadioButton("arena");
rdbtnArena.setBounds(203, 144, 88, 23);
rdbtnArena.addActionListener(new RdbtnArena());
frame.getContentPane().add(rdbtnArena);
ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnSimple);
bg.add(rdbtnComplete);
bg.add(rdbtnOldSchool);
bg.add(rdbtnInfection);
bg.add(rdbtnArena);
JButton btnBack = new JButton("BACK");
btnBack.setBounds(10, 196, 89, 25);
btnBack.addActionListener(new Back());
frame.getContentPane().add(btnBack);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(240, 196, 89, 25);
btnNext.addActionListener(new Next());
frame.getContentPane().add(btnNext);*/
Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
Dimension frameSize = frame.getSize ();
frame.setLocation ((screenSize.width - frameSize.width) / 2 , (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
private class RdbtnBase implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnAdvanced implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnOldSchool implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnInfection implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnArena implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class Back implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
ChoiceGameTypePhase.closeFrame();
ChoiceConnectionPhase choiceConnectionPhase = new ChoiceConnectionPhase();
choiceConnectionPhase.exec();
}
}
private class Next implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
if (typeGame != null ) {
try {
System.out.println(typeGame);
controllerClient.controlGameType(typeGame);
} catch (BadInputException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>" + e.getMessage() + "</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>No field selected</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void closeFrame() {
ChoiceGameTypePhase.frame.dispose();
}
}
package it.polimi.client.controller;
import it.polimi.client.exceptions.BadInputException;
//import it.polimi.client.phases.Phase;
import it.polimi.client.view.gui.GuiHandleNotifiesFromServer;
import it.polimi.client.view.gui.phases.ChoiceConnectionPhase;
import it.polimi.client.view.gui.phases.ChoiceGameTypePhase;
import it.polimi.client.view.gui.phases.ChoiceNicknamePhase;
import it.polimi.client.view.gui.phases.SimpleGamePhase;
import it.polimi.client.view.util.SocketWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
public class ControllerClientGui extends ControllerClient {
public ControllerClientGui() {
super();
}
@Override
public void controlConnection(String userInput) throws BadInputException {
switch (userInput) {
case "SOCKET":
try {
Socket socketServer = createSocket();
this.socketWriter = new SocketWriter(socketServer);
ChoiceConnectionPhase.closeFrame();
new GuiHandleNotifiesFromServer(socketServer, this);
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>Error creating socket</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>Error creating socket</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
break;
case "RMI":
ChoiceConnectionPhase.closeFrame();
ChoiceGameTypePhase choiceGameTypePhaseRmi = new ChoiceGameTypePhase(this);
choiceGameTypePhaseRmi.exec();
break;
}
}
@Override
public void controlGameType(String userInput) throws BadInputException {
System.out.println(userInput);
//socketWriter.printLineStringOnSocket("GameType:" + userInput);
ChoiceGameTypePhase.closeFrame();
ChoiceNicknamePhase choiceNicknamePhase = new ChoiceNicknamePhase(this);
choiceNicknamePhase.exec();
}
@Override
public void controlNickname(String userInput) {
if (userInput.matches("[a-zA-Z]+")) {
//socketWriter.printLineStringOnSocket("Nickname:" + userInput);
ChoiceNicknamePhase.closeFrame();
SimpleGamePhase simpleGamePhase = new SimpleGamePhase(this);
simpleGamePhase.exec();
}
else {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>enter username [only character]</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void controlSimpleGame(String userInput) {
}
@Override
public void controlCompleteGame(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlMove(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlNoise(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlUseItem(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlListRounds(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlPossibleMovementsOpponents(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlEndTurn(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlTimer(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlExit(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlAttack(String userInput) {
// TODO Auto-generated method stub
}
}
而上一个类的“phase”变量是根据服务器发送的内容在另一个类中设置的
而是在此类中创建一个“HandleNotifiesFromServer”实例(或者更好地说是其子类的一个实例:“GuiHandleNotifiesFromServer”):
package it.polimi.client.view.gui.phases;
import it.polimi.client.controller.ControllerClient;
import it.polimi.client.exceptions.BadInputException;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChoiceGameTypePhase extends GuiPhase {
ControllerClient controllerClient;
public static JFrame frame;
private String typeGame;
public ChoiceGameTypePhase(ControllerClient controllerClient) {
super();
this.controllerClient = controllerClient;
}
@Override
public void exec() {
initialize();
}
private void initialize() {
frame = new JFrame("ciao");
frame.setBounds(100, 100, 345, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JLabel lblChoiceRules = new JLabel("choose which rules to use");
lblChoiceRules.setBounds(38, 44, 152, 14);
frame.getContentPane().add(lblChoiceRules);
/*JRadioButton rdbtnSimple = new JRadioButton("simple");
rdbtnSimple.setBounds(203, 40, 88, 23);
rdbtnSimple.addActionListener(/new RdbtnBase());
frame.getContentPane().add(rdbtnSimple);
JRadioButton rdbtnComplete = new JRadioButton("complete");
rdbtnComplete.setBounds(203, 66, 88, 23);
rdbtnComplete.addActionListener(new RdbtnAdvanced());
frame.getContentPane().add(rdbtnComplete);
JRadioButton rdbtnOldSchool = new JRadioButton("old school");
rdbtnOldSchool.setBounds(203, 92, 88, 23);
rdbtnOldSchool.addActionListener(new RdbtnOldSchool());
frame.getContentPane().add(rdbtnOldSchool);
JRadioButton rdbtnInfection = new JRadioButton("infection");
rdbtnInfection.setBounds(203, 118, 88, 23);
rdbtnInfection.addActionListener(new RdbtnInfection());
frame.getContentPane().add(rdbtnInfection);
JRadioButton rdbtnArena = new JRadioButton("arena");
rdbtnArena.setBounds(203, 144, 88, 23);
rdbtnArena.addActionListener(new RdbtnArena());
frame.getContentPane().add(rdbtnArena);
ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnSimple);
bg.add(rdbtnComplete);
bg.add(rdbtnOldSchool);
bg.add(rdbtnInfection);
bg.add(rdbtnArena);
JButton btnBack = new JButton("BACK");
btnBack.setBounds(10, 196, 89, 25);
btnBack.addActionListener(new Back());
frame.getContentPane().add(btnBack);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(240, 196, 89, 25);
btnNext.addActionListener(new Next());
frame.getContentPane().add(btnNext);*/
Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();
Dimension frameSize = frame.getSize ();
frame.setLocation ((screenSize.width - frameSize.width) / 2 , (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
private class RdbtnBase implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnAdvanced implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnOldSchool implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnInfection implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class RdbtnArena implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
typeGame = e.getActionCommand();
}
}
private class Back implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
ChoiceGameTypePhase.closeFrame();
ChoiceConnectionPhase choiceConnectionPhase = new ChoiceConnectionPhase();
choiceConnectionPhase.exec();
}
}
private class Next implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
if (typeGame != null ) {
try {
System.out.println(typeGame);
controllerClient.controlGameType(typeGame);
} catch (BadInputException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>" + e.getMessage() + "</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>No field selected</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void closeFrame() {
ChoiceGameTypePhase.frame.dispose();
}
}
package it.polimi.client.controller;
import it.polimi.client.exceptions.BadInputException;
//import it.polimi.client.phases.Phase;
import it.polimi.client.view.gui.GuiHandleNotifiesFromServer;
import it.polimi.client.view.gui.phases.ChoiceConnectionPhase;
import it.polimi.client.view.gui.phases.ChoiceGameTypePhase;
import it.polimi.client.view.gui.phases.ChoiceNicknamePhase;
import it.polimi.client.view.gui.phases.SimpleGamePhase;
import it.polimi.client.view.util.SocketWriter;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
public class ControllerClientGui extends ControllerClient {
public ControllerClientGui() {
super();
}
@Override
public void controlConnection(String userInput) throws BadInputException {
switch (userInput) {
case "SOCKET":
try {
Socket socketServer = createSocket();
this.socketWriter = new SocketWriter(socketServer);
ChoiceConnectionPhase.closeFrame();
new GuiHandleNotifiesFromServer(socketServer, this);
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>Error creating socket</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>Error creating socket</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
break;
case "RMI":
ChoiceConnectionPhase.closeFrame();
ChoiceGameTypePhase choiceGameTypePhaseRmi = new ChoiceGameTypePhase(this);
choiceGameTypePhaseRmi.exec();
break;
}
}
@Override
public void controlGameType(String userInput) throws BadInputException {
System.out.println(userInput);
//socketWriter.printLineStringOnSocket("GameType:" + userInput);
ChoiceGameTypePhase.closeFrame();
ChoiceNicknamePhase choiceNicknamePhase = new ChoiceNicknamePhase(this);
choiceNicknamePhase.exec();
}
@Override
public void controlNickname(String userInput) {
if (userInput.matches("[a-zA-Z]+")) {
//socketWriter.printLineStringOnSocket("Nickname:" + userInput);
ChoiceNicknamePhase.closeFrame();
SimpleGamePhase simpleGamePhase = new SimpleGamePhase(this);
simpleGamePhase.exec();
}
else {
JOptionPane.showMessageDialog(null, "<html><p align='center' width='150'>enter username [only character]</p></html>", "messagge", JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void controlSimpleGame(String userInput) {
}
@Override
public void controlCompleteGame(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlMove(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlNoise(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlUseItem(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlListRounds(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlPossibleMovementsOpponents(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlEndTurn(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlTimer(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlExit(String userInput) {
// TODO Auto-generated method stub
}
@Override
public void controlAttack(String userInput) {
// TODO Auto-generated method stub
}
}
在GuiController中直接创建阶段,在下一行执行它,我可以正确地看到包含所有元素的窗口
。。。我真的不知道该怎么想,调试了一整天,什么都没得到,你能帮我吗
我关闭了框架,因为我的想法是为每个“阶段”打开一个窗口,服务器通过套接字发送正确的阶段。 如前所述,这是来自服务器的guihandlenotifies的内容:
package it.polimi.client.view.gui;
import java.net.Socket;
import it.polimi.client.controller.ControllerClient;
import it.polimi.client.view.HandleNotifiesFromServer;
import it.polimi.client.view.gui.phases.ChoiceGameTypePhase;
import it.polimi.client.view.gui.phases.ChoiceNicknamePhase;
import it.polimi.client.view.gui.phases.SimpleGamePhase;
public class GuiHandleNotifiesFromServer extends HandleNotifiesFromServer {
public GuiHandleNotifiesFromServer(Socket socketServer, ControllerClient controllerClient) {
super(socketServer, controllerClient);
}
@Override
public void buildPhaseObjectDependingOnServerPhase(String phase) {
switch(phase) {
case "InsertGameTypePhase":
System.out.println(session.getControllerClient() + " " + phase + " DEBUG buildPhaseObjectDependingOnServerPhase");
this.phase = new ChoiceGameTypePhase(session.getControllerClient());
break;
case "InsertNicknamePhase":
this.phase = new ChoiceNicknamePhase(session.getControllerClient());
break;
case "InsertSimpleGamePhase ":
this.phase = new SimpleGamePhase(session.getControllerClient());
break;
}
}
}
为了更快地获得更好的帮助,可以发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。据我所知,可能是套接字I/O阻塞了EDT。不要阻止EDT(事件调度线程)。发生这种情况时,GUI将“冻结”。有关详细信息和修复方法,请参见。
lblChoiceRules.setBounds(38,44,152,14)代码>Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或者与布局填充和边框一起使用。Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();维度frameSize=frame.getSize();frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);frame.setVisible(true)代码>更容易frame.pack();frame.setLocationRelativeTo(空);frame.setVisibe(true)代码>为什么要处理帧ChoiceConnectionPhase.closeFrame()代码>和新GuiHandleNotifiesFromServer中的内容(socketServer,this)代码>constructor@Madhan编辑以回答您的问题。