我对Java还是很陌生,但是对层次结构仍然不熟悉,我的代码由一个主要的JFrame组成,我想充当所有面板的控制器。我有一个扩展为JPanel的ToolBar类,然后还有一个扩展为JPanel的EditorPanel类,我只是想使用我的ToolBar中的按钮在EditorPanel上添加另一个名为IntroPanel的面板。
当我尝试直接从MainFrame中添加IntroPanel时,它可以工作,但是当我通过事件监听器触发它时,它不起作用。
其中可能有什么问题?
package gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainFrame extends JFrame {
private EditorPanel editorPanel;
private Toolbar toolbar;
public MainFrame() {
super("Ankanaya");
setLayout(new BorderLayout());
toolbar = new Toolbar();
editorPanel = new EditorPanel();
toolbar.setEditorListener(new EditorListener() {
public void editorEventOccured(EditorEvent ev) {
setIntroPanel(editorPanel);
}
});
//editorPanel.add(new IntroPanel());
add(editorPanel, BorderLayout.CENTER);
add(toolbar, BorderLayout.NORTH);
setSize(900, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void setIntroPanel(EditorPanel panel) {
EditorPanel editorPanel = panel;
editorPanel.add(new IntroPanel());
}
}
package gui;
import java.util.EventObject;
public class EditorEvent extends EventObject{
public EditorEvent(Object source) {
super(source);//here source is going to be toolbar
}
}
package gui;
import java.util.EventListener;
public interface EditorListener extends EventListener {
public void editorEventOccured(EditorEvent ev);
}
package gui;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Toolbar extends JPanel{
private JButton introButton;
private EditorListener editorListener;
private MainFrame mainframe;
public Toolbar() {
setBorder(BorderFactory.createEtchedBorder());
introButton = new JButton("Add Intro");
setLayout(new FlowLayout(FlowLayout.LEFT));
add(introButton);//adding to the layout
introButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EditorEvent ev = new EditorEvent(this);
if(editorListener != null) {
editorListener.editorEventOccured(ev);
}
}
});
}
public void setEditorListener(EditorListener listener) {
this.editorListener = listener;
}
}
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class EditorPanel extends JPanel{
public EditorPanel() {
setLayout(new FlowLayout());
}
}
package gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class IntroPanel extends JPanel{
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
public IntroPanel() {
text1 = new JTextField(2);
text2 = new JTextField(2);
text3 = new JTextField(2);
text4 = new JTextField(2);
add(text1);
add(text2);
add(text3);
add(text4);
setLayout(new GridLayout(1,4));
setBackground(Color.black);
}
}
答案 0 :(得分:0)
我认为,将revalidate
添加到setIntroPanel
的末尾。 AWT不会在不被告知的情况下进行重新布局和重新粉刷。