Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

JFrame - JPanel aktualisieren

Empfohlene Antworten

Veröffentlicht

Hi zusammen:

Ich habe folgenden Code


public class MainFrame extends JFrame

implements ActionListener {


 MainPanel main;


 /**

  * Konstruktor

  */

 public MainFrame() {

  super("Test");

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  setSize(400,400);

  setResizable(false);

  init();

 }


 private void init() {

  filechooser = new JFileChooser(new File("./"));

  createMenu();

 }


 private void createMenu() {

  JMenuBar mb = new JMenuBar();

  JMenu file = new JMenu("Datei");

  JMenu help = new JMenu("Hilfe");


  file.add(createMenuItem("Neu","new",this));

  file.add(createMenuItem("Öffnen","open",this));

  file.add(createMenuItem("Speichern","save",this));

  file.addSeparator();

  file.add(createMenuItem("Beenden","exit",this));


  help.add(createMenuItem("Hilfe","help",this));


  mb.add(file);

  mb.add(help);

  setJMenuBar(mb);

 }


 private JMenuItem createMenuItem(String title, String command, ActionListener listener) {

  JMenuItem item = new JMenuItem(title);

  item.setActionCommand(command);

  item.addActionListener(listener);

  return item;

 }


 public void actionPerformed(ActionEvent e) {

  System.out.println(e.getActionCommand());

  String cmd = e.getActionCommand();

  if(cmd.equals("new")) {

   main = new MainPanel();

   this.getContentPane().add(main);

  } else if(cmd.equals("open")) {

  }

 }


 public static void main(String[] args) {

  MainFrame mf = new MainFrame();

  mf.show();

 }


}

MainPanel ist ein Formular mit mehreren TextFeldern und einem eigenen Actionlistener.

Mein Problem Hierbei ist:

- Beim ersten mal drücken im Menü "Neu" bekomme ich den MainPanel angezeigt!

- Ich trage meine Daten ein und schicke diese Weg.

- Danach Drücke ich wieder "Neu" und will eigentlich, das das MainPanel neu initialisiert wird, das heisst alle Felder leer sind.

Aber mir wird das selbe Panel angezeigt.

Woran liegt das? Kann mir einer weiterhelfen?

Hi

versuch doch mal folgende Zeile in deinen Code einzufügen. Vielleicht hilft das schon.

if(cmd.equals("new")) {

   main = new MainPanel();

   this.getContentPane().add(main);

   [B][COLOR=DarkRed]this.pack();[/COLOR][/B]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.