M
mike
Gast
habe eine leicht abgeänderte Version von dem Code, den ich hier gepostet habe http://www.java-forum.org/de/topics/java_2553.html
und
habe nun folgendes Problem, wenn ich durch Drücken von Button "neu" eine neue myPanel erzeuge, dann andere Werte für rows und columns in den choices wähle und mit Hilfe des Buttons "neu" wieder eine neue myPanel erzeuge, wird sie zwar korrekt gezeichnet, aber sobald ich das Fenster minimere oder sonst etwas damit mache, sind die Änderungen weg. Habe schon versucht, das Problem durch Aufruf von repaint(), invalidate() und revalidate() an verschiedensten Programmstellen zu lösen, habe auch versucht vor dem Erzeugen eines neuen myPanel myPanel.removeAll() anzuwenden, aber das bringt nicht den gewüschten Effekt.
Kann mir jemand helfen?
Gruß mike
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyFrame extends JFrame implements ActionListener,ItemListener
{
private MyPanel myPanel;
private JPanel startChoicePanel, inputsChoicePanel, settingsPanel;
private JButton newButton, createButton, beispiel1Button, beispiel2Button;
private Choice startChoice, inputsChoice;
private JLabel startLabel, inputsLabel;
private GridLayout tableGridLayout;
private int inputs = 1;
private int start = 1;
public MyFrame(String title)
{
super(title);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
/* Panels initialisieren */
settingsPanel = new JPanel();
settingsPanel.setLayout(new GridLayout(6,1,2,2));
startChoicePanel = new JPanel();
startChoicePanel.setLayout(new FlowLayout());
inputsChoicePanel = new JPanel();
inputsChoicePanel.setLayout(new FlowLayout());
/* Labels & Choices hinzufügen */
startLabel = new JLabel("Text");
startChoice = new Choice();
startChoice.add("1");
startChoice.add("2");
startChoice.add("3");
startChoice.addItemListener(this);
startChoicePanel.add(startLabel);
startChoicePanel.add(startChoice);
inputsLabel = new JLabel("Anzahl Inputs");
inputsChoice = new Choice();
inputsChoice.add("1");
inputsChoice.add("2");
inputsChoice.add("3");
inputsChoice.add("4");
inputsChoice.add("5");
inputsChoice.addItemListener(this);
inputsChoicePanel.add(inputsLabel);
inputsChoicePanel.add(inputsChoice);
settingsPanel.add(startChoicePanel);
settingsPanel.add(inputsChoicePanel);
/* Buttons hinzufügen */
newButton = new JButton("neu");
newButton.addActionListener(this);
beispiel1Button = new JButton("Beispiel 1");
beispiel1Button.addActionListener(this);
beispiel2Button = new JButton("Beispiel 2");
beispiel2Button.addActionListener(this);
createButton = new JButton("OK");
createButton.addActionListener(this);
settingsPanel.add(newButton);
settingsPanel.add(beispiel1Button);
settingsPanel.add(beispiel2Button);
settingsPanel.add(createButton);
contentPane.add(settingsPanel,BorderLayout.WEST);
}
public void actionPerformed(ActionEvent evt)
{
String command = evt.getActionCommand();
if (command.equals("neu"))
{
myPanel = new MyPanel(start,inputs);
this.getContentPane().add(myPanel,BorderLayout.CENTER);
validate();
}
}
public void itemStateChanged(ItemEvent evt)
{
Choice sourceChoice = (Choice)evt.getSource();
String command = evt.getItem().toString();
if (sourceChoice.equals(startChoice))
{
start = Integer.parseInt(command);
}
if (sourceChoice.equals(inputsChoice))
{
inputs = Integer.parseInt(command);
}
}
}
class MyPanel extends JPanel
{
private JTextField textField[];
private int rows,columns;
public MyPanel(int a, int b)
{
rows = a;
columns = b;
setLayout(new GridLayout(rows,columns));
textField = new JTextField[rows*columns];
for (int i = 0; i <= rows*columns-1; i++)
{
textField[i] = new JTextField();
add(textField[i]);
}
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame implements ActionListener
{
private JButton tableButton;
private JPanel buttonleiste;
private BorderLayout borderLayout;
private MyFrame myFrame;
public MainFrame(String title)
{
super(title);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
/* Panels initialisieren */
buttonleiste = new JPanel();
borderLayout = new BorderLayout();
buttonleiste.setLayout(borderLayout);
buttonleiste.setBackground(Color.lightGray);
/* Buttons hinzufügen */
tableButton = new JButton("Table");
tableButton.addActionListener(this);
buttonleiste.add(tableButton,borderLayout.CENTER);
contentPane.add(buttonleiste,BorderLayout.NORTH);
}
public static void main(String args[])
{
MainFrame mainFrame = new MainFrame("MainFrame");
mainFrame.setSize(600,600);
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String command = evt.getActionCommand();
if (command.equals("Table"))
{
myFrame = new MyFrame("Table");
myFrame.setSize(400,400);
myFrame.setVisible(true);
}
}
}
habe nun folgendes Problem, wenn ich durch Drücken von Button "neu" eine neue myPanel erzeuge, dann andere Werte für rows und columns in den choices wähle und mit Hilfe des Buttons "neu" wieder eine neue myPanel erzeuge, wird sie zwar korrekt gezeichnet, aber sobald ich das Fenster minimere oder sonst etwas damit mache, sind die Änderungen weg. Habe schon versucht, das Problem durch Aufruf von repaint(), invalidate() und revalidate() an verschiedensten Programmstellen zu lösen, habe auch versucht vor dem Erzeugen eines neuen myPanel myPanel.removeAll() anzuwenden, aber das bringt nicht den gewüschten Effekt.
Kann mir jemand helfen?
Gruß mike