- EDIT-- 我有一个由两个JLabel组成的欢迎窗口。它有一个指向从3到0计时的定时器的链接。之后,包含JLabel和单选按钮的新窗口“UsedBefore”应自动出现在前一个窗口的位置。当我运行“启动器”时,第一个窗口显示计数器显示3,2,1,0然后没有任何反应。
我认为问题在于参考不佳,但我不确定。我有“Launcher”课程:
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Welcome window = new Welcome();
window.setVisible(true);
}
});
} // end main
我启动“欢迎”窗口的地方:
public Welcome() {
init();
}
public void init() {
// here I'm adding stuff to the window and then I have:
setLayout(cardLayout);
add(big, "1welcome");
// UsedBefore.MakeUsedBeforeWindow(); // ???
new MyTimer(this).start();
} // end init
这是MyTimer进行倒计时和:
welcome.showNextWindow(); // private Welcome welcome;
我们回到“欢迎”课程:
public void showNextWindow() {
cardLayout.next(this);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("My Frame");
frame.getContentPane().add(new Welcome());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
最后是“UsedBefore”类:
public UsedBefore() {
super(new BorderLayout());
init();
}
public void MakeUsedBeforeWindow() {
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
add(area, "2usedBefore?");
area.setBackground(Color.white);
JLabel textLabel = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel.setForeground(Color.green);
Font font = new Font("SansSerif", Font.PLAIN, 30);
textLabel.setFont(font);
textLabel.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel, SwingConstants.CENTER);
add(area, "2usedBefore?");
}
主要:
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() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("RadioButtons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane - not sure how to do it
// JComponent newContentPane = new UsedBefore();
// newContentPane.setOpaque(true); //content panes must be opaque
// frame.setContentPane(newContentPane);
// frame.getContentPane().add(new UsedBefore());
//Display the window.
frame.setSize(550, 450);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
这是一段相当艰难的旅程。很抱歉很多代码,我希望路径清晰。一旦我有1-> 2&gt; 3链接正确,我应该能够做其余的,所以任何帮助表示赞赏。谢谢。
答案 0 :(得分:0)
我建议采用略有不同的方法。为什么不构建JPanel
而不是Windows,并在需要时添加/删除这些JPanel
。
Ex(此处welcome
面板首先显示其减少的计数器,当计数器达到0时,会显示other
面板):
public class T extends JFrame {
private int couterValue = 3;
private JPanel welcome;
private JLabel counter;
private JPanel other;
private Timer timer;
public T() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
buildWelcomePanel();
buildOtherPanel();
add(welcome);
setSize(550, 450);
setLocationRelativeTo(null);
setVisible(true);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (couterValue == 0) {
timer.cancel();
// Switch the panels as the counter reached 0
remove(welcome);
add(other);
validate();
} else {
counter.setText(couterValue + ""); // Update the UI counter
couterValue--;
}
}
}, 0, 1000);
}
private void buildWelcomePanel() {
welcome = new JPanel();
counter = new JLabel();
welcome.add(counter);
}
private void buildOtherPanel() {
other = new JPanel();
JLabel otherStuff = new JLabel("Anything else ...");
other.add(otherStuff);
}
public static void main(String[] args) {
new T();
}
}