Hallo,
ich hab ein kleines Problem mit ein paar Componenten, die auf einem JPanel dargestellt werden sollen. Die JPanel sind mit einem BoxLayout untereinander angeordnet und Fehlermeldungen seitens des Compilers gibt es nicht.
Zuvor hab ich die Panels statt auf ein weiteres Grundpanel auf ein JFrame gezeichnet, da hab ich aber irgendeine Fehlermeldung bekommen, laut der man ein BoxLayout nicht direkt auf ein JFrame zeichnen darf.
Kann mir jemand sagen, warum zwar das JPanel erstellt, aber sonst nichts weiter gezeichnet wird?
ich hab ein kleines Problem mit ein paar Componenten, die auf einem JPanel dargestellt werden sollen. Die JPanel sind mit einem BoxLayout untereinander angeordnet und Fehlermeldungen seitens des Compilers gibt es nicht.
Zuvor hab ich die Panels statt auf ein weiteres Grundpanel auf ein JFrame gezeichnet, da hab ich aber irgendeine Fehlermeldung bekommen, laut der man ein BoxLayout nicht direkt auf ein JFrame zeichnen darf.
Kann mir jemand sagen, warum zwar das JPanel erstellt, aber sonst nichts weiter gezeichnet wird?
Code:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Panel extends JPanel{
private JPanel pnlForm = null;
private JPanel pnlComponent = null;
private JButton btn = null;
/**
* Launch the application
* @param args
*/
public static void main(final String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Panel(530, 375);
} catch (final Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Panel(final int w, final int h) {
super();
final JFrame f = new JFrame("Panel");
f.setPreferredSize(new Dimension(w, h));
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
initGUI();
} catch (final Throwable e) {
e.printStackTrace();
}
f.pack();
f.setVisible(true);
}
private void initGUI() throws Exception {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
initPanelForm();
initPanelComponent();
}
private JPanel getPanelForm() {
if (this.pnlForm != null)
{ return this.pnlForm; }
final JPanel p = new JPanel(new FlowLayout());
p.setBorder(new TitledBorder(null, "Form",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
null, null));
add(p);
this.pnlForm = p;
return this.pnlForm;
}
private JPanel getPanelComponent() {
if (this.pnlComponent != null)
{ return this.pnlComponent; }
final JPanel p = new JPanel(new FlowLayout());
p.setBorder(new TitledBorder(null, "Component",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
null, null));
add(p);
this.pnlComponent = p;
return this.pnlComponent;
}
private void initPanelForm() {
getPanelForm();
}
private void initPanelComponent() {
getPanelComponent();
getButton();
}
private JButton getButton() {
if (this.btn != null)
{ return this.btn; }
final JButton b = new JButton("Button");
pnlComponent.add(b);
this.btn = b;
return this.btn;
}
}