GUI-Komponente in separate Klassen auslagern?

Hallo zusammen,

ich habe eine GUI programmiert. Leider ist der Code ziemlich unübersichtlich. Deswegen dachte ich, ich lagere die Erstellung der Komponenten aus. Nur leider verschwinden dann die ausgelagerten Komponenten aus der GUI.

Beispiel:

Erste Klasse:

package Verknüpfung;

import java.awt.BorderLayout;

public class Teil1 extends JFrame {
  private JPanel contentPane;

  static int f;

  /*
   * Launch the application.
   */
  public static void main(String[] args) {
    Teil2 blubb = new Teil2();
    Teil2.textfeld();

    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Teil1 frame = new Teil1();
          frame.setVisible(true);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /*
   * Create the frame.
   */
  public Teil1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lbl1 = new JLabel("Test");
    lbl1.setBounds(45, 68, 69, 20);
    contentPane.add(lbl1);

    JLabel lbl2 = new JLabel("Test");
    lbl2.setBounds(45, 151, 69, 20);
    contentPane.add(lbl2);
  
    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        int i;
        i = 5;
        f = i + 6;
      }
    });
    btnNewButton.setBounds(209, 199, 115, 29);
    contentPane.add(btnNewButton);
  }
}

Zweite Klasse:

package Verknüpfung;

import java.awt.BorderLayout;

public class Teil2 extends JFrame {
  private JPanel contentPane;

  private JTextField txt1;

  private JTextField txt2;

  /*
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Teil2 frame = new Teil2();
          frame.setVisible(true);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
                                                                                                  
  /*
   * Create the frame.
   */
  public Teil2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    txt1 = new JTextField();
    txt1.setBounds(267, 74, 146, 26);
    contentPane.add(txt1);
    txt1.setColumns(10);
    
    txt2 = new JTextField();
    txt2.setBounds(267, 149, 146, 26);
    contentPane.add(txt2);
    txt2.setColumns(10);
    txt2.setText("Hi");
  }

  public static void textfeld() {
  }                                                                               }

programmieren, Java, Oberfläche, GUI
Wie kann ich bei Excel einen Button erstellen der abspeichert?

Hallo,

Ich würde gerne bei Excel einen Button erstellen, der die Excel mappe auf dem Desktop abspeichert. Der Name der Datei soll in Zeile A1 stehen.

Den Button habe ich bereits erstellt. Ich scheitere noch bei dem Makro, weil ich keine ahnung davon habe. Ich habe etwas im Internet gefunden, was meinen Vorstellungen nahe kommt. Ich habe versucht es anzupassen, aber irgendwie klappt das alles nicht.

Sub Schaltfläche1_Klicken()
Dim lw_pfad As String
lw_pfad = ActiveSheet.Range("A1").Value
lw_pfad = InputBox("Geben Sie hier das Laufwerk und den Pfad an, wo die Datei gespeichert werden soll." & Chr(13) & Chr(13) & "(Ihre Eingabe wird in A1 als neuer Default-Wert gespeichert.)", "Datei speichern unter...", lw_pfad)
If lw_pfad = "" Then
  MsgBox "Die Datei wird nicht gespeichert, da Sie [Abbrechen] gedrückt oder nichts eingegeben haben.", , "Abbruch"
  Exit Sub
Else
  If Right(lw_pfad, 1) <> "\" Then lw_pfad = lw_pfad & "\"
  ActiveSheet.Range("A1").Value = lw_pfad
Rem MsgBox lw_pfad
ActiveWorkbook.SaveAs lw_pfad & ActiveSheet.Range("B2").Value & ActiveSheet.Range("C4").Value & ".xls"
MsgBox "Die Datei wurde unter " & lw_pfad & ActiveSheet.Range("B2").Value & ActiveSheet.Range("C4").Value & ".xls gespeichert.", , "OK"
End If
End Sub

Das ist, was ich bis jetzt habe.

Vielen Dank für die Hilfe im Voraus!

Computer, Microsoft, Microsoft Excel, Excel 2010, Technik, Programm, programmieren, Makro, VBA, Technologie, Spiele und Gaming
C#: Ping Pong Programm startet nicht?

Mein Programm ist zwar vollständig, aber beim Start fängt es sich nicht an zu bewegen, sondern bleibt gefreezed.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pong_the_Game
{
  public partial class Form1 : Form
  {
    public int speed_left = 4; // ball speed
    public int speed_top = 4;
    public int Point = 0;

    public Form1()
    {
      InitializeComponent();

      timer1.Enabled = true;
      Cursor.Hide();
      this.FormBorderStyle = FormBorderStyle.None; // wegmachen der Border
      this.TopMost = true; //form im vordergrund
      this.Bounds = Screen.PrimaryScreen.Bounds; // Fullscreen
      
      Brett.Top = Playground.Bottom - (Playground.Bottom / 10); // Position vom Brett
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      Brett.Left = Cursor.Position.X - (Brett.Width / 2); // macht das Brett als Cursor
      Ball.Left += speed_left;   // bewegt den Ball
      Ball.Top += speed_top;

      if (Ball.Bottom >= Brett.Top && Ball.Bottom <= Brett.Bottom && Ball.Left >= Brett.Left && Ball.Right <= Brett.Right) // Kollision
      {
        speed_top += 2;
        speed_left += 2;
        speed_top = -speed_top; // tauscht Beschreibeung
        Point += 1;
      }

      if (Ball.Left <= Playground.Left)
      {
        speed_left = -speed_left;
      }

      if (Ball.Right <= Playground.Right)
      {
        speed_left = -speed_left;
      }

      if (Ball.Left <= Playground.Top)
      {
        speed_top = -speed_top;
      }

      if (Ball.Bottom <= Playground.Bottom)
      {
        timer1.Enabled = false; // wenn der Ball ins unten raus ist, stoppt das Spiel
      }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Escape) // Esc drücken für Verlassen
      {
        this.Close();
      }
    }

    private void pbr_Click(object sender, EventArgs e)
    {
    }

    private void pbb_Click(object sender, EventArgs e)
    {
    }
  }
}

Bild zum Beitrag
programmieren, Informatik
Discord kann nicht importiert werden (Python)?

Hallo,

Ich möchte einen Discord Bot programmieren. Jedoch scheitert schon der Anfang. Sobald ich

import discord

eingebe kommt die Meldung

Exception has occurred: SyntaxError
invalid syntax (compat.py, line 32)
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 677, in _load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 967, in _find_and_load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 983, in _find_and_load
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 677, in _load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 967, in _find_and_load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 983, in _find_and_load
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 677, in _load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 967, in _find_and_load_unlocked
  File "D:\kubilay\coding\&lt;frozen importlib._bootstrap&gt;", line 983, in _find_and_load
  File "D:\Kubilay\Coding\bot.py", line 1, in <module>
    import discord

Was müsste ich machen, damit dieser "Syntax" Fehler behoben wird? Ich habe Python auch schon neu installiert. Dies hat jedoch nichts gebracht.

Code:

import discord
import asyncio

client = discord.Client()

@client.event async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------')

client.run('token')

Python 3.7.0

PC, Computer, Software, Technik, programmieren, Bots, Python, Discord

Meistgelesene Beiträge zum Thema Programmieren