请问:在下的代码运行后,为何只在第一次按下按钮后,能显示出jl(JLabel),但jp(JScrollPane)不显示,再次按下按钮后,就没有任何变化了,只能点击最大化,或还原按钮,才能再次刷新JFrame,为何???请高人指点,应该如何改写代码,使之,每按一下按钮都能显示出需要显示的组件来,谢谢import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
JButton jb=new JButton("实始化时产生的按钮");
public Main() {
getContentPane().setLayout(new FlowLayout());
add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
jb.setText("被按下了");
JLabel jl=new JLabel("按下后新增的标签");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jl);
javax.swing.JScrollPane jp=new JScrollPane();
jp.setSize(640,480);
jp.setBackground(Color.BLUE);
jp.add(new JLabel("滑动窗中的标签"));
getContentPane().add(jp);
jp.setVisible(true);
}
});
} public static void main(String[] args) {
// TODO code application logic here]
Main m=new Main();m.setSize(800,600);
m.setVisible(true);
}
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
JButton jb=new JButton("实始化时产生的按钮");
public Main() {
getContentPane().setLayout(new FlowLayout());
add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
jb.setText("被按下了");
JLabel jl=new JLabel("按下后新增的标签");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jl);
javax.swing.JScrollPane jp=new JScrollPane();
jp.setSize(640,480);
jp.setBackground(Color.BLUE);
jp.add(new JLabel("滑动窗中的标签"));
getContentPane().add(jp);
jp.setVisible(true);
}
});
} public static void main(String[] args) {
// TODO code application logic here]
Main m=new Main();m.setSize(800,600);
m.setVisible(true);
}
按钮的actionPerformed方法: public void actionPerformed(ActionEvent event)
{ jb.setText("被按下了");
JLabel jl=new JLabel("按下后新增的标签");
//这里不需要重新设置布局了,因为你在构造方法里已经更改过布局了
getContentPane().add(jl);
javax.swing.JScrollPane jp=new JScrollPane();
jp.setSize(640,480);
jp.setBackground(Color.BLUE);
//下面一句,往滑动窗中添加组件不能用add(),而是setViewporView();
//因为JScrollPane是一种特殊的容器,他只能显示一个组件。
jp.setViewportView(new JLabel("滑动窗中的标签"));
getContentPane().add(jp);
jp.setVisible(true);
//添加完组件后,调用下面的方法,作用是验证此组件所有的子组件。以保证新添加的组件能立即显示出来。
validate();
}
public static void main(String[] args) {
// TODO code application logic here]
Main m=new Main();
m.setSize(800,600);
//最后,在main方法中加一句:设置点击窗体上的关闭按钮后,退出程序。
m.setDefaultCloseOperation(EXIT_ON_CLOSE);
m.setVisible(true);
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
JButton jb=new JButton("实始化时产生的按钮");
javax.swing.JScrollPane jp=new JScrollPane(); //这个放在外面就可以见到 JScrollPane 啦!
public Main() {
getContentPane().setLayout(new FlowLayout());
add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
jb.setText("被按下了");
JLabel jl=new JLabel("按下后新增的标签");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jl);
jp.setSize(640,480);
jp.setBackground(Color.BLUE);
jp.add(new JLabel("滑动窗中的标签"));
getContentPane().add(jp);
jp.setVisible(true); }
}); } public static void main(String[] args) {
// TODO code application logic here]
Main m=new Main();m.setSize(800,600);
m.setVisible(true);
}}
-------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Main extends JFrame { JButton jb = new JButton("实始化时产生的按钮"); public Main() {
getContentPane().setLayout(new FlowLayout());
add(jb); jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Main.this.jb.setText("被按下了");
JLabel jl = new JLabel("按下后新增的标签");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jl); javax.swing.JScrollPane jp = new JScrollPane();
// jp.setSize(640, 480);
jp.setBackground(Color.BLUE); jp.setViewportView(new JLabel("滑动窗中的标签"));
getContentPane().add(jp);
getContentPane().validate();
System.out.println(getContentPane().getComponentCount());
jp.setVisible(true); }
}); } public static void main(String[] args) {
// TODO code application logic here]
Main m = new Main();
m.setSize(800, 600);
m.setVisible(true);
m.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}