我正在尝试实现一个按钮,用于激活在另一个窗口中绘制的线条。
我投了3个班:
Table_Workspace - 扩展JFrame类包含按钮;
Table_graph - 扩展JFrame类。除了其他之外,它还包含一个ActiveZone类型的元素
ActiveZone - 扩展JPanel类,它覆盖paintComponent函数:
=============================================== =================================
public class ActiveZone extends JPanel {
@Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.drawLine(1, 1, 20, 20);
System.out.print("Add img");
}
}
public class Table_Workspace extends JFrame {
//.....
btnBuild = new JButton("Build");
contentPane.add(btnBuild);
btnBuild.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ActiveZone activezone = new ActiveZone();
Table_graph table_graph = new Table_graph(); //window in which we draw
//sl_contentPane = SpringLayout() from Table_graph
table_graph.sl_contentPane.putConstraint(SpringLayout.NORTH,
activezone, 0, SpringLayout.NORTH,
table_graph.GridPanel);
table_graph.sl_contentPane
.putConstraint(SpringLayout.EAST, activezone, 0,
SpringLayout.EAST, table_graph.GridPanel);
table_graph.sl_contentPane.putConstraint(SpringLayout.SOUTH,
activezone, 0, SpringLayout.SOUTH,
table_graph.GridPanel);
table_graph.sl_contentPane
.putConstraint(SpringLayout.WEST, activezone, 0,
SpringLayout.WEST, table_graph.GridPanel);
table_graph.contentPane.add(activezone, 0);
table_graph.contentPane.revalidate();
table_graph.contentPane.repaint();
activezone.setOpaque(false);
}
});
=============================================== =================================
在执行期间,它不输出任何内容,在System.out中没有输出,也没有任何错误消息。
但是如果我从Table_Workspace获取组件创建代码 把它放到Table_graph一切正常,然后绘制线条。 像这样:
=============================================== ==================================
activezone = new ActiveZone();
sl_contentPane.putConstraint(SpringLayout.NORTH, activezone, 0,
SpringLayout.NORTH, GridPanel);
sl_contentPane.putConstraint(SpringLayout.EAST, activezone, 0,
SpringLayout.EAST, GridPanel);
sl_contentPane.putConstraint(SpringLayout.SOUTH, activezone, 0,
SpringLayout.SOUTH, GridPanel);
sl_contentPane.putConstraint(SpringLayout.WEST, activezone, 0,
SpringLayout.WEST, GridPanel);
contentPane.add(activezone, 0);
activezone.setOpaque(false);
=============================================== ===================================
请告诉我,什么是错的,我怎么能实现这个想法?
事先先谢谢你!