Ich bin grad dabei für meine Minispiele einen Launcher zu schreiben, in dem sich ein paar Einstellungen tätigen lassen sollen, auf einmal bekomme ich dann ein seltsames Problem. Das Panel mit den wichtigsten Komponenten wird nicht mehr angezeigt, nur wenn ich mit der Maus drüberfahre, werden einige Komponenten zum Teil sichtbar, Ich hab auch schon das Internet durchforstet -auch dieses Forum-, doch egal an welche Stelle ich ein repaint(), validate() oder sonstiges setze, es will einfach nichts bringen. Das wirklich seltsame daran ist die Tatsache, dass es erst mehrere Male ohne Probleme ging, aber jetzt eben nicht mehr, obwohl ich nichts an den Komponenten etc. geändert habe. Ich hoffe ihr könnt mir helfen, wäre klasse.
Klasse, welche das Launcher Panel enthält:
Klasse, die das Frame enthält, auf welches das Panel gezeichnet werden soll:
Die Main Klasse
Hab die Klassen auch als .java Anhänge hinzugefügt und zudem noch zusätzlich zwei Anhänge hinzugefügt, die ungefähr zeigen, wie das aussieht.Ich hoffe ihr könnt mir helfen, wäre super, ich verzweifle da momentan dran.
Klasse, welche das Launcher Panel enthält:
Java:
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import mainGame.Main;
public class Launcher extends JPanel {
private static final long serialVersionUID = 1L;
private final String TITLE = "Launcher v0.1";
private ActionHandler actionHandler;
private Dimension size;
private Dimension itemSize;
private Dimension buttonSize;
private Dimension smallSpace;
private JPanel optionsPanel;
private JPanel buttonPanel;
private JLabel info;
private JLabel res_info;
private JButton start;
private JButton close;
private JCheckBox decorated;
private JComboBox<String> resolution;
private boolean framed = false;
private int width;
private int height;
public Launcher() {
setName(TITLE);
size = new Dimension(240, 240/9*12);
itemSize = new Dimension(size.width/4*3, size.height/12);
buttonSize = new Dimension(size.width/2, itemSize.height);
smallSpace = new Dimension(buttonSize.width, buttonSize.height/4);
actionHandler = new ActionHandler();
optionsPanel = new JPanel();
buttonPanel = new JPanel();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
info = new JLabel(TITLE);
res_info = new JLabel();
start = new JButton("Start");
close = new JButton("Close");
decorated = new JCheckBox("Use a decorated frame?");
resolution = new JComboBox<String>();
info.setHorizontalAlignment(JLabel.CENTER);
info.setMaximumSize(itemSize);
info.setMinimumSize(itemSize);
info.setPreferredSize(itemSize);
info.setAlignmentX(Component.CENTER_ALIGNMENT);
resolution.setMaximumSize(itemSize);
resolution.setMinimumSize(itemSize);
resolution.setPreferredSize(itemSize);
resolution.setEditable(false);
resolution.addItem("640x384");
resolution.addItem("960x576");
resolution.addItem("1280x768");
resolution.addItem("1600x960");
resolution.addItem("1920x1152");
resolution.setToolTipText("Choose your wanted resolution!");
resolution.addActionListener(actionHandler);
resolution.setAlignmentX(Component.CENTER_ALIGNMENT);
resolution.setSelectedItem(resolution.getItemAt(0));
res_info.setHorizontalAlignment(JLabel.CENTER);
res_info.setMaximumSize(itemSize);
res_info.setMinimumSize(itemSize);
res_info.setPreferredSize(itemSize);
res_info.setAlignmentX(Component.CENTER_ALIGNMENT);
decorated.setEnabled(true);
decorated.setMaximumSize(itemSize);
decorated.setMinimumSize(itemSize);
decorated.setPreferredSize(itemSize);
decorated.setHorizontalAlignment(JCheckBox.CENTER);
decorated.setAlignmentX(Component.CENTER_ALIGNMENT);
decorated.setToolTipText("<html><p>Activated: Game will be framed</p><p>Deactivated: Game won´t be framed</p></html>");
start.setMaximumSize(buttonSize);
start.setMinimumSize(buttonSize);
start.setPreferredSize(buttonSize);
start.addActionListener(actionHandler);
start.setAlignmentX(Component.CENTER_ALIGNMENT);
close.setMaximumSize(buttonSize);
close.setMinimumSize(buttonSize);
close.setPreferredSize(buttonSize);
close.addActionListener(actionHandler);
close.setAlignmentX(Component.CENTER_ALIGNMENT);
add(Box.createRigidArea(smallSpace));
add(info);
add(Box.createRigidArea(smallSpace));
add(Box.createRigidArea(smallSpace));
add(Box.createRigidArea(smallSpace));
optionsPanel.add(Box.createRigidArea(smallSpace));
optionsPanel.add(resolution);
optionsPanel.add(res_info);
optionsPanel.add(Box.createRigidArea(smallSpace));
optionsPanel.add(decorated);
buttonPanel.add(Box.createRigidArea(smallSpace));
buttonPanel.add(start);
buttonPanel.add(Box.createRigidArea(smallSpace));
buttonPanel.add(close);
add(optionsPanel);
add(buttonPanel);
}
public Dimension getSize() {
return size;
}
private String[] getResolution(String resolution) {
boolean pastX = false;
String width = "";
String height = "";
for(int i = 0; i < resolution.length(); i++) {
if(resolution.substring(i, i+1).equalsIgnoreCase("x")) {
pastX = true;
i++;
}
if(!pastX) {
width += resolution.substring(i, i+1);
} else {
height += resolution.substring(i, i+1);
}
}
return new String[] {
width,
height
};
}
private int[] parseStrings(String[] resolution) {
return new int[] {
Integer.parseInt(resolution[0]),
Integer.parseInt(resolution[1])
};
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean isFramed() {
return framed;
}
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(resolution)) {
String res = resolution.getSelectedItem().toString();
res_info.setText("Resolution: "+res);
res_info.setToolTipText("<html><p>Actually chosen resolution:</p><p>WIDTH: "+getResolution(res)[0]+" | HEIGHT: "+getResolution(res)[1]+"</p></html>");
}
if(ae.getSource().equals(start)) {
String res = resolution.getSelectedItem().toString();
framed = decorated.isSelected();
width = parseStrings(getResolution(res))[0];
height = parseStrings(getResolution(res))[1];
Main.run();
}
if(ae.getSource().equals(close)) {
if(JOptionPane.showConfirmDialog(Launcher.this, "Do you really want to close jPong?", Launcher.this.TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
System.exit(0);
};
}
}
}
}
Klasse, die das Frame enthält, auf welches das Panel gezeichnet werden soll:
Java:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import resource.ImageLoader;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
private String title;
private Dimension innerSize;
private Dimension framedSize;
private Dimension frameSize;
private ImageLoader loader;
public Frame(JPanel layout, Dimension innerSize) {
this.innerSize = innerSize;
this.title = layout.getName();
loader = new ImageLoader();
setTitle(title);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(loader.loadImage("favicon"));
setVisible(true);
frameSize = new Dimension(getInsets().left+getInsets().right, getInsets().bottom+getInsets().bottom);
framedSize = new Dimension(frameSize.width+innerSize.width, innerSize.height+frameSize.height);
setVisible(false);
setMaximumSize(framedSize);
setMinimumSize(framedSize);
setPreferredSize(framedSize);
setLocationRelativeTo(null);
getContentPane().add(layout);
pack();
}
public Dimension getFramedSize() {
return framedSize;
}
public Dimension getInnerSize() {
return innerSize;
}
public Dimension getFrameSize() {
return frameSize;
}
public String getTitle() {
return title;
}
}
Die Main Klasse
Java:
import frame.Frame;
import frame.Launcher;
public class Main {
static Launcher launcher;
static Frame f;
public static void main(String[] args) {
launcher = new Launcher();
f = new Frame(launcher, launcher.getSize());
f.setVisible(true);
}
public static void run() {
//WIRD BEI START AUFGERUFEN
}
}
Hab die Klassen auch als .java Anhänge hinzugefügt und zudem noch zusätzlich zwei Anhänge hinzugefügt, die ungefähr zeigen, wie das aussieht.Ich hoffe ihr könnt mir helfen, wäre super, ich verzweifle da momentan dran.
Anhänge
Zuletzt bearbeitet: