Наследование Java или GUI пошли не так

Несмотря на некоторые советы, я все еще ошибаюсь. Я получаю одно базовое окно, а другое - с дополнительными функциями, но без базовых из предыдущего окна. Вместо этого я хотел бы одно новое окно, объединяющее основные и новые функции. Вот код, который у меня есть: (также какой подход вы посоветуете?)

package windows;

import java.awt.*;
import javax.swing.*;

public abstract class WindowTemplate extends JFrame {

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */
public WindowTemplate () {

JFrame myFrame = new JFrame("My first window");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setSize(550, 450);
myFrame.setLocationRelativeTo(null);

// JLabel emptyLabel = new JLabel("");
// emptyLabel.setPreferredSize(new Dimension(550, 450));

// myFrame.getContentPane().setLayout(new CardLayout());
// myFrame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

// myFrame.pack();

}

}

теперь тот, который предназначен для "расширенного":

package windows;

import java.awt.*;
import javax.swing.*;

public class a_Welcome extends WindowTemplate {

public a_Welcome() {

JPanel area = new JPanel();

JLabel text = new JLabel("One line another line and another line"); // , JLabel.CENTER);

// text.setBounds(80, 400, 400, 50);
add(area);

// area.setLayout(null);
area.add(text, new CardLayout());

// area.add(text); // , BorderLayout.CENTER);

Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
text.setForeground(Color.green);
area.setBackground(Color.darkGray);
area.setSize(550, 450);

}

}

// timer-after 5 seconds-go to the next window (countdown in the bottom right corner)

и основной:

package windows;

public class Launcher {

public static void main(String[] args) {

// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {

        // WindowTemplate.createWindow();
        // a_Welcome.createWindow();

         a_Welcome window = new a_Welcome();
         window.setVisible(true);
    }
});

}

}



- Альтернативно -

public class WindowTemplate extends JFrame {

// Constructor
public WindowTemplate() {
    init();
}

public void init() {
    // add basic components
    JFrame myFrame = new JFrame("My first window");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);
    myFrame.setSize(550, 450);
    myFrame.setLocationRelativeTo(null);
}
}

и

public class a_Welcome extends WindowTemplate {

public a_Welcome() {
    super();
}

@Override
public void init() {
    super.init(); // important so you get the base stuff
    // add other components
    JPanel area = new JPanel();
    JLabel text = new JLabel("One line another line and another line");
    add(area);
    area.add(text, new CardLayout());

    Font font = new Font("SansSerif", Font.BOLD, 30);
    text.setFont(font);
    text.setForeground(Color.green);
    area.setBackground(Color.darkGray);
    area.setSize(550, 450);

}
}

Приносим извинения за большой объем кода и благодарим за помощь!

0
задан Hurdler 20 July 2011 в 20:27
поделиться