分析你的代码发现你的paintcomponent没有得到运行。因为查看jdk文档,你就会发现repaint()
执行的操作是
repaint
public void repaint()Repaints this component. If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible. Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
Since:
JDK1.0
See Also:
update(Graphics)
我采用了修改paint()的办法。不知道你有没有改好,这是我修改的结果,可以达到你的要求。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InputDialog2 extends JFrame implements ActionListener{
JButton theButton;
String input; public InputDialog2(){
super("InputDialog2");
theButton=new JButton("click me");
JPanel pane=new JPanel();
input="";
pane.add(theButton);
theButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(pane);
setLocation(400,300);
setSize(300,200);
setVisible(true);
} public void actionPerformed(ActionEvent e){
if(e.getSource()==theButton)
input=JOptionPane.showInputDialog(this,"Please input something");
repaint();
}
//The method used to PAINT the UI.
public void paint(Graphics g){
super.paint(g);
System.out.println(input);
if(input==null) input="You Pressed Cancel";
g.drawString(input,100,100);
} public static void main(String[] args) {
InputDialog2 id=new InputDialog2();
}
}
执行的操作是
repaint
public void repaint()Repaints this component. If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible. Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.
Since:
JDK1.0
See Also:
update(Graphics)
我采用了修改paint()的办法。不知道你有没有改好,这是我修改的结果,可以达到你的要求。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InputDialog2 extends JFrame implements ActionListener{
JButton theButton;
String input; public InputDialog2(){
super("InputDialog2");
theButton=new JButton("click me");
JPanel pane=new JPanel();
input="";
pane.add(theButton);
theButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(pane);
setLocation(400,300);
setSize(300,200);
setVisible(true);
} public void actionPerformed(ActionEvent e){
if(e.getSource()==theButton)
input=JOptionPane.showInputDialog(this,"Please input something");
repaint();
}
//The method used to PAINT the UI.
public void paint(Graphics g){
super.paint(g);
System.out.println(input);
if(input==null) input="You Pressed Cancel";
g.drawString(input,100,100);
} public static void main(String[] args) {
InputDialog2 id=new InputDialog2();
}
}
直接把输出的语句写在
actioPerform中,这样就行了
在actionPerform中添加如下代码:
Graphics g = pane.getGraphics();
g.drawString(10,20,input);
这样就行了。