Hold graphic screen
In my program I have a class GraphDisplay
extended from JPanel that is used to create graphics... In its constructor I have a function display() that creates all the stuff that I want including a button for listening...
In some other class MainGUI
I create a object of above class after a dialog action from user and what happens is that the above class object gets created and gets displayed for fraction of a second before dissapearing....
Is there a way that I can tell MainGUI
to wait on above object for its input rather than to get closed....
GraphDisplay is something like this ....
public class GraphDisplay extends JPanel {
private static final long serialVersionUID = 1L;
GraphDisplay(String source , String destination , List<Node> nodes , List<GUIEdge> edges , List<GUIEdge> spanedges)
{
//assigning values
this.display();
}
public void display() {
frame1 = new JFrame();
frame1.setSize(400,400);
frame1.setVisible(true);
frame1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame1.setBackground(Color.lightGray);
frame1.repaint();
JButton next = new JButton("NEXT");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello");
}
});
}
public void paintComponent(Graphics g) {
//does some painting
开发者_运维知识库 }
Thanks
If you want to draw stuff on JPanel
, you should be overriding paintComponent(Graphics)
method. It will automatically be called every time the JPanel needs to paint itself.
@Override
public paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(...);
g.drawOval(...);
/*Other stuff*/
}
You are creating the JFrame inside your JPanel. The most basic example I can give you about what you want to do is below, I think you can see the problem:
public class Test extends JFrame{
public static void main(String[] args){
JFrame frame = new JFrame();
MyPanel myPanel = new MyPanel();
frame.add(myPanel);
frame.pack();
frame.setVisible(true);
}
}
class MyPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
g.drawLine(10, 10, 100, 100);
}
}
If the entire program is closing (not clear from your question), then it is doing so as a result of some method you call (e.g. System.exit());
Is MainGUI performing additional actions after it creates the GraphDisplay? If so, remember that creating a panel does not stop the current flow of execution.
A clean way to close the program once the GraphDisplay is complete would be to write a listenerInterface (with a single 'I'm Done' method), implement that interface with the MainGUI class (to close the application) and pass the MainGUI class to the GraphDisplay panel. The GraphDisplay panel can then call 'I'm Done' when the user performs the required action.
精彩评论