いくつかのヒントにもかかわらず、私はまだこの1つを間違っています。私は1つの基本的なウィンドウと別の機能を備えた別のもので終わりますが、前のウィンドウの基本的なものはありません。代わりに、基本と新機能を組み合わせた新しいウィンドウが1つ必要です。 (また、あなたがどのアプローチを助言する?)
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();
}
}
を今「拡張」することを意図している1:
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);
}
}
!
'a_Welcome'は関係なく、言語の慣用的なJavaとひどいクラス名ではありませんし、その後、任意のMyFrameと参照を交換してください。 –
はい、私はそれを知っています。しかし、それは最初のウィンドウであり、 "a_"ビットがそれがトップに留まることを確かめるので、私の人生は楽になります。 「ウィンドウ」を書く方法ではないことを確認してください。明らかに、私はそれを適切に変更します。 – Hurdler
「何の上に」とどまる? –